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 support for exclusion/inclusion of none and unset fields in OpenAPI schema #5693

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions fastapi/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ def __init__(
generate_unique_id_function: Callable[[routing.APIRoute], str] = Default(
generate_unique_id
),
openapi_schema_exclude_unset: bool = False,
openapi_schema_exclude_none: bool = True,
**extra: Any,
) -> None:
self._debug: bool = debug
Expand All @@ -105,6 +107,8 @@ def __init__(
self.extra = extra
self.openapi_version = "3.0.2"
self.openapi_schema: Optional[Dict[str, Any]] = None
self.openapi_schema_exclude_unset = openapi_schema_exclude_unset
self.openapi_schema_exclude_none = openapi_schema_exclude_none
if self.openapi_url:
assert self.title, "A title must be provided for OpenAPI, e.g.: 'My API'"
assert self.version, "A version must be provided for OpenAPI, e.g.: '2.1.0'"
Expand Down Expand Up @@ -208,6 +212,8 @@ def openapi(self) -> Dict[str, Any]:
routes=self.routes,
tags=self.openapi_tags,
servers=self.servers,
exclude_unset=self.openapi_schema_exclude_unset,
exclude_none=self.openapi_schema_exclude_none,
)
return self.openapi_schema

Expand Down
9 changes: 8 additions & 1 deletion fastapi/openapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@ def get_openapi(
terms_of_service: Optional[str] = None,
contact: Optional[Dict[str, Union[str, Any]]] = None,
license_info: Optional[Dict[str, Union[str, Any]]] = None,
exclude_unset: bool = False,
exclude_none: bool = True,
) -> Dict[str, Any]:
info: Dict[str, Any] = {"title": title, "version": version}
if description:
Expand Down Expand Up @@ -445,4 +447,9 @@ def get_openapi(
output["paths"] = paths
if tags:
output["tags"] = tags
return jsonable_encoder(OpenAPI(**output), by_alias=True, exclude_none=True) # type: ignore
return jsonable_encoder( # type: ignore
OpenAPI(**output),
by_alias=True,
exclude_unset=exclude_unset,
exclude_none=exclude_none,
)
57 changes: 57 additions & 0 deletions tests/test_get_openapi_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from fastapi import FastAPI
from fastapi.testclient import TestClient

app = FastAPI(
openapi_schema_exclude_unset=True,
openapi_schema_exclude_none=False,
)


@app.get(
"/items",
responses={
200: {"content": {"application/json": {"example": {"id": None, "value": 50}}}}
},
)
def get_items():
return {"id": "foo", "value": 50}


client = TestClient(app)


openapi_schema = {
"openapi": "3.0.2",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/items": {
"get": {
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {},
"example": {"id": None, "value": 50},
}
},
}
},
"summary": "Get Items",
"operationId": "get_items_items_get",
}
}
},
}


def test_openapi_schema():
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == openapi_schema


def test_default_get_items():
response = client.get("/items")
assert response.status_code == 200, response.text
assert response.json() == {"id": "foo", "value": 50}