Is there a way to hide some fields from OpenAPI specs?

See original GitHub issue

First check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.
  • After submitting this, I commit to one of:
    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
    • I already hit the “watch” button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
    • Implement a Pull Request for a confirmed bug.

Example

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"}

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?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:23
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

17reactions
tiangolocommented, Jul 24, 2020

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

6reactions
Gatuxcommented, Oct 12, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hide a Request Field in Swagger API
A quick and practical guide to hiding a request field in Swagger UI.
Read more >
How to hide a field in OpenApi 3.0.3 - swagger
I created a swagger containing a get and a put, where in the first I call an element ("MyCar") and in the second...
Read more >
OpenAPI 3 Library for spring-boot
For custom path of the swagger documentation in HTML format, add a custom springdoc property, in your spring-boot configuration file: . # ...
Read more >
How to automatically hide fields from a flow in Jestor
Focus on what matters. This automation allows you to hide fields according to conditions of your choice. Limit the view of the least ......
Read more >
How to hide some APIs from swagger UI?
Which Swagger library do you use to generate the documentation? Should the API be hidden to everyone or based on some condition?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found