From f482866acf9f46e3c570c6bf387ee760f88b6538 Mon Sep 17 00:00:00 2001 From: Koudai Aono Date: Sun, 21 Jun 2020 04:43:30 +0900 Subject: [PATCH 1/5] fix invalid query parameter --- fastapi_code_generator/parser.py | 7 +++-- .../openapi/body_and_parameters/main.py | 11 +++++--- tests/data/openapi/body_and_parameters.yaml | 26 +++++++++++++++++-- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/fastapi_code_generator/parser.py b/fastapi_code_generator/parser.py index e287527..75f7837 100644 --- a/fastapi_code_generator/parser.py +++ b/fastapi_code_generator/parser.py @@ -216,17 +216,16 @@ def get_parameter_type( required=parameter.get("required") or parameter.get("in") == "path", ) self.imports.extend(field.imports) - represented_default = repr(schema.default) if orig_name != name: - default = f"Query({'...' if field.required else represented_default}, alias='{orig_name}')" + default = f"Query({'...' if field.required else repr(schema.default)}, alias='{orig_name}')" self.imports.append(Import(from_='fastapi', import_='Query')) else: - default = represented_default + default = repr(schema.default) if 'default' in parameter["schema"] else None return Argument( name=field.name, type_hint=field.type_hint, default=default, # type: ignore - default_value=represented_default, # type: ignore + default_value=schema.default, # type: ignore required=field.required, ) diff --git a/tests/data/expected/openapi/body_and_parameters/main.py b/tests/data/expected/openapi/body_and_parameters/main.py index 413dde7..83b8538 100644 --- a/tests/data/expected/openapi/body_and_parameters/main.py +++ b/tests/data/expected/openapi/body_and_parameters/main.py @@ -8,11 +8,16 @@ from fastapi import FastAPI, Query -from .models import Pet, PetForm, Pets +from .models import Pet, PetForm app = FastAPI() +@app.get('/food/{food_id}', response_model=None) +def show_food_by_id(food_id: str) -> None: + pass + + @app.get('/pets', response_model=List[Pet]) def list_pets( limit: Optional[int] = 0, @@ -27,8 +32,8 @@ def post_pets(body: PetForm) -> None: pass -@app.get('/pets/{pet_id}', response_model=Pets) -def show_pet_by_id(pet_id: str = Query(..., alias='petId')) -> Pets: +@app.get('/pets/{pet_id}', response_model=Pet) +def show_pet_by_id(pet_id: str = Query(..., alias='petId')) -> Pet: pass diff --git a/tests/data/openapi/body_and_parameters.yaml b/tests/data/openapi/body_and_parameters.yaml index 721f29a..c3106b0 100644 --- a/tests/data/openapi/body_and_parameters.yaml +++ b/tests/data/openapi/body_and_parameters.yaml @@ -45,8 +45,9 @@ paths: content: application/json: schema: + type: array items: - $ref: "#/components/schemas/Pet" + - $ref: "#/components/schemas/Pet" default: description: unexpected error content: @@ -91,7 +92,7 @@ paths: content: application/json: schema: - $ref: "#/components/schemas/Pets" + $ref: "#/components/schemas/Pet" default: description: unexpected error content: @@ -130,6 +131,27 @@ paths: passthroughBehavior: when_no_templates httpMethod: POST type: aws_proxy + /food/{food_id}: + get: + summary: Info for a specific pet + operationId: showFoodById + tags: + - foods + parameters: + - name: food_id + in: path + description: The id of the food to retrieve + schema: + type: string + responses: + '200': + description: Expected response to a valid request + x-amazon-apigateway-integration: + uri: + Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${PythonVersionFunction.Arn}/invocations + passthroughBehavior: when_no_templates + httpMethod: POST + type: aws_proxy components: schemas: Pet: From 80a95c47700979bc5ded5809b8f95601d1619ea7 Mon Sep 17 00:00:00 2001 From: Koudai Aono Date: Sun, 21 Jun 2020 04:48:01 +0900 Subject: [PATCH 2/5] fix types --- fastapi_code_generator/parser.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/fastapi_code_generator/parser.py b/fastapi_code_generator/parser.py index 75f7837..87bb8c7 100644 --- a/fastapi_code_generator/parser.py +++ b/fastapi_code_generator/parser.py @@ -217,7 +217,9 @@ def get_parameter_type( ) self.imports.extend(field.imports) if orig_name != name: - default = f"Query({'...' if field.required else repr(schema.default)}, alias='{orig_name}')" + default: Optional[ + str + ] = f"Query({'...' if field.required else repr(schema.default)}, alias='{orig_name}')" self.imports.append(Import(from_='fastapi', import_='Query')) else: default = repr(schema.default) if 'default' in parameter["schema"] else None @@ -225,7 +227,7 @@ def get_parameter_type( name=field.name, type_hint=field.type_hint, default=default, # type: ignore - default_value=schema.default, # type: ignore + default_value=schema.default, required=field.required, ) From 4f0281a94cde50b9f9d929e3b8ffdcea2442fa07 Mon Sep 17 00:00:00 2001 From: Koudai Aono Date: Sun, 21 Jun 2020 05:07:36 +0900 Subject: [PATCH 3/5] change a condition for checking default --- fastapi_code_generator/parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastapi_code_generator/parser.py b/fastapi_code_generator/parser.py index 87bb8c7..ced54bd 100644 --- a/fastapi_code_generator/parser.py +++ b/fastapi_code_generator/parser.py @@ -76,7 +76,7 @@ def __str__(self) -> str: @cached_property def argument(self) -> str: - if not self.default and self.required: + if self.default is None and self.required: return f'{self.name}: {self.type_hint}' return f'{self.name}: {self.type_hint} = {self.default}' From 982cb600bde027ec98ef7ce168bf4e941a1c517c Mon Sep 17 00:00:00 2001 From: Koudai Aono Date: Sun, 21 Jun 2020 05:17:51 +0900 Subject: [PATCH 4/5] fix function name with query parameter --- fastapi_code_generator/parser.py | 3 ++- tests/data/expected/openapi/body_and_parameters/main.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/fastapi_code_generator/parser.py b/fastapi_code_generator/parser.py index ced54bd..01eb4a4 100644 --- a/fastapi_code_generator/parser.py +++ b/fastapi_code_generator/parser.py @@ -168,7 +168,8 @@ def function_name(self) -> str: if self.operationId: name: str = self.operationId else: - name = f"{self.type}{re.sub(r'[/{}]', '_', self.path)}" + path = re.sub(r'/{|/','_', self.snake_case_path).replace('}', '') + name = f"{self.type}{path}" return stringcase.snakecase(name) @cached_property diff --git a/tests/data/expected/openapi/body_and_parameters/main.py b/tests/data/expected/openapi/body_and_parameters/main.py index 83b8538..0f4570e 100644 --- a/tests/data/expected/openapi/body_and_parameters/main.py +++ b/tests/data/expected/openapi/body_and_parameters/main.py @@ -38,7 +38,7 @@ def show_pet_by_id(pet_id: str = Query(..., alias='petId')) -> Pet: @app.put('/pets/{pet_id}', response_model=None) -def put_pets__pet_id_( +def put_pets_pet_id( pet_id: str = Query(..., alias='petId'), body: PetForm = None ) -> None: pass From b83e2396a52f4269dc30629d722897ac68605453 Mon Sep 17 00:00:00 2001 From: Koudai Aono Date: Sun, 21 Jun 2020 05:19:36 +0900 Subject: [PATCH 5/5] fix format --- fastapi_code_generator/parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastapi_code_generator/parser.py b/fastapi_code_generator/parser.py index 01eb4a4..681db9d 100644 --- a/fastapi_code_generator/parser.py +++ b/fastapi_code_generator/parser.py @@ -168,7 +168,7 @@ def function_name(self) -> str: if self.operationId: name: str = self.operationId else: - path = re.sub(r'/{|/','_', self.snake_case_path).replace('}', '') + path = re.sub(r'/{|/', '_', self.snake_case_path).replace('}', '') name = f"{self.type}{path}" return stringcase.snakecase(name)