@computed_field not showing in OpenAPI Json #9856
-
First Check
Commit to Help
Example Codefrom fastapi import FastAPI
import pydantic
import uvicorn
class StudentGrades(pydantic.BaseModel):
english: int
history: int
geography: int
@pydantic.computed_field()
@property
def grade(self) -> float:
return (self.english + self.history + self.geography)/ 3
app = FastAPI()
@app.get("/")
def sample() -> StudentGrades:
return StudentGrades(english=100, history=100, geography=70) DescriptionWhen using model with a computed field, if I check the content of the response: {
"english": 100,
"history": 100,
"geography": 70,
"grade": 90.0 // computed field
} The computed field is in the response, as expected. However, the computed field is not being added to the json schema: {
"openapi": "3.1.0",
"info": {
"title": "FastAPI",
"version": "0.1.0"
},
"paths": {
"/": {
"get": {
"summary": "Sample",
"operationId": "sample__get",
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/StudentGrades"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"StudentGrades": {
"properties": {
"english": {
"type": "integer",
"title": "English"
},
"history": {
"type": "integer",
"title": "History"
},
"geography": {
"type": "integer",
"title": "Geography"
}
},
"type": "object",
"required": [
"english",
"history",
"geography"
],
"title": "StudentGrades"
}
}
}
} And therefore, not showing in Swagger UI: Operating SystemWindows, macOS Operating System DetailsNo response FastAPI Version0.100.0 Python VersionPython 3.10.11 Additional ContextI initially thought this was an issue on Pydantic, and i even opend an issue But as explained there, the computed field appears when using |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 11 replies
-
|
Beta Was this translation helpful? Give feedback.
-
@tiangolo is already aware of this. This will shortly be supported. |
Beta Was this translation helpful? Give feedback.
-
In the API route self.response_field = create_response_field(
name=response_name,
type_=self.response_model,
# TODO: This should actually set mode='serialization', just, that changes the schemas
# mode="serialization",
mode="validation",
) Is it possible |
Beta Was this translation helpful? Give feedback.
-
It has been a year, what is the current status? |
Beta Was this translation helpful? Give feedback.
@tiangolo is already aware of this. This will shortly be supported.