Skip to content
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

Merged

Conversation

ahem
Copy link
Contributor

@ahem ahem commented Apr 23, 2024

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

  • The code follows the project's coding standards and is properly linted (npm run lint).
  • Tests have been added or updated to cover the changes.
  • Documentation has been updated to reflect the changes.
  • All tests pass successfully locally.(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:

{
   "requiredField": "[email protected]",
   "noDescription": "test",
   "options": 123,
   "content-type": "application/json"
}

Without this change Pydantic would ignore the "content-type" field since it would not know to map that to the models contentType 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.

Copy link

netlify bot commented Apr 23, 2024

Deploy Preview for modelina canceled.

Name Link
🔨 Latest commit 44c4ae5
🔍 Latest deploy log https://app.netlify.com/sites/modelina/deploys/66277ac58d2cd400083ff9c6

Copy link
Contributor

@github-actions github-actions bot left a 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.

@ahem ahem changed the title pydantic generator: support dash in property names fix: support dash in property names (Pydantic generator) Apr 23, 2024
@ahem ahem marked this pull request as ready for review April 23, 2024 09:03
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".
@ahem ahem force-pushed the pydantic-support-dash-in-property-names branch from 9610ee2 to 44c4ae5 Compare April 23, 2024 09:09
Copy link

sonarcloud bot commented Apr 23, 2024

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@coveralls
Copy link

Pull Request Test Coverage Report for Build 8797818240

Details

  • 7 of 8 (87.5%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.003%) to 92.3%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/generators/python/presets/Pydantic.ts 7 8 87.5%
Totals Coverage Status
Change from base Build 8737163308: -0.003%
Covered Lines: 5995
Relevant Lines: 6326

💛 - Coveralls

Copy link
Member

@jonaslagoni jonaslagoni left a comment

Choose a reason for hiding this comment

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

Awesome, thanks @ahem 🙏

@jonaslagoni
Copy link
Member

/rtm

@asyncapi-bot asyncapi-bot merged commit 025c267 into asyncapi:master Apr 23, 2024
17 checks passed
@jonaslagoni
Copy link
Member

@all-contributors please add @ahem for code, test, bug

Copy link
Contributor

@jonaslagoni

I've put up a pull request to add @ahem! 🎉

@asyncapi-bot
Copy link
Contributor

🎉 This PR is included in version 3.4.9 🎉

The release is available on:

Your semantic-release bot 📦🚀

@ahem ahem deleted the pydantic-support-dash-in-property-names branch April 23, 2024 13:05
@asyncapi-bot
Copy link
Contributor

🎉 This PR is included in version 4.0.0-next.36 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Python Pydantic improvements
4 participants