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

Conversation

toomuchdesign
Copy link
Contributor

@toomuchdesign toomuchdesign commented Jul 5, 2023

Changes

Related to: #1197

JSON schemas like:

{
  "properties": {
    "petType": {
      "type": [
        "null",
        "string"
      ],
      "enum": [
        "foo", "bar"
      ]
    }
  }
}

..results into:

{
  petType: "foo" | "bar" | ""
}

...which seems to add an unexpected empty string to the union. See the example in this JSON schema validator.

With this PR the empty "" is removed from the union:

{
  petType: "foo" | "bar"
}

Happy to review the PR based on your suggestions!

null not listed as enum

In case type contains null but then null is not listed as enum:

{
  "properties": {
    "petType": {
      "type": [
        "null",
        "string"
      ],
      "enum": [
        "foo", "bar"
      ]
    }
  }
}

..I understand the result should not contain null. See this online validator test case.

How to Review

Take a look at the new test cases added. The fix didn't introduce any regressive test error.

Checklist

  • Unit tests updated
  • README updated
  • examples/ directory updated (only applicable for openapi-typescript)

@changeset-bot
Copy link

changeset-bot bot commented Jul 5, 2023

🦋 Changeset detected

Latest commit: 3505067

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
openapi-typescript Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@@ -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)); // empty/missing values are empty strings
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 Nice cleanup here. I think this was a byproduct of being written before tsUnionOf() deduplicated things.

expect(generated).toBe(`"" | "false positive" | "won't fix" | "used in tests"`);
});

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

@drwpow
Copy link
Contributor

drwpow commented Jul 6, 2023

Could you add a changeset so the changelog is updated (details in comment)? 🙏

@toomuchdesign toomuchdesign force-pushed the fix/polimorphism-empty-strings branch from cb44f01 to 1fd4b0b Compare July 7, 2023 09:13
@toomuchdesign
Copy link
Contributor Author

Thank you @drwpow!
Cleaned up my PR, rebased and and added the relevant changelog.

Cheers!

@@ -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.

@drwpow
Copy link
Contributor

drwpow commented Jul 7, 2023

The test failures may be from the snapshots failing. In the packages/openapi-typescript directory, try running pnpm run build && pnpm run update:examples to regenerate those.

The diff that shows is also helpful to see what it does on large real-world schemas, and they’re helpful in addition to the ever-essential unit tests.

@toomuchdesign
Copy link
Contributor Author

All updated 👍 I've seen a few "" being removed :)

@drwpow
Copy link
Contributor

drwpow commented Jul 7, 2023

All updated 👍 I've seen a few "" being removed :)

Yup those look like good diffs to me 🙂

@drwpow drwpow merged commit 4fc8c20 into openapi-ts:main Jul 7, 2023
@github-actions github-actions bot mentioned this pull request Jul 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants