Skip to content

Commit

Permalink
Merge pull request #9 from koxudaxi/fix_default_arguments
Browse files Browse the repository at this point in the history
fix default arguments
  • Loading branch information
koxudaxi authored Jun 17, 2020
2 parents 5fbaca8 + 13e6ce0 commit 628e8c3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
1 change: 0 additions & 1 deletion fastapi_code_generator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def main(
template_dir if template_dir else f"{Path(__file__).parent}/template",
encoding="utf8",
),
autoescape=True,
)
results: Dict[Path, str] = {}
for target in template_dir.rglob("*"):
Expand Down
9 changes: 6 additions & 3 deletions fastapi_code_generator/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,9 @@ def argument_list(self) -> List[Argument]:
def get_parameter_type(
self, parameter: Dict[str, Union[str, Dict[str, str]]], snake_case: bool
) -> str:
format_ = parameter["schema"].get("format", "default") # type: ignore
type_ = json_schema_data_formats[parameter["schema"]["type"]][format_] # type: ignore
schema: JsonSchemaObject = JsonSchemaObject.parse_obj(parameter["schema"])
format_ = schema.format or "default"
type_ = json_schema_data_formats[schema.type][format_]
return self.get_data_type_hint(
name=stringcase.snakecase(parameter["name"])
if snake_case
Expand All @@ -208,6 +209,7 @@ def get_parameter_type(
required=parameter.get("required") == "true"
or parameter.get("in") == "path",
snake_case=snake_case,
default=schema.typed_default,
)

def get_data_type_hint(
Expand All @@ -230,7 +232,8 @@ def get_data_type_hint(

if not default and field.required:
return f"{field.name}: {field.type_hint}"
return f'{field.name}: {field.type_hint} = {default or "None"}'

return f'{field.name}: {field.type_hint} = {default}'

@cached_property
def response(self) -> str:
Expand Down
20 changes: 10 additions & 10 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.5"
datamodel-code-generator = "^0.5.8"
stringcase = "^1.2.0"
black = "19.10b0"
isort = "^4.3.21"
Expand Down

0 comments on commit 628e8c3

Please sign in to comment.