-
-
Notifications
You must be signed in to change notification settings - Fork 183
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
fix: support dash in property names (Pydantic generator) #1955
fix: support dash in property names (Pydantic generator) #1955
Conversation
✅ Deploy Preview for modelina canceled.
|
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.
Welcome to AsyncAPI. Thanks a lot for creating your first pull request. Please check out our contributors guide useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.
This enables the generated models for Pydantic to work for properties with dashes in the name. The generated code now puts descriptions in the "description" argument of the Pydantic Field decorator, and puts the original, unconstrained, property name in the "alias" field. This is correct usage as per the [Pydantic documentation](https://docs.pydantic.dev/latest/concepts/fields/#field-aliases) Also, dashes are allowed through in property key constrainer, so that a property name like "content-type" is converted into "contentType" instead of "contentMinusType".
9610ee2
to
44c4ae5
Compare
Quality Gate passedIssues Measures |
Pull Request Test Coverage Report for Build 8797818240Details
💛 - Coveralls |
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.
Awesome, thanks @ahem 🙏
/rtm |
@all-contributors please add @ahem for code, test, bug |
I've put up a pull request to add @ahem! 🎉 |
🎉 This PR is included in version 3.4.9 🎉 The release is available on: Your semantic-release bot 📦🚀 |
🎉 This PR is included in version 4.0.0-next.36 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Description
This enables the generated models for Pydantic to work for properties with dashes in the name.
The generated code now puts descriptions in the "description" argument of the Pydantic Field decorator, and puts the original, unconstrained, property name in the "alias" field. This is correct usage as per the Pydantic documentation
Also, dashes are allowed through in property key constrainer, so that a property name like "content-type" is converted into "contentType" instead of "contentMinusType".
Related Issue
I believe this fixes #1058 but apparently that has been closed with a fix to an mostly unrelated problem.
Checklist
npm run lint
).npm run test
).Additional Notes
With these changes the generated Pydantic model class can correctly deserialise and serialise the JSON object described by the schema in the updated generate-python-pydantic-models test:
Without this change Pydantic would ignore the
"content-type"
field since it would not know to map that to the modelscontentType
property.Note that this is technically a breaking change, since before users could use the "description" field in the JSON schema to set the "alias" field on the Pydantic model. This would be a misuse of the description field, though, since it is to decorate a user interface with information about the data produced by this user interface.