openapi-python-client icon indicating copy to clipboard operation
openapi-python-client copied to clipboard

Support top level `oneOf` and `anyOf`

Open forest-benchling opened this issue 4 years ago • 5 comments

Describe the bug If a top-level schema is defined using oneOf or anyOf, the generated model has no properties.

To Reproduce See this PR which adds a test case for the bug: https://github.com/triaxtec/openapi-python-client/pull/387

Expected behavior It should work the same way as it does for model properties, i.e., constructing a union type.

OpenAPI Spec File https://github.com/triaxtec/openapi-python-client/pull/387

Desktop (please complete the following information):

  • OS: [e.g. macOS 10.15.1]
  • Python Version: [e.g. 3.8.0]
  • openapi-python-client version [e.g. 0.1.0]

forest-benchling avatar Apr 14 '21 16:04 forest-benchling

cc @packyg @bowenwr @dtkav @adamrp

forest-benchling avatar Apr 14 '21 16:04 forest-benchling

I guess on second thought here, it's unclear how we'd actually represent a model which is nothing other than a union of other models. Maybe the desired outcome of this ticket is simply to raise an error during code generation?

forest-benchling avatar Apr 19 '21 13:04 forest-benchling

For oneOf we should probably make a type alias to Union[ThingOne, ThingTwo]. anyOf is a lot harder... my read of it is that it has to validate against at least one schema but can contain properties of other schemas. So we might have to generate several models, each of which has the requirements of one schema and optional fields from all of the others. Sounds messy.

I'd love to know how folks are actually using anyOf to get a better feel for how the implementation should shake out.

dbanty avatar Dec 18 '21 20:12 dbanty

Openapi schemas can combine oneOf anyOf allOf and properties all in a single schema. So I don't think that For oneOf we should probably make a type alias to Union[ThingOne, ThingTwo] would work for all cases.

spacether avatar Dec 28 '21 23:12 spacether

What is the status for this issue? Because I am in a situation there I need it and would love to not create workarounds for it 😉

dvaerum avatar Jun 06 '22 13:06 dvaerum