Skip to content

Commit

Permalink
Merge pull request #11 from koxudaxi/support_aliased_query_parameter
Browse files Browse the repository at this point in the history
Support aliased query parameter
  • Loading branch information
koxudaxi authored Jun 18, 2020
2 parents 0c7ef52 + aa4b7b2 commit ee6edcf
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions fastapi_code_generator/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
JsonSchemaObject,
json_schema_data_formats,
)
from datamodel_code_generator.types import DataType
from pydantic import BaseModel, root_validator

MODEL_PATH = ".models"
Expand Down Expand Up @@ -69,6 +68,7 @@ class Argument(CachedPropertyModel):
name: UsefulStr
type_hint: UsefulStr
default: Optional[UsefulStr]
default_value: Optional[UsefulStr]
required: bool

def __str__(self) -> str:
Expand Down Expand Up @@ -212,19 +212,28 @@ def get_parameter_type(
format_ = schema.format or "default"
type_ = json_schema_data_formats[schema.type][format_]
name: str = parameter["name"] # type: ignore
orig_name = name
if snake_case:
name = stringcase.snakecase(name)

field = DataModelField(
name=stringcase.snakecase(name) if snake_case else name,
name=name,
data_types=[type_map[type_]],
required=parameter.get("required") == "true"
or parameter.get("in") == "path",
default=schema.typed_default,
)
self.imports.extend(field.imports)
if orig_name != name:
default = f"Query({'...' if field.required else field.default}, alias='{orig_name}')"
self.imports.append(Import(from_='fastapi', import_='Query'))
else:
default = field.default
return Argument(
name=field.name,
type_hint=field.type_hint,
default=field.default,
default=default, # type: ignore
default_value=field.default,
required=field.required,
)

Expand Down Expand Up @@ -357,6 +366,7 @@ def __init__(self, parsed_operations: List[Operation]):
for operation in self.operations:
# create imports
operation.arguments
operation.snake_case_arguments
operation.request
operation.response
self.imports.append(operation.imports)
Expand Down

0 comments on commit ee6edcf

Please sign in to comment.