diff --git a/packages/openapi-typescript/examples/digital-ocean-api.ts b/packages/openapi-typescript/examples/digital-ocean-api.ts index e484f567b..c15138163 100644 --- a/packages/openapi-typescript/examples/digital-ocean-api.ts +++ b/packages/openapi-typescript/examples/digital-ocean-api.ts @@ -12326,7 +12326,7 @@ export interface components { * "web" * ] */ - tags: string[]; + tags: string[] | null; /** * @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. * @example #cloud-config diff --git a/packages/openapi-typescript/examples/github-api-export-type-immutable.ts b/packages/openapi-typescript/examples/github-api-export-type-immutable.ts index 699297eb3..483598b53 100644 --- a/packages/openapi-typescript/examples/github-api-export-type-immutable.ts +++ b/packages/openapi-typescript/examples/github-api-export-type-immutable.ts @@ -32229,7 +32229,7 @@ export type components = { * @default RIGHT * @enum {string|null} */ - readonly start_side: "LEFT" | "RIGHT"; + readonly start_side: "LEFT" | "RIGHT" | null; /** * @description The line of the blob to which the comment applies. The last line of the range for a multi-line comment * @example 2 @@ -33004,7 +33004,7 @@ export type components = { * @default RIGHT * @enum {string|null} */ - readonly start_side: "LEFT" | "RIGHT"; + readonly start_side: "LEFT" | "RIGHT" | null; /** * @description The line of the blob to which the comment applies. The last line of the range for a multi-line comment * @example 2 @@ -108426,7 +108426,7 @@ export interface operations { /** @description The name of the task for the deployment (e.g., `deploy` or `deploy:migrations`). */ readonly task?: string; /** @description The name of the environment that was deployed to (e.g., `staging` or `production`). */ - readonly environment?: string; + readonly environment?: string | null; /** @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)." */ readonly per_page?: components["parameters"]["per-page"]; /** @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)." */ diff --git a/packages/openapi-typescript/examples/github-api-immutable.ts b/packages/openapi-typescript/examples/github-api-immutable.ts index 25a97d3f9..323ea9428 100644 --- a/packages/openapi-typescript/examples/github-api-immutable.ts +++ b/packages/openapi-typescript/examples/github-api-immutable.ts @@ -32229,7 +32229,7 @@ export interface components { * @default RIGHT * @enum {string|null} */ - readonly start_side: "LEFT" | "RIGHT"; + readonly start_side: "LEFT" | "RIGHT" | null; /** * @description The line of the blob to which the comment applies. The last line of the range for a multi-line comment * @example 2 @@ -33004,7 +33004,7 @@ export interface components { * @default RIGHT * @enum {string|null} */ - readonly start_side: "LEFT" | "RIGHT"; + readonly start_side: "LEFT" | "RIGHT" | null; /** * @description The line of the blob to which the comment applies. The last line of the range for a multi-line comment * @example 2 @@ -108426,7 +108426,7 @@ export interface operations { /** @description The name of the task for the deployment (e.g., `deploy` or `deploy:migrations`). */ readonly task?: string; /** @description The name of the environment that was deployed to (e.g., `staging` or `production`). */ - readonly environment?: string; + readonly environment?: string | null; /** @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)." */ readonly per_page?: components["parameters"]["per-page"]; /** @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)." */ diff --git a/packages/openapi-typescript/examples/github-api-next.ts b/packages/openapi-typescript/examples/github-api-next.ts index 3e54966f1..454391ea9 100644 --- a/packages/openapi-typescript/examples/github-api-next.ts +++ b/packages/openapi-typescript/examples/github-api-next.ts @@ -108750,7 +108750,7 @@ export interface operations { /** @description The name of the task for the deployment (e.g., `deploy` or `deploy:migrations`). */ task?: string; /** @description The name of the environment that was deployed to (e.g., `staging` or `production`). */ - environment?: string; + environment?: string | null; /** @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)." */ per_page?: components["parameters"]["per-page"]; /** @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)." */ diff --git a/packages/openapi-typescript/examples/github-api-required.ts b/packages/openapi-typescript/examples/github-api-required.ts index ed8280c84..ff4362624 100644 --- a/packages/openapi-typescript/examples/github-api-required.ts +++ b/packages/openapi-typescript/examples/github-api-required.ts @@ -32229,7 +32229,7 @@ export interface components { * @default RIGHT * @enum {string|null} */ - start_side: "LEFT" | "RIGHT"; + start_side: "LEFT" | "RIGHT" | null; /** * @description The line of the blob to which the comment applies. The last line of the range for a multi-line comment * @example 2 @@ -33004,7 +33004,7 @@ export interface components { * @default RIGHT * @enum {string|null} */ - start_side: "LEFT" | "RIGHT"; + start_side: "LEFT" | "RIGHT" | null; /** * @description The line of the blob to which the comment applies. The last line of the range for a multi-line comment * @example 2 @@ -108426,7 +108426,7 @@ export interface operations { /** @description The name of the task for the deployment (e.g., `deploy` or `deploy:migrations`). */ task?: string; /** @description The name of the environment that was deployed to (e.g., `staging` or `production`). */ - environment?: string; + environment?: string | null; /** @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)." */ per_page?: components["parameters"]["per-page"]; /** @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)." */ diff --git a/packages/openapi-typescript/examples/github-api-root-types.ts b/packages/openapi-typescript/examples/github-api-root-types.ts index f010442dc..dd0d43544 100644 --- a/packages/openapi-typescript/examples/github-api-root-types.ts +++ b/packages/openapi-typescript/examples/github-api-root-types.ts @@ -32229,7 +32229,7 @@ export interface components { * @default RIGHT * @enum {string|null} */ - start_side: "LEFT" | "RIGHT"; + start_side: "LEFT" | "RIGHT" | null; /** * @description The line of the blob to which the comment applies. The last line of the range for a multi-line comment * @example 2 @@ -33004,7 +33004,7 @@ export interface components { * @default RIGHT * @enum {string|null} */ - start_side: "LEFT" | "RIGHT"; + start_side: "LEFT" | "RIGHT" | null; /** * @description The line of the blob to which the comment applies. The last line of the range for a multi-line comment * @example 2 @@ -109453,7 +109453,7 @@ export interface operations { /** @description The name of the task for the deployment (e.g., `deploy` or `deploy:migrations`). */ task?: string; /** @description The name of the environment that was deployed to (e.g., `staging` or `production`). */ - environment?: string; + environment?: string | null; /** @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)." */ per_page?: components["parameters"]["per-page"]; /** @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)." */ diff --git a/packages/openapi-typescript/examples/github-api.ts b/packages/openapi-typescript/examples/github-api.ts index 13489f8f8..510f78b83 100644 --- a/packages/openapi-typescript/examples/github-api.ts +++ b/packages/openapi-typescript/examples/github-api.ts @@ -32229,7 +32229,7 @@ export interface components { * @default RIGHT * @enum {string|null} */ - start_side: "LEFT" | "RIGHT"; + start_side: "LEFT" | "RIGHT" | null; /** * @description The line of the blob to which the comment applies. The last line of the range for a multi-line comment * @example 2 @@ -33004,7 +33004,7 @@ export interface components { * @default RIGHT * @enum {string|null} */ - start_side: "LEFT" | "RIGHT"; + start_side: "LEFT" | "RIGHT" | null; /** * @description The line of the blob to which the comment applies. The last line of the range for a multi-line comment * @example 2 @@ -108426,7 +108426,7 @@ export interface operations { /** @description The name of the task for the deployment (e.g., `deploy` or `deploy:migrations`). */ task?: string; /** @description The name of the environment that was deployed to (e.g., `staging` or `production`). */ - environment?: string; + environment?: string | null; /** @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)." */ per_page?: components["parameters"]["per-page"]; /** @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)." */ diff --git a/packages/openapi-typescript/src/transform/schema-object.ts b/packages/openapi-typescript/src/transform/schema-object.ts index a41ce0e69..9f0a3b2bf 100644 --- a/packages/openapi-typescript/src/transform/schema-object.ts +++ b/packages/openapi-typescript/src/transform/schema-object.ts @@ -130,10 +130,7 @@ export function transformSchemaObjectWithComposition( return hasNull ? tsUnion([ref, NULL]) : ref; } const enumType = schemaObject.enum.map(tsLiteral); - if ( - ((Array.isArray(schemaObject.type) && schemaObject.type.includes("null")) || schemaObject.nullable) && - !schemaObject.default - ) { + if ((Array.isArray(schemaObject.type) && schemaObject.type.includes("null")) || schemaObject.nullable) { enumType.push(NULL); } @@ -266,7 +263,7 @@ export function transformSchemaObjectWithComposition( } } - if (finalType !== UNKNOWN && schemaObject.nullable && !schemaObject.default) { + if (finalType !== UNKNOWN && schemaObject.nullable) { finalType = tsNullable([finalType]); } @@ -405,9 +402,7 @@ function transformSchemaObjectCore(schemaObject: SchemaObject, options: Transfor } else { for (const t of schemaObject.type) { if (t === "null" || t === null) { - if (!schemaObject.default) { - uniqueTypes.push(NULL); - } + uniqueTypes.push(NULL); } else { uniqueTypes.push(transformSchemaObject({ ...schemaObject, type: t } as SchemaObject, options)); } diff --git a/packages/openapi-typescript/test/transform/schema-object/string.test.ts b/packages/openapi-typescript/test/transform/schema-object/string.test.ts index e5e9a5ac1..54033d404 100644 --- a/packages/openapi-typescript/test/transform/schema-object/string.test.ts +++ b/packages/openapi-typescript/test/transform/schema-object/string.test.ts @@ -102,21 +102,21 @@ describe("transformSchemaObject > string", () => { "default + nullable", { given: { type: ["string", "null"], default: "en" }, - want: "string", + want: "string | null", }, ], [ "default + nullable + enum", { given: { type: ["string", "null"], enum: ["en", "es", "fr", "de"], default: "en" }, - want: '"en" | "es" | "fr" | "de"', + want: '"en" | "es" | "fr" | "de" | null', }, ], [ "default + nullable (deprecated syntax)", { given: { type: "string", default: "en", nullable: true }, - want: "string", + want: "string | null", }, ], ];