You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Optional enum fields are generated with the wrong type #311
Describe the bug
An endpoint which takes a body which has enum fields is being incorrectly generated with the type Union[Unset, None]. Of not here is that the enum itself is correctly generated in its own file, but are not used (or even imported) in the model for the wrapping type.
To Reproduce
This is a minimal specification I came up with, which reproduces the problem:
We generate the client using simply openapi-python-client generate and the specification below, with no additional configuration.
Expected behavior
We only just upgraded to the version which uses Unset, so I am not quite sure what exactly the type should be, but I assume something along the lines of either Optional[MyEnum], Union[MyEnum, Unset] or Union[Optional[MyEnum], Unset]
This also lead me to notice that this problem is not limited to enums, but any nested models where the $ref is inside an allOf. The first version is what FastAPI/Pydantic generates, and as far as I can tell it is a perfectly valid specification.
Although it's not directly related to this library, I noticed this repo is tagged with "FastAPI", so I will also post the workaround I came up with until this is fixed. I added the following to the Config of my Pydantic models and described by their documentation:
@staticmethoddefschema_extra(schema: Dict[str, Any]) ->None:
forfield_name, field_valuesinschema.get("properties", {}).items():
if"allOf"notinfield_values:
continue# $ref fiels can't have siblings, so only keeping the $refschema["properties"][field_name] = {"$ref": field_values["allOf"][0]["$ref"]}
This works for us because we don't have very complex nesting, but assuming "allOf" only has a single field could probably cause issues for more complex cases.
@frjonsen thanks for following up! If the problem is related to allOf, it's actually because there's no support for it at all yet in this library. There's an open issue for this (#98) and a PR which will add support whenever I get around to reviewing it 😅(#312).
Activity
frjonsen commentedon Jan 25, 2021
After further testing, I've realized what makes this break is the
allOfinThis works fine, however
This also lead me to notice that this problem is not limited to enums, but any nested models where the
$refis inside anallOf. The first version is what FastAPI/Pydantic generates, and as far as I can tell it is a perfectly valid specification.Although it's not directly related to this library, I noticed this repo is tagged with "FastAPI", so I will also post the workaround I came up with until this is fixed. I added the following to the Config of my Pydantic models and described by their documentation:
This works for us because we don't have very complex nesting, but assuming "allOf" only has a single field could probably cause issues for more complex cases.
dbanty commentedon Jan 25, 2021
@frjonsen thanks for following up! If the problem is related to allOf, it's actually because there's no support for it at all yet in this library. There's an open issue for this (#98) and a PR which will add support whenever I get around to reviewing it 😅(#312).
frjonsen commentedon Jan 26, 2021
It does indeed seem to be related to #98. Excellent to hear it's already being worked on. I will close this as a duplicate then.