Currently I have a pydantic models that handle 3 scenarios as documented below, For FastAPI response model I use a union of these 3 models so that the corresponding model can be used to validate the response fields.
class SuccessResponse(BaseModel):
status: Literal['SUCCESS']
data: Dict // varies
class FailureResponse(BaseModel):
status: Literal['FAILED']
data: Dict // varies
class ErrorResponse(BaseModel):
status: Literal['ERROR']
data: Dict //varies
class ResponseModel(BaseModel):
__root__ = Union[
SuccessResponse,
FailureResponse,
ErrorResponse
]
in FastAPI I use the response model as the model must be able to validate the response of a method against all three models and return a positive response. But inside the FastAPI route how do I know which model is being returned so that I can parse corresponding data using SuccessResponse.parse_obj(response)
or the other models in Union. Is there a way to find this out??