Replies: 1 comment 2 replies
-
Hello, if you want to hide models from the schema documentation, you might need to manually create a custom OpenAPI schema to hide them, just like the example below: from pydantic import BaseModel
from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi
class _X(BaseModel):
x: int
class _Y(BaseModel):
y: int
x_model: _X
class Z(BaseModel):
z: int
y_model: _Y
app = FastAPI()
@app.get("/")
def read_root(z_model: Z) -> int:
return 0
def custom_openapi():
if app.openapi_schema:
return app.openapi_schema
openapi_schema = get_openapi(
title="My API",
version="1",
routes=app.routes,
)
# Remove auxiliary models from the schema
openapi_schema["components"]["schemas"].pop("_X", None)
openapi_schema["components"]["schemas"].pop("_Y", None)
app.openapi_schema = openapi_schema
return app.openapi_schema
app.openapi = custom_openapi You might encounter an error in the documentation related to the OpenAPI schema. It still references the _Y and _X models in the Z model, even though they have been removed from the schemas. just ignore it. Remember, this technique only removes the models from the documentation. You still need to ensure that the data is properly sent to your API for it to function correctly. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
First Check
Commit to Help
Example Code
Description
I am looking at the auto-generated docs page http://127.0.0.1:8000/docs, section “Schemas”. It shows not only the main model Z, but also auxiliary models _Y and _X. How do I hide them?
Note that the schemas of _Y and _X are perfectly visible inside the definition of Z. I only want to hide their names, and to shorten the list of schemas.
Operating System
macOS
Operating System Details
No response
FastAPI Version
0.111.1
Pydantic Version
2.8.2
Python Version
3.12
Additional Context
Beta Was this translation helpful? Give feedback.
All reactions