Here's a self-contained, minimal, reproducible, example with my use case:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root(hidden_param=Query(None, description="This parameter should not be visible in OpenApi specs")):
if hidden_param:
return {"Hello": "Master!"}
else:
return {"Hello": "World"}
I'd like to have some request parameters (query or body ones) to be available in the python code, but to be hidden from the OpenAPI specs. Wonder if it is possible to do?
Hmm, interesting...
I guess we could have a parameter include_in_schema for Query, Body, etc, something like:
from fastapi import FastAPI, Query, Body
from pydantic import BaseModel
app = FastAPI()
@app.get("/")
def read_root(hidden_param: str = Query(None, include_in_schema=False)):
if hidden_param:
return {"Hello": "Master!"}
else:
return {"Hello": "World"}
class Item(BaseModel):
name: str
price: float
@app.post("/items/")
def create_item(item: Item = Body(None, include_in_schema=False)):
return item
BTW, if you want to selectively remove or alter things from the schema for a Pydantic model you can already do that with schema_extra: https://pydantic-docs.helpmanual.io/usage/schema/#schema-customization
Cool @tiangolo, I will give it a try 馃槃
I would also be interested in an include_in_schema argument for Query, Body etc.
It may also be useful with Header to hide x-forwarded-for from the documentation for example, when FastAPI is used behind a reverse proxy.
I would like this feature too. We have a header with auth info thats always sent from our API manager to our backend. I want to consume this header in my API but I don't want it to show up in the docs.
Another use case: testing. In order to test the expiry of a session I use an header parameter and pass a short timeout to test for session expiry:
async def login(auth: AuthForm, x_session_timeout_sec: Optional[float] = Header(None)) -> LoginResponse:
kw = {}
conf = get_config()
if conf.testing:
kw["timeout_sec"] = x_session_timeout_sec
await create_session(user_data, **kw)
...
the use of such parameter can be disabled in production, testing it against env vars, but it's not easy to change the signature of the python function based on env vars, and changing the openapi.json for every such use is pretty cumbersome.
I have got around it by using a Depends which looks at the request directly so its not showing the parameter in documentation.
Most helpful comment
Hmm, interesting...
I guess we could have a parameter
include_in_schemaforQuery,Body, etc, something like:BTW, if you want to selectively remove or alter things from the schema for a Pydantic model you can already do that with
schema_extra: https://pydantic-docs.helpmanual.io/usage/schema/#schema-customization