Custom response breaks for Starlite >= 1.48.0 #1072
-
We have a custom response class to handle serialization of Mongo IDs: class BeanieResponse(Response):
def serializer(self, value: Any) -> Dict[str, Any]:
if isinstance(value, PydanticObjectId):
return str(value)
if isinstance(value, Link):
return value.to_dict().get("id")
if isinstance(value, Int64):
return int(value)
return super().serializer(value) On upgrading Starlite to 1.48.0 or greater, I get the following error:
I see this info in the method |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
However, there are easier methods to solve this! Take a look at the custom responses examples in the docs. You can add a from starlite import Starlite
# add your type imports here
beanie_encoders = {
PydanticObjectId: str,
Link: lambda link: link.to_dict().get("id"),
Int64: int,
}
app = Starlite(..., type_encoders=beanie_encoders) |
Beta Was this translation helpful? Give feedback.
Response.serializer
is a classmethod, so you need to add the@classmethod
decorator.However, there are easier methods to solve this! Take a look at the custom responses examples in the docs. You can add a
type_encoders
dictionary on any layer of the application, so a minimal implementation would look something like this: