Skip to content

Enum values with trailing white space are trimmed #1224

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

Closed
2 tasks done
pimveldhuisen opened this issue Jul 13, 2023 · 4 comments · Fixed by #1269
Closed
2 tasks done

Enum values with trailing white space are trimmed #1224

pimveldhuisen opened this issue Jul 13, 2023 · 4 comments · Fixed by #1269
Labels
bug Something isn't working good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project openapi-ts Relevant to the openapi-typescript library PRs welcome PRs are welcome to solve this issue!

Comments

@pimveldhuisen
Copy link
Contributor

Description

In OpenAPI, it is possible to define an enum type, with different string constants as its values. These string constants might contain trailing white space, or in the extreme case consist of only white space.

Example OpenAPI YAML:

FooBarCode:
  type: string
  description: "Character code to indicate the FooBar attitude"
  enum:
    - " "
    - "1"
    - "2"
    - "X"

Currently, when openapi-typescript generates types for such enums, any trailing whitespace is trimmed.

Example resulting Typescript Type with current bug:

FooBarCode: "" | "1" | "2" | "X"

Expected behaviour for resulting Typescript Type:

FooBarCode: " " | "1" | "2" | "X"

Currently, the generated type does not match the OpenAPI spec, and as a result type checking cannot be done, and in fact generates false positive errors.

Reproduction

This occurs for any enum with string values containing trailing white space.
It seems that the trimming occurs here in the code.

Resolution

If the maintainers agree that the current behaviour should be considered as a bug, I'm happy to open an MR with unit tests for this scenario and implement a fix.

Checklist

@pimveldhuisen pimveldhuisen added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels Jul 13, 2023
@drwpow drwpow added PRs welcome PRs are welcome to solve this issue! good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project labels Jul 25, 2023
@drwpow
Copy link
Contributor

drwpow commented Jul 31, 2023

This is a definite bug. This is also a great first issue with a straightforward problem that’s easy to write tests for.

I believe this could be captured in a schema-object.test.ts test easily, and the problem fixed within src/transform/schema-object.ts. Would happily welcome a PR on this 🙏

@mitsuyuki418
Copy link

Is this the same place? When I convert "enum": [0, 1, 2], the result is '' | 1 | 2 but I want 0 | 1 | 2
Would it be escStr(t ?? "") rather than escStr(t || "")?

@pimveldhuisen
Copy link
Contributor Author

I opened a PR: #1269

Happy to iterate a bit if you would like to see it done differently.

I did not include any changes in the PR related to @mitsuyuki418 's comment around the 0 value, since I could not easily reproduce it. Maybe that problem is best tackled in a separate issue?

@mitsuyuki418
Copy link

sure thanks @pimveldhuisen I'll open another one for that issue :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project openapi-ts Relevant to the openapi-typescript library PRs welcome PRs are welcome to solve this issue!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants