Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for discriminated Union types #7

Closed
bruno-f-cruz opened this issue Oct 25, 2023 · 2 comments · Fixed by #16 or #18
Closed

Add support for discriminated Union types #7

bruno-f-cruz opened this issue Oct 25, 2023 · 2 comments · Fixed by #16 or #18

Comments

@bruno-f-cruz
Copy link
Contributor

Union present a particular challenge during deserialization as the type can only be inferred at runtime. Moreover, Json does not include a spec for the type of the object being deserialized and must often be inferred.

The suggestion is to leverage discriminated unions and attempt to deserialize in runtime the objects. In order to integrate this strategy seamlessly with bonsai, an operator must be added that filters/convert each object in the collection to a specific given type thus allowing for polymorphic deserialization but maintaining the sequence data type.

@glopesdev
Copy link
Member

@bruno-f-cruz we should confirm whether pydantic discriminated unions generate OpenAPI 3 compatible schemas. It looks like they should according to this PR: pydantic/pydantic#619

This is important because OpenAPI 3 discriminators are what is supported by NJsonSchema according to their wiki: https://github.com/RicoSuter/NJsonSchema/wiki/Inheritance

Assuming this is the case, adding support for discriminators might still require a bit of work, since we ultimately need to support both JSON and YAML serialization. It looks like in both cases this will require generating additional helper code to help marshal types around:

@glopesdev
Copy link
Member

After further analysis there now seems to be an easier (in terms of infrastructure) option for YamlDotNet polymorphic deserialization: aaubry/YamlDotNet#774

As for Newtonsoft.Json the best option for leveraging infrastructure would be to leverage JsonSubTypes. This would require consumers to take in an extra dependency, but it seems to be the default choice as reusable infrastructure helper to implement discriminator based deserialization (40M+ downloads and long maintenance history).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants