Skip to content

fix(openapi-typescript): Stringify primitive constant values #1149

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 2 commits into from
Jun 12, 2023

Conversation

duncanbeevers
Copy link
Contributor

Closes #1118

Changes

#1118

The escStr function claims to always return a string, but in fact would return whatever was passed into it, preserving the original type.
This PR aligns the behavior of the function with type signature, and keeps the original behavior mostly the same.
Because these values were being turned into strings (through implicit toString() during backtick string expansion), the final generated output was mostly correct.
Additionally, because there was special handling for const values with a specified type, some string consts were being quoted.

In this PR, I removed the special handling of const types, and instead pass every const value through escStr.
escStr then passes non-string arguments through JSON.stringify.

How to Review

Ensure JSON stringification of primitive types is the correct treatment for OpenAPI const members, even with no type specified.

I added tests for string and number consts with both explicit and implicit type arguments.

  • Where in the OpenAPI spec is const described? I couldn't find it in the 3.1 spec.
  • How should other primitive constant types be treated? (eg, true, false)

Checklist

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

@changeset-bot
Copy link

changeset-bot bot commented May 26, 2023

🦋 Changeset detected

Latest commit: 4901efd

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 Minor

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

@mitchell-merry
Copy link
Contributor

mitchell-merry commented May 28, 2023

Where in the OpenAPI spec is const described? I couldn't find it in the 3.1 spec.

It's in the JSON schema spec: https://json-schema.org/understanding-json-schema/reference/generic.html#constant-values

How should other primitive constant types be treated? (eg, true, false)

I'm pretty sure they should be treated as the boolean values rather than the string "true"/"false". That's how the yaml spec works I believe. https://yaml.org/type/bool.html.

Edit: Actually, I think whatever parses the yaml/json file will (or should) determine this for us, right?

Copy link
Contributor

@drwpow drwpow left a comment

Choose a reason for hiding this comment

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

This looks great to me! I can’t recall the reason why that particular code block in transform/schema-object.ts is so anti-idiomatic. So the fact that there’s no change to tests/output generation, and this PR cleans up that bit of code is probably a good sign overall 🙂

@drwpow drwpow merged commit b82cffb into openapi-ts:main Jun 12, 2023
@github-actions github-actions bot mentioned this pull request Jun 12, 2023
@duncanbeevers duncanbeevers deleted the fix/constant-string-types-1118 branch June 12, 2023 21:42
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.

Constant string types result in invalid TypeScript code
3 participants