feat: pydantic discriminator for unions#2316
Merged
jonaslagoni merged 8 commits intoasyncapi:masterfrom Aug 27, 2025
Merged
Conversation
✅ Deploy Preview for modelina canceled.
|
Contributor
Author
|
@jonaslagoni I would like to have your input on possible more elegant and backwards compatible way of providing preset-defined type mapping to generator. |
Contributor
Author
|
@jonaslagoni I refactored it to a bit less clean, but backwards and cli compatible change. |
Pull Request Test Coverage Report for Build 17276333952Details
💛 - Coveralls |
662fb2f to
b794158
Compare
32eba24 to
3e3ffd2
Compare
Contributor
Author
|
@jonaslagoni perhaps, you have some time to check this one out 🙏 |
|
jonaslagoni
approved these changes
Aug 27, 2025
Contributor
|
🎉 This PR is included in version 5.6.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Description
it is a 2-fold solution:
A | B, whereAandBare distinguished by discriminator).Unionor|must be wrapped inAnnotatedwithField(discriminator=...)provided.Literal.additionally there's a first commit in the PR, which aligns on Python pre 3.10 support (using
Unioninstead of|). it can be dropped, if doesn't look like a good fit here.I had to introduce a standalone
PythonTypeMappingfor Pydantic (PydanticConstrainer), because solving the problem with presets looked very dirty and improper. I haven't tried to do more deep changes in order to not blow the scope here. If there's a better ideas/ways of serving that functionality, I can adjust.Introducing new type mapping led to it becoming exportable for the CLI and that makes CLI depend on particular not-less-than version of library, which made testing challenging and I was able to test it by tricking
modelina-clito usemodelinafiles generated in the branch. Perhaps, if there's a better way to couplePYTHON_PYDANTIC_PRESETwithPYTHON_PYDANTIC_TYPE_MAPPING, that export can be omitted and compatibility of the CLI stays the same.Related Issue
Resolves #2315
Checklist
npm run lint).npm run test).Additional Notes
I would love to hear some ideas of how new type mapping can be integrated without explicit export and remain an implementation detail without changing too many objects. e.g. can it be a field in a preset w/o the need to adjust all presets across the code base?