Skip to content

Commit 4fc8c20

Browse files
fix: empty strings in enums when type [string, null] (#1200)
* test: add failing tests for enums with type [string, null] * fix: empty strings in enums when type [string, null] * test: change polyphormism test examples * refactor: remove outdated comment * chore: add changeset * chore: update autogenerated examples
1 parent f4dbd52 commit 4fc8c20

File tree

6 files changed

+213
-198
lines changed

6 files changed

+213
-198
lines changed

.changeset/shy-turtles-taste.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-typescript": patch
3+
---
4+
5+
Remove unexpected empty string in generated nullable polymophic enum types (["string", "null"])

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

+110-110
Large diffs are not rendered by default.

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

+82-82
Large diffs are not rendered by default.

packages/openapi-typescript/examples/octokit-ghes-3.6-diff-to-api.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3174,7 +3174,7 @@ export interface components {
31743174
* @description **Required when the `state` is `resolved`.** The reason for resolving the alert.
31753175
* @enum {string|null}
31763176
*/
3177-
"secret-scanning-alert-resolution": "" | "false_positive" | "wont_fix" | "revoked" | "used_in_tests" | null;
3177+
"secret-scanning-alert-resolution": null | "false_positive" | "wont_fix" | "revoked" | "used_in_tests";
31783178
/**
31793179
* Minimal Repository
31803180
* @description Minimal Repository

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ export function defaultSchemaObjectTransform(schemaObject: SchemaObject | Refere
5151
if (schemaObject.enum) {
5252
let items = schemaObject.enum as any[];
5353
if ("type" in schemaObject) {
54-
if (schemaObject.type === "string" || (Array.isArray(schemaObject.type) && schemaObject.type.includes("string"))) items = items.map((t) => escStr(t || "")); // empty/missing values are empty strings
54+
if (schemaObject.type === "string" || (Array.isArray(schemaObject.type) && schemaObject.type.includes("string"))) items = items.map((t) => escStr(t));
5555
}
5656
// if no type, assume "string"
5757
else {
5858
items = items.map((t) => escStr(t || ""));
5959
}
60-
return tsUnionOf(...items, ...(schemaObject.nullable || ("type" in schemaObject && Array.isArray(schemaObject.type) && schemaObject.type.includes("null")) ? ["null"] : []));
60+
return tsUnionOf(...items, ...(schemaObject.nullable ? ["null"] : []));
6161
}
6262

6363
// oneOf (no discriminator)

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

+13-3
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,19 @@ describe("Schema Object", () => {
278278
expect(generated).toBe("OneOf<[string, boolean, number, null]>");
279279
});
280280

281-
test("enum + polymorphism + nullable", () => {
282-
const generated = transformSchemaObject({ type: ["string", "null"], enum: ["", "false positive", "won't fix", "used in tests"] }, options);
283-
expect(generated).toBe(`"" | "false positive" | "won't fix" | "used in tests" | null`);
281+
test("enum + polymorphism + nullable 1", () => {
282+
const generated = transformSchemaObject({ type: ["string", "null"], enum: ["blue", "green", "yellow"] }, options);
283+
expect(generated).toBe(`"blue" | "green" | "yellow"`);
284+
});
285+
286+
test("enum + polymorphism + nullable 2", () => {
287+
const generated = transformSchemaObject({ type: ["string", "null"], enum: ["", "blue", "green", "yellow"] }, options);
288+
expect(generated).toBe(`"" | "blue" | "green" | "yellow"`);
289+
});
290+
291+
test("enum + polymorphism + nullable 3", () => {
292+
const generated = transformSchemaObject({ type: ["string", "null"], enum: [null, "blue", "green", "yellow"] }, options);
293+
expect(generated).toBe(`null | "blue" | "green" | "yellow"`);
284294
});
285295
});
286296
});

0 commit comments

Comments
 (0)