Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix invalid query parameter #21

Merged
merged 5 commits into from
Jun 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions fastapi_code_generator/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def __str__(self) -> str:

@cached_property
def argument(self) -> str:
if not self.default and self.required:
if self.default is None and self.required:
return f'{self.name}: {self.type_hint}'
return f'{self.name}: {self.type_hint} = {self.default}'

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

@cached_property
Expand Down Expand Up @@ -216,17 +217,18 @@ def get_parameter_type(
required=parameter.get("required") or parameter.get("in") == "path",
)
self.imports.extend(field.imports)
represented_default = repr(schema.default)
if orig_name != name:
default = f"Query({'...' if field.required else represented_default}, alias='{orig_name}')"
default: Optional[
str
] = f"Query({'...' if field.required else repr(schema.default)}, alias='{orig_name}')"
self.imports.append(Import(from_='fastapi', import_='Query'))
else:
default = represented_default
default = repr(schema.default) if 'default' in parameter["schema"] else None
return Argument(
name=field.name,
type_hint=field.type_hint,
default=default, # type: ignore
default_value=represented_default, # type: ignore
default_value=schema.default,
required=field.required,
)

Expand Down
13 changes: 9 additions & 4 deletions tests/data/expected/openapi/body_and_parameters/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@

from fastapi import FastAPI, Query

from .models import Pet, PetForm, Pets
from .models import Pet, PetForm

app = FastAPI()


@app.get('/food/{food_id}', response_model=None)
def show_food_by_id(food_id: str) -> None:
pass


@app.get('/pets', response_model=List[Pet])
def list_pets(
limit: Optional[int] = 0,
Expand All @@ -27,13 +32,13 @@ def post_pets(body: PetForm) -> None:
pass


@app.get('/pets/{pet_id}', response_model=Pets)
def show_pet_by_id(pet_id: str = Query(..., alias='petId')) -> Pets:
@app.get('/pets/{pet_id}', response_model=Pet)
def show_pet_by_id(pet_id: str = Query(..., alias='petId')) -> Pet:
pass


@app.put('/pets/{pet_id}', response_model=None)
def put_pets__pet_id_(
def put_pets_pet_id(
pet_id: str = Query(..., alias='petId'), body: PetForm = None
) -> None:
pass
26 changes: 24 additions & 2 deletions tests/data/openapi/body_and_parameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ paths:
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/Pet"
- $ref: "#/components/schemas/Pet"
default:
description: unexpected error
content:
Expand Down Expand Up @@ -91,7 +92,7 @@ paths:
content:
application/json:
schema:
$ref: "#/components/schemas/Pets"
$ref: "#/components/schemas/Pet"
default:
description: unexpected error
content:
Expand Down Expand Up @@ -130,6 +131,27 @@ paths:
passthroughBehavior: when_no_templates
httpMethod: POST
type: aws_proxy
/food/{food_id}:
get:
summary: Info for a specific pet
operationId: showFoodById
tags:
- foods
parameters:
- name: food_id
in: path
description: The id of the food to retrieve
schema:
type: string
responses:
'200':
description: Expected response to a valid request
x-amazon-apigateway-integration:
uri:
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${PythonVersionFunction.Arn}/invocations
passthroughBehavior: when_no_templates
httpMethod: POST
type: aws_proxy
components:
schemas:
Pet:
Expand Down