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
Describe the bug
A pydantic schema (extending BaseModel) is automatically picked up by PydanticExtension, which serializes it with a name that contains square brackets, e.g. WrappedResponse[MessageResponse]. This name is invalid for OpenAPI schemas.
To Reproduce
Here's the base code to play around with.
Expected behavior
The PydanticExtension class should be able to generate names that are safe for OpenAPI schemas, e.g. by replacing [ and ] with _.
Alternatively, I should be able to provide my own OpenApiSerializerExtension that picks up the generic type and has a higher priority than the built-in extension. Unfortunately, because class names are directly compared in the registry (including type parameters), we cannot register one extension that picks up all types.
Another approach we could take would be to override _get_serializer_name in our own custom AutoSchema (get_serializer_name won't work because it's never called for Pydantic models). Because this is a private method, it doesn't feel correct to override it.
The approach that we ended up going with, until this is fixed, is to override the serializer name inside of e.g. WrappedResponse:
classWrappedResponse(OutputSchema, Generic[U]):
data: U@classmethoddefmodel_parametrized_name(cls, params: tuple[type[Any], ...]) ->str:
""" Ensures that types like WrappedResponse[MySchema] get names that are valid OpenAPI schema names (e.g. WrappedResponse_MySchema). """param_names='_'.join([param.__name__forparaminparams])
returnf"{cls.__name__}_{param_names}"@staticmethoddefwith(data: T) ->WrappedResponse[T]:
returnWrappedResponse(data=data)
I'm not sure if model_parametrized_name is used elsewhere, so I'm not 100% comfortable with this solution. What do you think is the best way to solve this?
The text was updated successfully, but these errors were encountered:
Describe the bug
A pydantic schema (extending
BaseModel
) is automatically picked up byPydanticExtension
, which serializes it with a name that contains square brackets, e.g.WrappedResponse[MessageResponse]
. This name is invalid for OpenAPI schemas.To Reproduce
Here's the base code to play around with.
Expected behavior
The
PydanticExtension
class should be able to generate names that are safe for OpenAPI schemas, e.g. by replacing[
and]
with_
.Alternatively, I should be able to provide my own
OpenApiSerializerExtension
that picks up the generic type and has a higherpriority
than the built-in extension. Unfortunately, because class names are directly compared in the registry (including type parameters), we cannot register one extension that picks up all types.Another approach we could take would be to override
_get_serializer_name
in our own customAutoSchema
(get_serializer_name
won't work because it's never called for Pydantic models). Because this is a private method, it doesn't feel correct to override it.The approach that we ended up going with, until this is fixed, is to override the serializer name inside of e.g.
WrappedResponse
:I'm not sure if
model_parametrized_name
is used elsewhere, so I'm not 100% comfortable with this solution. What do you think is the best way to solve this?The text was updated successfully, but these errors were encountered: