Skip to content

Commit d79efae

Browse files
authored
Don't remove null type if a default is present. (#2145)
* Don't remove `null` type if a default is present. * Don't remove `null` type from enums if default is present. * Don't remove `null` type if default is present (legacy)
1 parent a041c2d commit d79efae

File tree

9 files changed

+23
-28
lines changed

9 files changed

+23
-28
lines changed

packages/openapi-typescript/examples/digital-ocean-api.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12326,7 +12326,7 @@ export interface components {
1232612326
* "web"
1232712327
* ]
1232812328
*/
12329-
tags: string[];
12329+
tags: string[] | null;
1233012330
/**
1233112331
* @description A string containing 'user data' which may be used to configure the Droplet on first boot, often a 'cloud-config' file or Bash script. It must be plain text and may not exceed 64 KiB in size.
1233212332
* @example #cloud-config

packages/openapi-typescript/examples/github-api-export-type-immutable.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -32229,7 +32229,7 @@ export type components = {
3222932229
* @default RIGHT
3223032230
* @enum {string|null}
3223132231
*/
32232-
readonly start_side: "LEFT" | "RIGHT";
32232+
readonly start_side: "LEFT" | "RIGHT" | null;
3223332233
/**
3223432234
* @description The line of the blob to which the comment applies. The last line of the range for a multi-line comment
3223532235
* @example 2
@@ -33004,7 +33004,7 @@ export type components = {
3300433004
* @default RIGHT
3300533005
* @enum {string|null}
3300633006
*/
33007-
readonly start_side: "LEFT" | "RIGHT";
33007+
readonly start_side: "LEFT" | "RIGHT" | null;
3300833008
/**
3300933009
* @description The line of the blob to which the comment applies. The last line of the range for a multi-line comment
3301033010
* @example 2
@@ -108426,7 +108426,7 @@ export interface operations {
108426108426
/** @description The name of the task for the deployment (e.g., `deploy` or `deploy:migrations`). */
108427108427
readonly task?: string;
108428108428
/** @description The name of the environment that was deployed to (e.g., `staging` or `production`). */
108429-
readonly environment?: string;
108429+
readonly environment?: string | null;
108430108430
/** @description The number of results per page (max 100). For more information, see "[Using pagination in the REST API](https://docs.github.com/rest/using-the-rest-api/using-pagination-in-the-rest-api)." */
108431108431
readonly per_page?: components["parameters"]["per-page"];
108432108432
/** @description The page number of the results to fetch. For more information, see "[Using pagination in the REST API](https://docs.github.com/rest/using-the-rest-api/using-pagination-in-the-rest-api)." */

packages/openapi-typescript/examples/github-api-immutable.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -32229,7 +32229,7 @@ export interface components {
3222932229
* @default RIGHT
3223032230
* @enum {string|null}
3223132231
*/
32232-
readonly start_side: "LEFT" | "RIGHT";
32232+
readonly start_side: "LEFT" | "RIGHT" | null;
3223332233
/**
3223432234
* @description The line of the blob to which the comment applies. The last line of the range for a multi-line comment
3223532235
* @example 2
@@ -33004,7 +33004,7 @@ export interface components {
3300433004
* @default RIGHT
3300533005
* @enum {string|null}
3300633006
*/
33007-
readonly start_side: "LEFT" | "RIGHT";
33007+
readonly start_side: "LEFT" | "RIGHT" | null;
3300833008
/**
3300933009
* @description The line of the blob to which the comment applies. The last line of the range for a multi-line comment
3301033010
* @example 2
@@ -108426,7 +108426,7 @@ export interface operations {
108426108426
/** @description The name of the task for the deployment (e.g., `deploy` or `deploy:migrations`). */
108427108427
readonly task?: string;
108428108428
/** @description The name of the environment that was deployed to (e.g., `staging` or `production`). */
108429-
readonly environment?: string;
108429+
readonly environment?: string | null;
108430108430
/** @description The number of results per page (max 100). For more information, see "[Using pagination in the REST API](https://docs.github.com/rest/using-the-rest-api/using-pagination-in-the-rest-api)." */
108431108431
readonly per_page?: components["parameters"]["per-page"];
108432108432
/** @description The page number of the results to fetch. For more information, see "[Using pagination in the REST API](https://docs.github.com/rest/using-the-rest-api/using-pagination-in-the-rest-api)." */

packages/openapi-typescript/examples/github-api-next.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -108750,7 +108750,7 @@ export interface operations {
108750108750
/** @description The name of the task for the deployment (e.g., `deploy` or `deploy:migrations`). */
108751108751
task?: string;
108752108752
/** @description The name of the environment that was deployed to (e.g., `staging` or `production`). */
108753-
environment?: string;
108753+
environment?: string | null;
108754108754
/** @description The number of results per page (max 100). For more information, see "[Using pagination in the REST API](https://docs.github.com/rest/using-the-rest-api/using-pagination-in-the-rest-api)." */
108755108755
per_page?: components["parameters"]["per-page"];
108756108756
/** @description The page number of the results to fetch. For more information, see "[Using pagination in the REST API](https://docs.github.com/rest/using-the-rest-api/using-pagination-in-the-rest-api)." */

packages/openapi-typescript/examples/github-api-required.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -32229,7 +32229,7 @@ export interface components {
3222932229
* @default RIGHT
3223032230
* @enum {string|null}
3223132231
*/
32232-
start_side: "LEFT" | "RIGHT";
32232+
start_side: "LEFT" | "RIGHT" | null;
3223332233
/**
3223432234
* @description The line of the blob to which the comment applies. The last line of the range for a multi-line comment
3223532235
* @example 2
@@ -33004,7 +33004,7 @@ export interface components {
3300433004
* @default RIGHT
3300533005
* @enum {string|null}
3300633006
*/
33007-
start_side: "LEFT" | "RIGHT";
33007+
start_side: "LEFT" | "RIGHT" | null;
3300833008
/**
3300933009
* @description The line of the blob to which the comment applies. The last line of the range for a multi-line comment
3301033010
* @example 2
@@ -108426,7 +108426,7 @@ export interface operations {
108426108426
/** @description The name of the task for the deployment (e.g., `deploy` or `deploy:migrations`). */
108427108427
task?: string;
108428108428
/** @description The name of the environment that was deployed to (e.g., `staging` or `production`). */
108429-
environment?: string;
108429+
environment?: string | null;
108430108430
/** @description The number of results per page (max 100). For more information, see "[Using pagination in the REST API](https://docs.github.com/rest/using-the-rest-api/using-pagination-in-the-rest-api)." */
108431108431
per_page?: components["parameters"]["per-page"];
108432108432
/** @description The page number of the results to fetch. For more information, see "[Using pagination in the REST API](https://docs.github.com/rest/using-the-rest-api/using-pagination-in-the-rest-api)." */

packages/openapi-typescript/examples/github-api-root-types.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -32229,7 +32229,7 @@ export interface components {
3222932229
* @default RIGHT
3223032230
* @enum {string|null}
3223132231
*/
32232-
start_side: "LEFT" | "RIGHT";
32232+
start_side: "LEFT" | "RIGHT" | null;
3223332233
/**
3223432234
* @description The line of the blob to which the comment applies. The last line of the range for a multi-line comment
3223532235
* @example 2
@@ -33004,7 +33004,7 @@ export interface components {
3300433004
* @default RIGHT
3300533005
* @enum {string|null}
3300633006
*/
33007-
start_side: "LEFT" | "RIGHT";
33007+
start_side: "LEFT" | "RIGHT" | null;
3300833008
/**
3300933009
* @description The line of the blob to which the comment applies. The last line of the range for a multi-line comment
3301033010
* @example 2
@@ -109453,7 +109453,7 @@ export interface operations {
109453109453
/** @description The name of the task for the deployment (e.g., `deploy` or `deploy:migrations`). */
109454109454
task?: string;
109455109455
/** @description The name of the environment that was deployed to (e.g., `staging` or `production`). */
109456-
environment?: string;
109456+
environment?: string | null;
109457109457
/** @description The number of results per page (max 100). For more information, see "[Using pagination in the REST API](https://docs.github.com/rest/using-the-rest-api/using-pagination-in-the-rest-api)." */
109458109458
per_page?: components["parameters"]["per-page"];
109459109459
/** @description The page number of the results to fetch. For more information, see "[Using pagination in the REST API](https://docs.github.com/rest/using-the-rest-api/using-pagination-in-the-rest-api)." */

packages/openapi-typescript/examples/github-api.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -32229,7 +32229,7 @@ export interface components {
3222932229
* @default RIGHT
3223032230
* @enum {string|null}
3223132231
*/
32232-
start_side: "LEFT" | "RIGHT";
32232+
start_side: "LEFT" | "RIGHT" | null;
3223332233
/**
3223432234
* @description The line of the blob to which the comment applies. The last line of the range for a multi-line comment
3223532235
* @example 2
@@ -33004,7 +33004,7 @@ export interface components {
3300433004
* @default RIGHT
3300533005
* @enum {string|null}
3300633006
*/
33007-
start_side: "LEFT" | "RIGHT";
33007+
start_side: "LEFT" | "RIGHT" | null;
3300833008
/**
3300933009
* @description The line of the blob to which the comment applies. The last line of the range for a multi-line comment
3301033010
* @example 2
@@ -108426,7 +108426,7 @@ export interface operations {
108426108426
/** @description The name of the task for the deployment (e.g., `deploy` or `deploy:migrations`). */
108427108427
task?: string;
108428108428
/** @description The name of the environment that was deployed to (e.g., `staging` or `production`). */
108429-
environment?: string;
108429+
environment?: string | null;
108430108430
/** @description The number of results per page (max 100). For more information, see "[Using pagination in the REST API](https://docs.github.com/rest/using-the-rest-api/using-pagination-in-the-rest-api)." */
108431108431
per_page?: components["parameters"]["per-page"];
108432108432
/** @description The page number of the results to fetch. For more information, see "[Using pagination in the REST API](https://docs.github.com/rest/using-the-rest-api/using-pagination-in-the-rest-api)." */

packages/openapi-typescript/src/transform/schema-object.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,7 @@ export function transformSchemaObjectWithComposition(
130130
return hasNull ? tsUnion([ref, NULL]) : ref;
131131
}
132132
const enumType = schemaObject.enum.map(tsLiteral);
133-
if (
134-
((Array.isArray(schemaObject.type) && schemaObject.type.includes("null")) || schemaObject.nullable) &&
135-
!schemaObject.default
136-
) {
133+
if ((Array.isArray(schemaObject.type) && schemaObject.type.includes("null")) || schemaObject.nullable) {
137134
enumType.push(NULL);
138135
}
139136

@@ -266,7 +263,7 @@ export function transformSchemaObjectWithComposition(
266263
}
267264
}
268265

269-
if (finalType !== UNKNOWN && schemaObject.nullable && !schemaObject.default) {
266+
if (finalType !== UNKNOWN && schemaObject.nullable) {
270267
finalType = tsNullable([finalType]);
271268
}
272269

@@ -405,9 +402,7 @@ function transformSchemaObjectCore(schemaObject: SchemaObject, options: Transfor
405402
} else {
406403
for (const t of schemaObject.type) {
407404
if (t === "null" || t === null) {
408-
if (!schemaObject.default) {
409-
uniqueTypes.push(NULL);
410-
}
405+
uniqueTypes.push(NULL);
411406
} else {
412407
uniqueTypes.push(transformSchemaObject({ ...schemaObject, type: t } as SchemaObject, options));
413408
}

packages/openapi-typescript/test/transform/schema-object/string.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -102,21 +102,21 @@ describe("transformSchemaObject > string", () => {
102102
"default + nullable",
103103
{
104104
given: { type: ["string", "null"], default: "en" },
105-
want: "string",
105+
want: "string | null",
106106
},
107107
],
108108
[
109109
"default + nullable + enum",
110110
{
111111
given: { type: ["string", "null"], enum: ["en", "es", "fr", "de"], default: "en" },
112-
want: '"en" | "es" | "fr" | "de"',
112+
want: '"en" | "es" | "fr" | "de" | null',
113113
},
114114
],
115115
[
116116
"default + nullable (deprecated syntax)",
117117
{
118118
given: { type: "string", default: "en", nullable: true },
119-
want: "string",
119+
want: "string | null",
120120
},
121121
],
122122
];

0 commit comments

Comments
 (0)