Skip to content

Commit 74a8176

Browse files
authored
Merge pull request #21 from koxudaxi/fix_invalid_query_parameter
fix invalid query parameter
2 parents 20ac3ae + b83e239 commit 74a8176

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

fastapi_code_generator/parser.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def __str__(self) -> str:
7676

7777
@cached_property
7878
def argument(self) -> str:
79-
if not self.default and self.required:
79+
if self.default is None and self.required:
8080
return f'{self.name}: {self.type_hint}'
8181
return f'{self.name}: {self.type_hint} = {self.default}'
8282

@@ -168,7 +168,8 @@ def function_name(self) -> str:
168168
if self.operationId:
169169
name: str = self.operationId
170170
else:
171-
name = f"{self.type}{re.sub(r'[/{}]', '_', self.path)}"
171+
path = re.sub(r'/{|/', '_', self.snake_case_path).replace('}', '')
172+
name = f"{self.type}{path}"
172173
return stringcase.snakecase(name)
173174

174175
@cached_property
@@ -216,17 +217,18 @@ def get_parameter_type(
216217
required=parameter.get("required") or parameter.get("in") == "path",
217218
)
218219
self.imports.extend(field.imports)
219-
represented_default = repr(schema.default)
220220
if orig_name != name:
221-
default = f"Query({'...' if field.required else represented_default}, alias='{orig_name}')"
221+
default: Optional[
222+
str
223+
] = f"Query({'...' if field.required else repr(schema.default)}, alias='{orig_name}')"
222224
self.imports.append(Import(from_='fastapi', import_='Query'))
223225
else:
224-
default = represented_default
226+
default = repr(schema.default) if 'default' in parameter["schema"] else None
225227
return Argument(
226228
name=field.name,
227229
type_hint=field.type_hint,
228230
default=default, # type: ignore
229-
default_value=represented_default, # type: ignore
231+
default_value=schema.default,
230232
required=field.required,
231233
)
232234

tests/data/expected/openapi/body_and_parameters/main.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@
88

99
from fastapi import FastAPI, Query
1010

11-
from .models import Pet, PetForm, Pets
11+
from .models import Pet, PetForm
1212

1313
app = FastAPI()
1414

1515

16+
@app.get('/food/{food_id}', response_model=None)
17+
def show_food_by_id(food_id: str) -> None:
18+
pass
19+
20+
1621
@app.get('/pets', response_model=List[Pet])
1722
def list_pets(
1823
limit: Optional[int] = 0,
@@ -27,13 +32,13 @@ def post_pets(body: PetForm) -> None:
2732
pass
2833

2934

30-
@app.get('/pets/{pet_id}', response_model=Pets)
31-
def show_pet_by_id(pet_id: str = Query(..., alias='petId')) -> Pets:
35+
@app.get('/pets/{pet_id}', response_model=Pet)
36+
def show_pet_by_id(pet_id: str = Query(..., alias='petId')) -> Pet:
3237
pass
3338

3439

3540
@app.put('/pets/{pet_id}', response_model=None)
36-
def put_pets__pet_id_(
41+
def put_pets_pet_id(
3742
pet_id: str = Query(..., alias='petId'), body: PetForm = None
3843
) -> None:
3944
pass

tests/data/openapi/body_and_parameters.yaml

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ paths:
4545
content:
4646
application/json:
4747
schema:
48+
type: array
4849
items:
49-
$ref: "#/components/schemas/Pet"
50+
- $ref: "#/components/schemas/Pet"
5051
default:
5152
description: unexpected error
5253
content:
@@ -91,7 +92,7 @@ paths:
9192
content:
9293
application/json:
9394
schema:
94-
$ref: "#/components/schemas/Pets"
95+
$ref: "#/components/schemas/Pet"
9596
default:
9697
description: unexpected error
9798
content:
@@ -130,6 +131,27 @@ paths:
130131
passthroughBehavior: when_no_templates
131132
httpMethod: POST
132133
type: aws_proxy
134+
/food/{food_id}:
135+
get:
136+
summary: Info for a specific pet
137+
operationId: showFoodById
138+
tags:
139+
- foods
140+
parameters:
141+
- name: food_id
142+
in: path
143+
description: The id of the food to retrieve
144+
schema:
145+
type: string
146+
responses:
147+
'200':
148+
description: Expected response to a valid request
149+
x-amazon-apigateway-integration:
150+
uri:
151+
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${PythonVersionFunction.Arn}/invocations
152+
passthroughBehavior: when_no_templates
153+
httpMethod: POST
154+
type: aws_proxy
133155
components:
134156
schemas:
135157
Pet:

0 commit comments

Comments
 (0)