Skip to content

fix: empty strings in enums when type [string, null] #1200

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
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
5 changes: 5 additions & 0 deletions .changeset/shy-turtles-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-typescript": patch
---

Remove unexpected empty string in generated nullable polymophic enum types (["string", "null"])
220 changes: 110 additions & 110 deletions packages/openapi-typescript/examples/github-api-next.ts

Large diffs are not rendered by default.

164 changes: 82 additions & 82 deletions packages/openapi-typescript/examples/github-api.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3174,7 +3174,7 @@ export interface components {
* @description **Required when the `state` is `resolved`.** The reason for resolving the alert.
* @enum {string|null}
*/
"secret-scanning-alert-resolution": "" | "false_positive" | "wont_fix" | "revoked" | "used_in_tests" | null;
"secret-scanning-alert-resolution": null | "false_positive" | "wont_fix" | "revoked" | "used_in_tests";
/**
* Minimal Repository
* @description Minimal Repository
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-typescript/src/transform/schema-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ export function defaultSchemaObjectTransform(schemaObject: SchemaObject | Refere
if (schemaObject.enum) {
let items = schemaObject.enum as any[];
if ("type" in schemaObject) {
if (schemaObject.type === "string" || (Array.isArray(schemaObject.type) && schemaObject.type.includes("string"))) items = items.map((t) => escStr(t || "")); // empty/missing values are empty strings
if (schemaObject.type === "string" || (Array.isArray(schemaObject.type) && schemaObject.type.includes("string"))) items = items.map((t) => escStr(t));
}
// if no type, assume "string"
else {
items = items.map((t) => escStr(t || ""));
}
return tsUnionOf(...items, ...(schemaObject.nullable || ("type" in schemaObject && Array.isArray(schemaObject.type) && schemaObject.type.includes("null")) ? ["null"] : []));
return tsUnionOf(...items, ...(schemaObject.nullable ? ["null"] : []));
}

// oneOf (no discriminator)
Expand Down
16 changes: 13 additions & 3 deletions packages/openapi-typescript/test/schema-object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,19 @@ describe("Schema Object", () => {
expect(generated).toBe("OneOf<[string, boolean, number, null]>");
});

test("enum + polymorphism + nullable", () => {
const generated = transformSchemaObject({ type: ["string", "null"], enum: ["", "false positive", "won't fix", "used in tests"] }, options);
expect(generated).toBe(`"" | "false positive" | "won't fix" | "used in tests" | null`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah thanks for changing these. I don’t think the old strings were meaningful / helpful.

test("enum + polymorphism + nullable 1", () => {
const generated = transformSchemaObject({ type: ["string", "null"], enum: ["blue", "green", "yellow"] }, options);
expect(generated).toBe(`"blue" | "green" | "yellow"`);
});

test("enum + polymorphism + nullable 2", () => {
const generated = transformSchemaObject({ type: ["string", "null"], enum: ["", "blue", "green", "yellow"] }, options);
expect(generated).toBe(`"" | "blue" | "green" | "yellow"`);
});

test("enum + polymorphism + nullable 3", () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome tests

const generated = transformSchemaObject({ type: ["string", "null"], enum: [null, "blue", "green", "yellow"] }, options);
expect(generated).toBe(`null | "blue" | "green" | "yellow"`);
});
});
});
Expand Down