Skip to content

Stop trimming whitespace other than linebreaks in string values #1269

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 3 commits into from
Aug 3, 2023

Conversation

pimveldhuisen
Copy link
Contributor

Changes

This PR attempts to fix #1224 while minimizing other impact.

How to Review

The unit test gives a clear example of the scenario in which the behaviour is currently not as expected. The code change is required to make the unit test pass.

Checklist

  • Unit tests updated
  • docs/ updated (if necessary) -> Not necessary
  • pnpm run update:examples run (only applicable for openapi-typescript) -> No changes produced?

@changeset-bot
Copy link

changeset-bot bot commented Aug 2, 2023

🦋 Changeset detected

Latest commit: 63fc53a

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

@@ -273,7 +273,7 @@ export function tsUnionOf(...types: (string | number | boolean)[]): string {
/** escape string value */
export function escStr(input: any): string {
if (typeof input !== "string") return JSON.stringify(input);
return `"${input.trim().replace(DOUBLE_QUOTE_RE, '\\"')}"`;
return `"${input.replace(LB_RE, "").replace(DOUBLE_QUOTE_RE, '\\"')}"`;
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 this is a smarter way to handle it

@drwpow drwpow merged commit e735ff2 into openapi-ts:main Aug 3, 2023
@github-actions github-actions bot mentioned this pull request Aug 3, 2023
@drwpow
Copy link
Contributor

drwpow commented Aug 3, 2023

Hm this seems to now be generating this type in the Stripe API example:

- name?: string;
+ name?: string | "";

This is on the right track but I think we’ll need to add a couple more tests before shipping

@drwpow
Copy link
Contributor

drwpow commented Aug 3, 2023

Oh my god this may have actually been a bug that was fixed 🙃

name:
  anyOf:
    - maxLength: 5000
      type: string
    - enum:
        - ''
      type: string

That kinda looks right to me! But will poke through the other changes to see if anything else is unexpected

@drwpow drwpow mentioned this pull request Aug 3, 2023
3 tasks
@pimveldhuisen
Copy link
Contributor Author

Seems to me that the change in the Stripe API you mention is actually the result of a change in the source OpenAPI yaml, not related to the code change in this PR.

string | "" is a bit redundant since the empty string is itself a string, but the TypeScript type here just inherits the redundancy from the OpenAPI spec. You could argue for or against removing this redundancy when generating the TypeScript Types, but it seems unrelated to this PR (although both coincidentally involve empty strings! 😄).

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.

Enum values with trailing white space are trimmed
2 participants