dapper91 / pydantic-xml

python xml for humans
https://pydantic-xml.readthedocs.io
The Unlicense
141 stars 14 forks source link

Support for discriminated unions with callable discriminator #157

Open joshvote opened 6 months ago

joshvote commented 6 months ago

The latest pydantic release has added support for Union discriminators that can be implemented via a custom function: https://docs.pydantic.dev/latest/concepts/unions/#discriminated-unions-with-callable-discriminator

I recently attempted to utilise this with pydantic-xml but ran into the following error raised by pydantic-xml:

E pydantic_xml.errors.ModelFieldError: .... field type error: only string discriminators are supported

I'd be willing to contribute a PR if you're able to point me in the right direction of how to add support for this to pydantic-xml

joshvote commented 6 months ago

I had a quick poke around in the code and I'm not familiar enough the internals of pydantic to make an obvious patch (the current tagged_union.py logic assumes that the fields will all be available as literals when the model is created. However I did end up whipping up a really simple unit test that hopefully highlights the issue.

https://github.com/dapper91/pydantic-xml/compare/master...joshvote:pydantic-xml:master