pydantic-xml icon indicating copy to clipboard operation
pydantic-xml copied to clipboard

Support for discriminated unions with callable discriminator

Open joshvote opened this issue 1 year ago • 1 comments

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 avatar Jan 04 '24 03:01 joshvote

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

joshvote avatar Jan 04 '24 04:01 joshvote