Skip to content

Fix schema objects with default + nullable #1669

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 1 commit into from
May 19, 2024
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"devDependencies": {
"@biomejs/biome": "^1.7.3",
"@changesets/changelog-github": "^0.5.0",
"@changesets/cli": "^2.27.1",
"@changesets/cli": "^2.27.2",
"del-cli": "^5.1.0",
"prettier": "^3.2.5",
"typescript": "^5.4.5"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9894,7 +9894,7 @@ export interface components {
* "web"
* ]
*/
tags: string[] | null;
tags: string[];
/**
* @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 @@ -29148,7 +29148,7 @@ export type components = {
* @default RIGHT
* @enum {string|null}
*/
readonly start_side: "LEFT" | "RIGHT" | null;
readonly start_side: "LEFT" | "RIGHT";
/**
* @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 @@ -30255,7 +30255,7 @@ export type components = {
* @default RIGHT
* @enum {string|null}
*/
readonly start_side: "LEFT" | "RIGHT" | null;
readonly start_side: "LEFT" | "RIGHT";
/**
* @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 @@ -101600,7 +101600,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 | null;
readonly environment?: string;
/** @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 @@ -29148,7 +29148,7 @@ export interface components {
* @default RIGHT
* @enum {string|null}
*/
readonly start_side: "LEFT" | "RIGHT" | null;
readonly start_side: "LEFT" | "RIGHT";
/**
* @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 @@ -30255,7 +30255,7 @@ export interface components {
* @default RIGHT
* @enum {string|null}
*/
readonly start_side: "LEFT" | "RIGHT" | null;
readonly start_side: "LEFT" | "RIGHT";
/**
* @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 @@ -101600,7 +101600,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 | null;
readonly environment?: string;
/** @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 @@ -101899,7 +101899,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 | null;
environment?: string;
/** @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 @@ -29148,7 +29148,7 @@ export interface components {
* @default RIGHT
* @enum {string|null}
*/
start_side: "LEFT" | "RIGHT" | null;
start_side: "LEFT" | "RIGHT";
/**
* @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 @@ -30255,7 +30255,7 @@ export interface components {
* @default RIGHT
* @enum {string|null}
*/
start_side: "LEFT" | "RIGHT" | null;
start_side: "LEFT" | "RIGHT";
/**
* @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 @@ -101600,7 +101600,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 | null;
environment?: string;
/** @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
23 changes: 15 additions & 8 deletions packages/openapi-typescript/src/transform/schema-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ export function transformSchemaObjectWithComposition(
return ts.factory.createTypeReferenceNode(enumType.name);
}
const enumType = schemaObject.enum.map(tsLiteral);
if ((Array.isArray(schemaObject.type) && schemaObject.type.includes("null")) || schemaObject.nullable) {
if (
((Array.isArray(schemaObject.type) && schemaObject.type.includes("null")) || schemaObject.nullable) &&
!schemaObject.default
) {
enumType.push(NULL);
}

Expand Down Expand Up @@ -242,7 +245,7 @@ export function transformSchemaObjectWithComposition(
// if final type could be generated, return intersection of all members
if (finalType) {
// deprecated nullable
if (schemaObject.nullable) {
if (schemaObject.nullable && !schemaObject.default) {
return tsNullable([finalType]);
}
return finalType;
Expand Down Expand Up @@ -364,7 +367,7 @@ function transformSchemaObjectCore(schemaObject: SchemaObject, options: Transfor
// polymorphic, or 3.1 nullable
if (Array.isArray(schemaObject.type) && !Array.isArray(schemaObject)) {
// skip any primitive types that appear in oneOf as well
let uniqueTypes: ts.TypeNode[] = [];
const uniqueTypes: ts.TypeNode[] = [];
if (Array.isArray(schemaObject.oneOf)) {
for (const t of schemaObject.type) {
if (
Expand All @@ -383,11 +386,15 @@ function transformSchemaObjectCore(schemaObject: SchemaObject, options: Transfor
);
}
} else {
uniqueTypes = schemaObject.type.map((t) =>
t === "null" || t === null
? NULL
: transformSchemaObject({ ...schemaObject, type: t } as SchemaObject, options),
);
for (const t of schemaObject.type) {
if (t === "null" || t === null) {
if (!schemaObject.default) {
uniqueTypes.push(NULL);
}
} else {
uniqueTypes.push(transformSchemaObject({ ...schemaObject, type: t } as SchemaObject, options));
}
}
}
return tsUnion(uniqueTypes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,27 @@ describe("transformSchemaObject > string", () => {
want: '"A" | "B" | "C" | null',
},
],
[
"default + nullable",
{
given: { type: ["string", "null"], default: "en" },
want: "string",
},
],
[
"default + nullable + enum",
{
given: { type: ["string", "null"], enum: ["en", "es", "fr", "de"], default: "en" },
want: '"en" | "es" | "fr" | "de"',
},
],
[
"default + nullable (deprecated syntax)",
{
given: { type: "string", default: "en", nullable: true },
want: "string",
},
],
];

for (const [testName, { given, want, options = DEFAULT_OPTIONS, ci }] of tests) {
Expand Down
26 changes: 13 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading