Skip to content
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

✨ Allow hiding from OpenAPI (and Swagger UI) Query, Cookie, Header, and Path parameters #3144

Merged
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
de951d9
Add include_in_schema parameter to headers to allow hiding in openapi…
astraldawn Mar 29, 2021
f64be7c
Remove unused print statement
astraldawn Mar 29, 2021
6264c0d
Swap header order in request for consistency
astraldawn Mar 29, 2021
29c8e57
Move tests to more appropriate location, update docs
astraldawn Mar 30, 2021
2447106
Use correct tutorial
astraldawn Mar 30, 2021
f34b9e1
Adjust highlight
astraldawn Mar 30, 2021
7b90c62
Enable include_in_schema for query parameters
astraldawn Apr 29, 2021
4e3a271
Generalise include_in_schema to param class
astraldawn Apr 29, 2021
f852aec
Consolidate tests
astraldawn Apr 29, 2021
685532c
Tidy up code
astraldawn Apr 29, 2021
cf8b080
Merge remote-tracking branch 'upstream/master' into hide-request-para…
astraldawn May 8, 2021
c46ae17
♻️ Simplify indentation in check for include_in_schema
tiangolo Jan 23, 2022
ca5b4d5
✅ Update tests for consistency
tiangolo Jan 23, 2022
c973938
🔀 Merge master
tiangolo Jan 23, 2022
8b60572
✅ Update tests for include_in_schema in params
tiangolo Jan 23, 2022
78e7439
📝 Update source example for include_in_schema
tiangolo Jan 23, 2022
7045627
📝 Add source example for include_in_schema for Python 3.10
tiangolo Jan 23, 2022
4e69689
✅ Add test for include_in_schema with Python 3.10
tiangolo Jan 23, 2022
c49e01c
📝 Update docs for include_in_schema with example in Python 3.6+ and 3.10
tiangolo Jan 23, 2022
fcf3c1c
✅ Fix tests for include_in_schema for Python 3.10
tiangolo Jan 23, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 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,14 @@ 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 hl_lines="7"
{!../../../docs_src/query_params_str_validations/tutorial014.py!}
```

## Recap

You can declare additional validations and metadata for your parameters.
Expand Down
11 changes: 11 additions & 0 deletions docs_src/query_params_str_validations/tutorial014.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 = 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,
tiangolo marked this conversation as resolved.
Show resolved Hide resolved
**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