-
-
Notifications
You must be signed in to change notification settings - Fork 540
feat: Under-specified objects are Record<string, unknown> #1032
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
feat: Under-specified objects are Record<string, unknown> #1032
Conversation
I've been using the 5.4.0 branch for quite some time to great effect. I published a private package with these changes and am using it happily in the existing app. (although it did reveal some places where I needed to make |
Upvoted as seems also what we need to migrate to v6. |
I’m not sure I agree with this change. Yes 5.x had that behavior, but this change does make the generated types for I guess the question is whether or not I do think that in general, a clearer schema results in clearer generated types. If you wanted to generate But in either case, I do like the flag idea. Rather than changing what 6.x does, we could add a Edit: clarified some thoughts |
I primarily based this change on schemas seen in the wild. For example, on the Stripe API, a notoriously-well-documented schema, all those In my opinion, this proposed behavior better-matches the intent of real-world schema authors. |
The This change only affects the fallback behavior where no schema for an object is specified. If there were an additional command-line flag, I'd imagine it would be something like |
ba416c0
to
8199976
Compare
8199976
to
ef586bd
Compare
@drwpow I've updated this PR to put the functionality behind the I tested the node-js generated pieces, but I didn't see a straightforward way to test CLI flag permutations against the example OpenAPI schema corpus. I did go ahead and regenerate the examples in |
According to the OpenAPI docs: "A free-form object (arbitrary property/value pairs) is defined as: type: object This is equivalent to type: object
additionalProperties: true and type: object
additionalProperties: {} " so I believe I think I disagree that a flag is necessary here. The correct behaviour is |
security: | ||
- bearer_auth: | ||
- 'read' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Thanks for updating this example!
@mitchell-merry ah thank you for providing that detail! Yes it does seem to be explicit in the Swagger 3.0 documentation. It’s much more ambiguous in the 3.1 specification.
I’d still like for this to be a flag for now, because it would be a behavior change for Because the discussion over generated types is stemming from ambiguity in an OpenAPI schema, we can could take 1 of 2 approaches: either err toward being too strict (you can’t access any properties that aren’t defined until you update your OpenAPI schema) or too lax (you can freely access any properties not in your OpenAPI schema by just asserting types). Just speaking personally, I‘d rather err on the side of being more strict, and requiring the OpenAPI schema to accurately describe all shapes rather than TypeScript. You’ll always get better mileage out of that approach, if you ask me! |
Changes
This changes the fallback behavior when generating types for
object
schemas without specified properties to beRecord<string, unknown>
instead ofRecord<string, never>
This change likely constitutes a major version bump, but more-closely resembles the v5 behavior.
We could hide it behind a CLI flag for backwards compatibility.
Closes #1031
How to Review
The example snapshots are checked as part of the tests. Reviewing the changes to these snapshots is the best way to get a sense of the scope of the changes.
For many of the changes I also went back to the source OpenAPI yaml definitions to verify that
Record<string, unknown>
accurately represents the types returned from the actual API implementations.Checklist
examples/
directory updated (if applicable)