Skip to content

Commit

Permalink
Merge pull request #20 from koxudaxi/fix_default
Browse files Browse the repository at this point in the history
fix default
  • Loading branch information
koxudaxi authored Jun 20, 2020
2 parents 983c0bc + bad3088 commit 20ac3ae
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 28 deletions.
8 changes: 4 additions & 4 deletions fastapi_code_generator/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,19 +214,19 @@ def get_parameter_type(
name=name,
data_types=[type_map[type_]],
required=parameter.get("required") or parameter.get("in") == "path",
default=schema.typed_default,
)
self.imports.extend(field.imports)
represented_default = repr(schema.default)
if orig_name != name:
default = f"Query({'...' if field.required else field.default}, alias='{orig_name}')"
default = f"Query({'...' if field.required else represented_default}, alias='{orig_name}')"
self.imports.append(Import(from_='fastapi', import_='Query'))
else:
default = field.default
default = represented_default
return Argument(
name=field.name,
type_hint=field.type_hint,
default=default, # type: ignore
default_value=field.default,
default_value=represented_default, # type: ignore
required=field.required,
)

Expand Down
44 changes: 22 additions & 22 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fastapi-codegen = "fastapi_code_generator.__main__:app"
[tool.poetry.dependencies]
python = "^3.8.0"
typer = {extras = ["all"], version = "^0.2.1"}
datamodel-code-generator = "^0.5.9"
datamodel-code-generator = "==0.5.10"
stringcase = "^1.2.0"
black = "19.10b0"
isort = "^4.3.21"
Expand Down
6 changes: 5 additions & 1 deletion tests/data/expected/openapi/body_and_parameters/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@


@app.get('/pets', response_model=List[Pet])
def list_pets(limit: Optional[int] = 0) -> List[Pet]:
def list_pets(
limit: Optional[int] = 0,
home_address: Optional[str] = Query('Unknown', alias='HomeAddress'),
kind: Optional[str] = 'dog',
) -> List[Pet]:
pass


Expand Down
12 changes: 12 additions & 0 deletions tests/data/openapi/body_and_parameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ paths:
default: 0
type: integer
format: int32
- name: HomeAddress
in: query
required: false
schema:
default: 'Unknown'
type: string
- name: kind
in: query
required: false
schema:
default: dog
type: string
responses:
'200':
description: A paged array of pets
Expand Down

0 comments on commit 20ac3ae

Please sign in to comment.