-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there a way to hide some fields from OpenAPI specs? #1708
Comments
Hmm, interesting... I guess we could have a parameter 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 |
Cool @tiangolo, I will give it a try 😄 |
I would also be interested in an |
It may also be useful with |
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 |
I have got around it by using a Depends which looks at the request directly so its not showing the parameter in documentation. |
@mnorrsken how did you get rid of the parameters with Depends? @router.get("/health")
async def health_check(*, settings = Depends(config.get_settings)) -> dict:
"""Answers ok if the system is up and running."""
# settings = config.get_settings()
return {
"Application": settings.app_name,
"Status": "OK",
}
#config.py
def get_settings(env_file: str = None):
#... To solve that I moved I moved the settings declaration to inside the function, which solved the problem. |
Sorry for late reply. Im using the raw request object in the parameters and extracting my data from that. it would becomes something like |
I am also running into the same issue mentioned by @daniel-demelo. In my case, I have a service listed as a dependency for a router. The service has an
router = APIRouter()
@router.get("/")
def get(service=Depends(Service):
...
class Service():
def __init__(self, thing: Optional[SomeNonPydanticThing]=None):
self.thing = thing or SomeNonPydanticThing() # use passed in value if truthy, else create a default value I am getting the following error:
Perhaps |
Agree with @joeriddles : |
unless you want to use the result of depends in your handler. |
Thanks, everyone! This was included in #3144, it's now available in FastAPI version |
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
First check
Example
Here's a self-contained, minimal, reproducible, example with my use case:
Description
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?
The text was updated successfully, but these errors were encountered: