From 554904a28b1879e13f20f5e23821d7c32ee4a415 Mon Sep 17 00:00:00 2001 From: Sander Hoentjen Date: Thu, 4 Jan 2024 15:08:47 +0100 Subject: [PATCH] [python-fastapi] Ensure path param is ... instead of None Fixes #16029 Code from https://github.com/OpenAPITools/openapi-generator/issues/16029#issuecomment-1776271921 --- .../python-fastapi/endpoint_argument_definition.mustache | 2 +- .../python-fastapi/src/openapi_server/apis/pet_api.py | 8 ++++---- .../python-fastapi/src/openapi_server/apis/store_api.py | 4 ++-- .../python-fastapi/src/openapi_server/apis/user_api.py | 6 +++--- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python-fastapi/endpoint_argument_definition.mustache b/modules/openapi-generator/src/main/resources/python-fastapi/endpoint_argument_definition.mustache index 2b4b6d7c4887..c8887fb64f4e 100644 --- a/modules/openapi-generator/src/main/resources/python-fastapi/endpoint_argument_definition.mustache +++ b/modules/openapi-generator/src/main/resources/python-fastapi/endpoint_argument_definition.mustache @@ -1 +1 @@ -{{#isPathParam}}{{baseName}}{{/isPathParam}}{{^isPathParam}}{{paramName}}{{/isPathParam}}: {{>param_type}} = {{#isPathParam}}Path{{/isPathParam}}{{#isHeaderParam}}Header{{/isHeaderParam}}{{#isFormParam}}Form{{/isFormParam}}{{#isQueryParam}}Query{{/isQueryParam}}{{#isCookieParam}}Cookie{{/isCookieParam}}{{#isBodyParam}}Body{{/isBodyParam}}({{&defaultValue}}{{^defaultValue}}None{{/defaultValue}}, description="{{description}}"{{#isQueryParam}}, alias="{{baseName}}"{{/isQueryParam}}{{#isLong}}{{#minimum}}, ge={{.}}{{/minimum}}{{#maximum}}, le={{.}}{{/maximum}}{{/isLong}}{{#isInteger}}{{#minimum}}, ge={{.}}{{/minimum}}{{#maximum}}, le={{.}}{{/maximum}}{{/isInteger}}{{#pattern}}, regex=r"{{.}}"{{/pattern}}{{#minLength}}, min_length={{.}}{{/minLength}}{{#maxLength}}, max_length={{.}}{{/maxLength}}) \ No newline at end of file +{{#isPathParam}}{{baseName}}{{/isPathParam}}{{^isPathParam}}{{paramName}}{{/isPathParam}}: {{>param_type}} = {{#isPathParam}}Path{{/isPathParam}}{{#isHeaderParam}}Header{{/isHeaderParam}}{{#isFormParam}}Form{{/isFormParam}}{{#isQueryParam}}Query{{/isQueryParam}}{{#isCookieParam}}Cookie{{/isCookieParam}}{{#isBodyParam}}Body{{/isBodyParam}}({{&defaultValue}}{{^defaultValue}}{{#isPathParam}}...{{/isPathParam}}{{^isPathParam}}None{{/isPathParam}}{{/defaultValue}}, description="{{description}}"{{#isQueryParam}}, alias="{{baseName}}"{{/isQueryParam}}{{#isLong}}{{#minimum}}, ge={{.}}{{/minimum}}{{#maximum}}, le={{.}}{{/maximum}}{{/isLong}}{{#isInteger}}{{#minimum}}, ge={{.}}{{/minimum}}{{#maximum}}, le={{.}}{{/maximum}}{{/isInteger}}{{#pattern}}, regex=r"{{.}}"{{/pattern}}{{#minLength}}, min_length={{.}}{{/minLength}}{{#maxLength}}, max_length={{.}}{{/maxLength}}) \ No newline at end of file diff --git a/samples/server/petstore/python-fastapi/src/openapi_server/apis/pet_api.py b/samples/server/petstore/python-fastapi/src/openapi_server/apis/pet_api.py index cf8ada786d1e..1229ceeed197 100644 --- a/samples/server/petstore/python-fastapi/src/openapi_server/apis/pet_api.py +++ b/samples/server/petstore/python-fastapi/src/openapi_server/apis/pet_api.py @@ -63,7 +63,7 @@ async def add_pet( response_model_by_alias=True, ) async def delete_pet( - petId: int = Path(None, description="Pet id to delete"), + petId: int = Path(..., description="Pet id to delete"), api_key: str = Header(None, description=""), token_petstore_auth: TokenModel = Security( get_token_petstore_auth, scopes=["write:pets", "read:pets"] @@ -125,7 +125,7 @@ async def find_pets_by_tags( response_model_by_alias=True, ) async def get_pet_by_id( - petId: int = Path(None, description="ID of pet to return"), + petId: int = Path(..., description="ID of pet to return"), token_api_key: TokenModel = Security( get_token_api_key ), @@ -166,7 +166,7 @@ async def update_pet( response_model_by_alias=True, ) async def update_pet_with_form( - petId: int = Path(None, description="ID of pet that needs to be updated"), + petId: int = Path(..., description="ID of pet that needs to be updated"), name: str = Form(None, description="Updated name of the pet"), status: str = Form(None, description="Updated status of the pet"), token_petstore_auth: TokenModel = Security( @@ -187,7 +187,7 @@ async def update_pet_with_form( response_model_by_alias=True, ) async def upload_file( - petId: int = Path(None, description="ID of pet to update"), + petId: int = Path(..., description="ID of pet to update"), additional_metadata: str = Form(None, description="Additional data to pass to server"), file: str = Form(None, description="file to upload"), token_petstore_auth: TokenModel = Security( diff --git a/samples/server/petstore/python-fastapi/src/openapi_server/apis/store_api.py b/samples/server/petstore/python-fastapi/src/openapi_server/apis/store_api.py index 585a671bbaf4..b583c10a625b 100644 --- a/samples/server/petstore/python-fastapi/src/openapi_server/apis/store_api.py +++ b/samples/server/petstore/python-fastapi/src/openapi_server/apis/store_api.py @@ -43,7 +43,7 @@ response_model_by_alias=True, ) async def delete_order( - orderId: str = Path(None, description="ID of the order that needs to be deleted"), + orderId: str = Path(..., description="ID of the order that needs to be deleted"), ) -> None: """For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors""" return BaseStoreApi.subclasses[0]().delete_order(orderId) @@ -79,7 +79,7 @@ async def get_inventory( response_model_by_alias=True, ) async def get_order_by_id( - orderId: int = Path(None, description="ID of pet that needs to be fetched", ge=1, le=5), + orderId: int = Path(..., description="ID of pet that needs to be fetched", ge=1, le=5), ) -> Order: """For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions""" return BaseStoreApi.subclasses[0]().get_order_by_id(orderId) diff --git a/samples/server/petstore/python-fastapi/src/openapi_server/apis/user_api.py b/samples/server/petstore/python-fastapi/src/openapi_server/apis/user_api.py index e57b899c69b6..1ee291c2a02b 100644 --- a/samples/server/petstore/python-fastapi/src/openapi_server/apis/user_api.py +++ b/samples/server/petstore/python-fastapi/src/openapi_server/apis/user_api.py @@ -100,7 +100,7 @@ async def create_users_with_list_input( response_model_by_alias=True, ) async def delete_user( - username: str = Path(None, description="The name that needs to be deleted"), + username: str = Path(..., description="The name that needs to be deleted"), token_api_key: TokenModel = Security( get_token_api_key ), @@ -121,7 +121,7 @@ async def delete_user( response_model_by_alias=True, ) async def get_user_by_name( - username: str = Path(None, description="The name that needs to be fetched. Use user1 for testing."), + username: str = Path(..., description="The name that needs to be fetched. Use user1 for testing."), ) -> User: """""" return BaseUserApi.subclasses[0]().get_user_by_name(username) @@ -174,7 +174,7 @@ async def logout_user( response_model_by_alias=True, ) async def update_user( - username: str = Path(None, description="name that need to be deleted"), + username: str = Path(..., description="name that need to be deleted"), user: User = Body(None, description="Updated user object"), token_api_key: TokenModel = Security( get_token_api_key