Skip to content

Commit f57a1d5

Browse files
committed
[python-fastapi] exclude unset response fields
Exclude unset fields when marshalling api endpoint response bodies.
1 parent 61ab83a commit f57a1d5

File tree

5 files changed

+22
-0
lines changed

5 files changed

+22
-0
lines changed

modules/openapi-generator/src/main/resources/python-fastapi/api.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ for _, name, _ in pkgutil.iter_modules(ns_pkg.__path__, ns_pkg.__name__ + "."):
5252
description = "{{.}}",
5353
{{/description}}
5454
response_model_by_alias=True,
55+
response_model_exclude_unset=True,
5556
)
5657
async def {{operationId}}(
5758
{{#allParams}}

samples/server/petstore/python-fastapi/src/openapi_server/apis/fake_api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
tags=["fake"],
4545
summary="test query parameter default value",
4646
response_model_by_alias=True,
47+
response_model_exclude_unset=True,
4748
)
4849
async def fake_query_param_default(
4950
has_default: Annotated[Optional[StrictStr], Field(description="has default value")] = Query('Hello World', description="has default value", alias="hasDefault"),

samples/server/petstore/python-fastapi/src/openapi_server/apis/pet_api.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
tags=["pet"],
4747
summary="Add a new pet to the store",
4848
response_model_by_alias=True,
49+
response_model_exclude_unset=True,
4950
)
5051
async def add_pet(
5152
pet: Annotated[Pet, Field(description="Pet object that needs to be added to the store")] = Body(None, description="Pet object that needs to be added to the store"),
@@ -67,6 +68,7 @@ async def add_pet(
6768
tags=["pet"],
6869
summary="Deletes a pet",
6970
response_model_by_alias=True,
71+
response_model_exclude_unset=True,
7072
)
7173
async def delete_pet(
7274
petId: Annotated[StrictInt, Field(description="Pet id to delete")] = Path(..., description="Pet id to delete"),
@@ -90,6 +92,7 @@ async def delete_pet(
9092
tags=["pet"],
9193
summary="Finds Pets by status",
9294
response_model_by_alias=True,
95+
response_model_exclude_unset=True,
9396
)
9497
async def find_pets_by_status(
9598
status: Annotated[List[StrictStr], Field(description="Status values that need to be considered for filter")] = Query(None, description="Status values that need to be considered for filter", alias="status"),
@@ -112,6 +115,7 @@ async def find_pets_by_status(
112115
tags=["pet"],
113116
summary="Finds Pets by tags",
114117
response_model_by_alias=True,
118+
response_model_exclude_unset=True,
115119
)
116120
async def find_pets_by_tags(
117121
tags: Annotated[List[StrictStr], Field(description="Tags to filter by")] = Query(None, description="Tags to filter by", alias="tags"),
@@ -135,6 +139,7 @@ async def find_pets_by_tags(
135139
tags=["pet"],
136140
summary="Find pet by ID",
137141
response_model_by_alias=True,
142+
response_model_exclude_unset=True,
138143
)
139144
async def get_pet_by_id(
140145
petId: Annotated[StrictInt, Field(description="ID of pet to return")] = Path(..., description="ID of pet to return"),
@@ -159,6 +164,7 @@ async def get_pet_by_id(
159164
tags=["pet"],
160165
summary="Update an existing pet",
161166
response_model_by_alias=True,
167+
response_model_exclude_unset=True,
162168
)
163169
async def update_pet(
164170
pet: Annotated[Pet, Field(description="Pet object that needs to be added to the store")] = Body(None, description="Pet object that needs to be added to the store"),
@@ -180,6 +186,7 @@ async def update_pet(
180186
tags=["pet"],
181187
summary="Updates a pet in the store with form data",
182188
response_model_by_alias=True,
189+
response_model_exclude_unset=True,
183190
)
184191
async def update_pet_with_form(
185192
petId: Annotated[StrictInt, Field(description="ID of pet that needs to be updated")] = Path(..., description="ID of pet that needs to be updated"),
@@ -203,6 +210,7 @@ async def update_pet_with_form(
203210
tags=["pet"],
204211
summary="uploads an image",
205212
response_model_by_alias=True,
213+
response_model_exclude_unset=True,
206214
)
207215
async def upload_file(
208216
petId: Annotated[StrictInt, Field(description="ID of pet to update")] = Path(..., description="ID of pet to update"),

samples/server/petstore/python-fastapi/src/openapi_server/apis/store_api.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
tags=["store"],
4646
summary="Delete purchase order by ID",
4747
response_model_by_alias=True,
48+
response_model_exclude_unset=True,
4849
)
4950
async def delete_order(
5051
orderId: Annotated[StrictStr, Field(description="ID of the order that needs to be deleted")] = Path(..., description="ID of the order that needs to be deleted"),
@@ -63,6 +64,7 @@ async def delete_order(
6364
tags=["store"],
6465
summary="Returns pet inventories by status",
6566
response_model_by_alias=True,
67+
response_model_exclude_unset=True,
6668
)
6769
async def get_inventory(
6870
token_api_key: TokenModel = Security(
@@ -85,6 +87,7 @@ async def get_inventory(
8587
tags=["store"],
8688
summary="Find purchase order by ID",
8789
response_model_by_alias=True,
90+
response_model_exclude_unset=True,
8891
)
8992
async def get_order_by_id(
9093
orderId: Annotated[int, Field(le=5, strict=True, ge=1, description="ID of pet that needs to be fetched")] = Path(..., description="ID of pet that needs to be fetched", ge=1, le=5),
@@ -104,6 +107,7 @@ async def get_order_by_id(
104107
tags=["store"],
105108
summary="Place an order for a pet",
106109
response_model_by_alias=True,
110+
response_model_exclude_unset=True,
107111
)
108112
async def place_order(
109113
order: Annotated[Order, Field(description="order placed for purchasing the pet")] = Body(None, description="order placed for purchasing the pet"),

samples/server/petstore/python-fastapi/src/openapi_server/apis/user_api.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
tags=["user"],
4545
summary="Create user",
4646
response_model_by_alias=True,
47+
response_model_exclude_unset=True,
4748
)
4849
async def create_user(
4950
user: Annotated[User, Field(description="Created user object")] = Body(None, description="Created user object"),
@@ -65,6 +66,7 @@ async def create_user(
6566
tags=["user"],
6667
summary="Creates list of users with given input array",
6768
response_model_by_alias=True,
69+
response_model_exclude_unset=True,
6870
)
6971
async def create_users_with_array_input(
7072
user: Annotated[List[User], Field(description="List of user object")] = Body(None, description="List of user object"),
@@ -86,6 +88,7 @@ async def create_users_with_array_input(
8688
tags=["user"],
8789
summary="Creates list of users with given input array",
8890
response_model_by_alias=True,
91+
response_model_exclude_unset=True,
8992
)
9093
async def create_users_with_list_input(
9194
user: Annotated[List[User], Field(description="List of user object")] = Body(None, description="List of user object"),
@@ -108,6 +111,7 @@ async def create_users_with_list_input(
108111
tags=["user"],
109112
summary="Delete user",
110113
response_model_by_alias=True,
114+
response_model_exclude_unset=True,
111115
)
112116
async def delete_user(
113117
username: Annotated[StrictStr, Field(description="The name that needs to be deleted")] = Path(..., description="The name that needs to be deleted"),
@@ -131,6 +135,7 @@ async def delete_user(
131135
tags=["user"],
132136
summary="Get user by user name",
133137
response_model_by_alias=True,
138+
response_model_exclude_unset=True,
134139
)
135140
async def get_user_by_name(
136141
username: Annotated[StrictStr, Field(description="The name that needs to be fetched. Use user1 for testing.")] = Path(..., description="The name that needs to be fetched. Use user1 for testing."),
@@ -150,6 +155,7 @@ async def get_user_by_name(
150155
tags=["user"],
151156
summary="Logs user into the system",
152157
response_model_by_alias=True,
158+
response_model_exclude_unset=True,
153159
)
154160
async def login_user(
155161
username: Annotated[str, Field(strict=True, description="The user name for login")] = Query(None, description="The user name for login", alias="username", regex=r"/^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$/"),
@@ -169,6 +175,7 @@ async def login_user(
169175
tags=["user"],
170176
summary="Logs out current logged in user session",
171177
response_model_by_alias=True,
178+
response_model_exclude_unset=True,
172179
)
173180
async def logout_user(
174181
token_api_key: TokenModel = Security(
@@ -190,6 +197,7 @@ async def logout_user(
190197
tags=["user"],
191198
summary="Updated user",
192199
response_model_by_alias=True,
200+
response_model_exclude_unset=True,
193201
)
194202
async def update_user(
195203
username: Annotated[StrictStr, Field(description="name that need to be deleted")] = Path(..., description="name that need to be deleted"),

0 commit comments

Comments
 (0)