Skip to content

feat: pydantic discriminator for unions#2316

Merged
jonaslagoni merged 8 commits intoasyncapi:masterfrom
412b:pydantic-discriminator-for-unions
Aug 27, 2025
Merged

feat: pydantic discriminator for unions#2316
jonaslagoni merged 8 commits intoasyncapi:masterfrom
412b:pydantic-discriminator-for-unions

Conversation

@412b
Copy link
Contributor

@412b 412b commented Jul 27, 2025

Description

it is a 2-fold solution:

  1. it requires changes in the "container" field (so any field having A | B, where A and B are distinguished by discriminator). Union or | must be wrapped in Annotated with Field(discriminator=...) provided.
  2. discriminator field in the object must be Literal.

additionally there's a first commit in the PR, which aligns on Python pre 3.10 support (using Union instead of |). it can be dropped, if doesn't look like a good fit here.

I had to introduce a standalone PythonTypeMapping for 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-cli to use modelina files generated in the branch. Perhaps, if there's a better way to couple PYTHON_PYDANTIC_PRESET with PYTHON_PYDANTIC_TYPE_MAPPING, that export can be omitted and compatibility of the CLI stays the same.

Related Issue

Resolves #2315

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

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?

@netlify
Copy link

netlify bot commented Jul 27, 2025

Deploy Preview for modelina canceled.

Name Link
🔨 Latest commit 789b45b
🔍 Latest deploy log https://app.netlify.com/projects/modelina/deploys/68af585ed6ccff00086c2660

@412b 412b changed the title feat: Pydantic discriminator for unions feat: pydantic discriminator for unions Jul 27, 2025
@412b
Copy link
Contributor Author

412b commented Jul 27, 2025

@jonaslagoni I would like to have your input on possible more elegant and backwards compatible way of providing preset-defined type mapping to generator.

@412b
Copy link
Contributor Author

412b commented Aug 14, 2025

@jonaslagoni I refactored it to a bit less clean, but backwards and cli compatible change.

@coveralls
Copy link

coveralls commented Aug 14, 2025

Pull Request Test Coverage Report for Build 17276333952

Details

  • 24 of 24 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.02%) to 92.619%

Totals Coverage Status
Change from base Build 17275663053: 0.02%
Covered Lines: 6669
Relevant Lines: 7064

💛 - Coveralls

@412b 412b force-pushed the pydantic-discriminator-for-unions branch 3 times, most recently from 662fb2f to b794158 Compare August 16, 2025 20:20
@412b 412b force-pushed the pydantic-discriminator-for-unions branch from 32eba24 to 3e3ffd2 Compare August 27, 2025 13:55
@412b
Copy link
Contributor Author

412b commented Aug 27, 2025

@jonaslagoni perhaps, you have some time to check this one out 🙏

@sonarqubecloud
Copy link

@jonaslagoni jonaslagoni merged commit 2c6220c into asyncapi:master Aug 27, 2025
16 checks passed
@asyncapi-bot
Copy link
Contributor

🎉 This PR is included in version 5.6.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@412b 412b deleted the pydantic-discriminator-for-unions branch August 29, 2025 10:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] proper support of disciriminated unions in Pydantic

4 participants