Skip to content

Commit

Permalink
✨ Allow hiding from OpenAPI (and Swagger UI) Query, Cookie, `Head…
Browse files Browse the repository at this point in the history
…er`, and `Path` parameters (#3144)

Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
  • Loading branch information
astraldawn and tiangolo committed Jan 23, 2022
1 parent 347e391 commit ca5d57e
Show file tree
Hide file tree
Showing 9 changed files with 474 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docs/en/docs/tutorial/query-params-str-validations.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,22 @@ The docs will show it like this:

<img src="/img/tutorial/query-params-str-validations/image01.png">

## Exclude from OpenAPI

To exclude a query parameter from the generated OpenAPI schema (and thus, from the automatic documentation systems), set the parameter `include_in_schema` of `Query` to `False`:

=== "Python 3.6 and above"

```Python hl_lines="10"
{!> ../../../docs_src/query_params_str_validations/tutorial014.py!}
```

=== "Python 3.10 and above"

```Python hl_lines="7"
{!> ../../../docs_src/query_params_str_validations/tutorial014_py310.py!}
```

## Recap

You can declare additional validations and metadata for your parameters.
Expand Down
15 changes: 15 additions & 0 deletions docs_src/query_params_str_validations/tutorial014.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from typing import Optional

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(
hidden_query: Optional[str] = Query(None, include_in_schema=False)
):
if hidden_query:
return {"hidden_query": hidden_query}
else:
return {"hidden_query": "Not found"}
11 changes: 11 additions & 0 deletions docs_src/query_params_str_validations/tutorial014_py310.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(hidden_query: str | None = Query(None, include_in_schema=False)):
if hidden_query:
return {"hidden_query": hidden_query}
else:
return {"hidden_query": "Not found"}
2 changes: 2 additions & 0 deletions fastapi/openapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ def get_openapi_operation_parameters(
for param in all_route_params:
field_info = param.field_info
field_info = cast(Param, field_info)
if not field_info.include_in_schema:
continue
parameter = {
"name": param.alias,
"in": field_info.in_.value,
Expand Down
8 changes: 8 additions & 0 deletions fastapi/param_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def Path( # noqa: N802
example: Any = Undefined,
examples: Optional[Dict[str, Any]] = None,
deprecated: Optional[bool] = None,
include_in_schema: bool = True,
**extra: Any,
) -> Any:
return params.Path(
Expand All @@ -37,6 +38,7 @@ def Path( # noqa: N802
example=example,
examples=examples,
deprecated=deprecated,
include_in_schema=include_in_schema,
**extra,
)

Expand All @@ -57,6 +59,7 @@ def Query( # noqa: N802
example: Any = Undefined,
examples: Optional[Dict[str, Any]] = None,
deprecated: Optional[bool] = None,
include_in_schema: bool = True,
**extra: Any,
) -> Any:
return params.Query(
Expand All @@ -74,6 +77,7 @@ def Query( # noqa: N802
example=example,
examples=examples,
deprecated=deprecated,
include_in_schema=include_in_schema,
**extra,
)

Expand All @@ -95,6 +99,7 @@ def Header( # noqa: N802
example: Any = Undefined,
examples: Optional[Dict[str, Any]] = None,
deprecated: Optional[bool] = None,
include_in_schema: bool = True,
**extra: Any,
) -> Any:
return params.Header(
Expand All @@ -113,6 +118,7 @@ def Header( # noqa: N802
example=example,
examples=examples,
deprecated=deprecated,
include_in_schema=include_in_schema,
**extra,
)

Expand All @@ -133,6 +139,7 @@ def Cookie( # noqa: N802
example: Any = Undefined,
examples: Optional[Dict[str, Any]] = None,
deprecated: Optional[bool] = None,
include_in_schema: bool = True,
**extra: Any,
) -> Any:
return params.Cookie(
Expand All @@ -150,6 +157,7 @@ def Cookie( # noqa: N802
example=example,
examples=examples,
deprecated=deprecated,
include_in_schema=include_in_schema,
**extra,
)

Expand Down
10 changes: 10 additions & 0 deletions fastapi/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ def __init__(
example: Any = Undefined,
examples: Optional[Dict[str, Any]] = None,
deprecated: Optional[bool] = None,
include_in_schema: bool = True,
**extra: Any,
):
self.deprecated = deprecated
self.example = example
self.examples = examples
self.include_in_schema = include_in_schema
super().__init__(
default,
alias=alias,
Expand Down Expand Up @@ -75,6 +77,7 @@ def __init__(
example: Any = Undefined,
examples: Optional[Dict[str, Any]] = None,
deprecated: Optional[bool] = None,
include_in_schema: bool = True,
**extra: Any,
):
self.in_ = self.in_
Expand All @@ -93,6 +96,7 @@ def __init__(
deprecated=deprecated,
example=example,
examples=examples,
include_in_schema=include_in_schema,
**extra,
)

Expand All @@ -117,6 +121,7 @@ def __init__(
example: Any = Undefined,
examples: Optional[Dict[str, Any]] = None,
deprecated: Optional[bool] = None,
include_in_schema: bool = True,
**extra: Any,
):
super().__init__(
Expand All @@ -134,6 +139,7 @@ def __init__(
deprecated=deprecated,
example=example,
examples=examples,
include_in_schema=include_in_schema,
**extra,
)

Expand All @@ -159,6 +165,7 @@ def __init__(
example: Any = Undefined,
examples: Optional[Dict[str, Any]] = None,
deprecated: Optional[bool] = None,
include_in_schema: bool = True,
**extra: Any,
):
self.convert_underscores = convert_underscores
Expand All @@ -177,6 +184,7 @@ def __init__(
deprecated=deprecated,
example=example,
examples=examples,
include_in_schema=include_in_schema,
**extra,
)

Expand All @@ -201,6 +209,7 @@ def __init__(
example: Any = Undefined,
examples: Optional[Dict[str, Any]] = None,
deprecated: Optional[bool] = None,
include_in_schema: bool = True,
**extra: Any,
):
super().__init__(
Expand All @@ -218,6 +227,7 @@ def __init__(
deprecated=deprecated,
example=example,
examples=examples,
include_in_schema=include_in_schema,
**extra,
)

Expand Down

0 comments on commit ca5d57e

Please sign in to comment.