pydantic-xml
pydantic-xml copied to clipboard
Support for discriminated unions with callable discriminator
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
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