-
-
Notifications
You must be signed in to change notification settings - Fork 531
Generated type of additionalProperties
values should not include undefined
#1070
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
Comments
This removes the `| undefined` addition to additional properties in an object ✅ Closes: openapi-ts#1070
I created a simple PR that would just remove the This could easily be modified to add a CLI argument to control this behaviour. |
Thank you for adding this issue and raising a PR! But I believe the Anecdotally, I just fixed a bug in a production app yesterday because of this exact behavior. A map of This wasn’t even using |
This removes the `| undefined` addition to additional properties in an object ✅ Closes: openapi-ts#1070
Thanks for the explanation. So, just to make sure I'm following your reasoning... If we generate this:
Then If so, I can't fault the logic there. If not, I'd appreciate the clarification, mostly for future readers of this issue :) |
Yes exactly! Another way of looking at it is this library tries to infer in between the lines of your schema. And there’s subtext of the assumption that you may be working with an external API you can’t manage or change. And erring on the side of safety is always better. When writing TypeScript natively, of course, you may choose to type things however you’d like. And it’s very common to do |
To be clear - the example provided of: type Foo = {
[index: string]: string
}
const foo:Foo = {};
const bar = foo.bar; will actually make bar |
Description
When trying to describe a simple dictionary object in Swagger, equivalent to a TS
Record<string,string>
, the generated type is, insteadRecord<string, string | undefined>
See https://swagger.io/docs/specification/data-models/dictionaries/
I dug into the code and noticed the comment on this line:
https://github.com/drwpow/openapi-typescript/blob/main/src/transform/schema-object.ts#L194
I haven't found any kind of spec that outlines clearly how
additionalProperties
should be interpreted with regards to whether a value is required. I concede that this may be a matter of opinion.However, I would contend that supporting unknown additional properties would imply that such properties must have a value; otherwise, why would that property even exist? Properties with undefined values are omitted from JSON anyway, so it's pretty hard to send them over the wire.
Therefore, I think this implementation may be flawed and should be changed.
If not, would it be possible to add a parameter to toggle this behavior to make
additionalProperties
with a defined type include or excludeundefined
?OpenAPI
Generated TS
Expected TS
Checklist
The text was updated successfully, but these errors were encountered: