pandera
pandera copied to clipboard
Description of field of schema model does not make it into FastAPI openapi.json
When defining a schema model as follows I would love to see the description occur in the FastAPI openapi.json / Swagger UI:
class InputSchema(pa.SchemaModel):
year: Series[int] = pa.Field(description="the year pandera was released")
Is this already possible by any means?
hi @BigNerd ! we'll need to update this function so that the json schema representation contains the description and other metadata like title, etc. https://github.com/unionai-oss/pandera/blob/master/pandera/json_schema.py
Would you be open to making a contribution on this front?
Hi @cosmicBboy yes, I think I can give it a try during the next weeks or so. Thanks for pointing me to the piece of code that needs to be worked on for this.
Here is the change to to_json_schema() that I want to contribute when I have time to go through the fork, branch, pull request process.
def to_json_schema(dataframe_schema):
"""Serialize schema metadata into json-schema format.
:param dataframe_schema: schema to write to json-schema format.
.. note::
This function currently does not fully specify a pandera schema,
and is primarily used internally to render OpenAPI docs via the
FastAPI integration.
"""
empty = pd.DataFrame(columns=dataframe_schema.columns.keys()).astype(
{k: v.type for k, v in dataframe_schema.dtypes.items()}
)
table_schema = pd.io.json.build_table_schema(empty)
def _field_json_schema(field):
annotations = {}
name = field["name"]
columns = dataframe_schema.columns
if name in columns and columns[name].title is not None:
annotations["title"] = columns[name].title
if name in columns and columns[name].description is not None:
annotations["description"] = columns[name].description
return {
"type": "array",
"items": {
"type": field["type"],
**annotations,
},
}
return {
"title": dataframe_schema.name or "pandera.DataFrameSchema",
"type": "object",
"properties": {
field["name"]: _field_json_schema(field)
for field in table_schema["fields"]
},
}
cool! I can review if you can make a PR ⭐️