Skip to content

Commit 20ac3ae

Browse files
authored
Merge pull request #20 from koxudaxi/fix_default
fix default
2 parents 983c0bc + bad3088 commit 20ac3ae

File tree

5 files changed

+44
-28
lines changed

5 files changed

+44
-28
lines changed

fastapi_code_generator/parser.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,19 +214,19 @@ def get_parameter_type(
214214
name=name,
215215
data_types=[type_map[type_]],
216216
required=parameter.get("required") or parameter.get("in") == "path",
217-
default=schema.typed_default,
218217
)
219218
self.imports.extend(field.imports)
219+
represented_default = repr(schema.default)
220220
if orig_name != name:
221-
default = f"Query({'...' if field.required else field.default}, alias='{orig_name}')"
221+
default = f"Query({'...' if field.required else represented_default}, alias='{orig_name}')"
222222
self.imports.append(Import(from_='fastapi', import_='Query'))
223223
else:
224-
default = field.default
224+
default = represented_default
225225
return Argument(
226226
name=field.name,
227227
type_hint=field.type_hint,
228228
default=default, # type: ignore
229-
default_value=field.default,
229+
default_value=represented_default, # type: ignore
230230
required=field.required,
231231
)
232232

poetry.lock

Lines changed: 22 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fastapi-codegen = "fastapi_code_generator.__main__:app"
2828
[tool.poetry.dependencies]
2929
python = "^3.8.0"
3030
typer = {extras = ["all"], version = "^0.2.1"}
31-
datamodel-code-generator = "^0.5.9"
31+
datamodel-code-generator = "==0.5.10"
3232
stringcase = "^1.2.0"
3333
black = "19.10b0"
3434
isort = "^4.3.21"

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414

1515

1616
@app.get('/pets', response_model=List[Pet])
17-
def list_pets(limit: Optional[int] = 0) -> List[Pet]:
17+
def list_pets(
18+
limit: Optional[int] = 0,
19+
home_address: Optional[str] = Query('Unknown', alias='HomeAddress'),
20+
kind: Optional[str] = 'dog',
21+
) -> List[Pet]:
1822
pass
1923

2024

tests/data/openapi/body_and_parameters.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ paths:
2222
default: 0
2323
type: integer
2424
format: int32
25+
- name: HomeAddress
26+
in: query
27+
required: false
28+
schema:
29+
default: 'Unknown'
30+
type: string
31+
- name: kind
32+
in: query
33+
required: false
34+
schema:
35+
default: dog
36+
type: string
2537
responses:
2638
'200':
2739
description: A paged array of pets

0 commit comments

Comments
 (0)