Skip to content

Don't remove null type if a default is present. #2145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)." */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)." */
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-typescript/examples/github-api-next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)." */
Expand Down
6 changes: 3 additions & 3 deletions packages/openapi-typescript/examples/github-api-required.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)." */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)." */
Expand Down
6 changes: 3 additions & 3 deletions packages/openapi-typescript/examples/github-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)." */
Expand Down
11 changes: 3 additions & 8 deletions packages/openapi-typescript/src/transform/schema-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -266,7 +263,7 @@ export function transformSchemaObjectWithComposition(
}
}

if (finalType !== UNKNOWN && schemaObject.nullable && !schemaObject.default) {
if (finalType !== UNKNOWN && schemaObject.nullable) {
finalType = tsNullable([finalType]);
}

Expand Down Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
],
];
Expand Down