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
When only specific values are allowed for a property in OpenAPI, they are listed as an enum, which is translated to an Enum class in the generated Python model. Unfortunately, when the model is converted to a dictionary using the Pydantic built-in model.dict() method, the dict field value is an Enum class instance, not the primitive value (e.g., string). It would work as intended if the enums would be encoded as typing.Literal instead.
OpenAPI:
AccessModel:
type: objectproperties:
access:
type: stringdescription: "Only 'readonly' or 'readwrite' is allowed"enum:
- readonly
- readwrite
Current generated model code:
classAccess(Enum):
readonly='readonly'readwrite='readwrite'classAccessModel(BaseModel):
access: Access=Field(
...,
description='Only 'readonly' or 'readwrite' is allowed',
)
Converting a model instance to a dictionary with access_model.dict() yields {'access': <Access.readonly: 'readonly'>}.
I'm proposing using typing.Literal to encode OpenAPI enums the following way:
fromtypingimportLiteralclassAccessModel(BaseModel):
access: Literal['readonly', 'readwrite'] =Field(
...,
description='Only 'readonly' or 'readwrite' is allowed',
)
Converting an instance of this yields a dictionary with the correct primitive type: {'access': 'readonly'}
The text was updated successfully, but these errors were encountered:
When only specific values are allowed for a property in OpenAPI, they are listed as an
enum
, which is translated to an Enum class in the generated Python model. Unfortunately, when the model is converted to a dictionary using the Pydantic built-inmodel.dict()
method, the dict field value is an Enum class instance, not the primitive value (e.g., string). It would work as intended if the enums would be encoded astyping.Literal
instead.OpenAPI:
Current generated model code:
Converting a model instance to a dictionary with
access_model.dict()
yields{'access': <Access.readonly: 'readonly'>}
.I'm proposing using
typing.Literal
to encode OpenAPI enums the following way:Converting an instance of this yields a dictionary with the correct primitive type:
{'access': 'readonly'}
The text was updated successfully, but these errors were encountered: