Skip to content

Commit 1e6f967

Browse files
committed
Don't remove null type from enums if default is present.
1 parent 9826652 commit 1e6f967

File tree

7 files changed

+12
-15
lines changed

7 files changed

+12
-15
lines changed

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

+2-2
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

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

+2-2
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

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

+2-2
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

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

+2-2
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

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

+2-2
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

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

+1-4
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

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ describe("transformSchemaObject > string", () => {
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
[

0 commit comments

Comments
 (0)