Skip to content

Commit 2baf9e1

Browse files
authored
Merge pull request #162 from allen-munsch/patch-1
Add the various parameter `in` operators for query, path, header, bod…
2 parents 7d1eff0 + 382e0f7 commit 2baf9e1

File tree

5 files changed

+24
-15
lines changed
  • fastapi_code_generator
  • tests/data/expected/openapi

5 files changed

+24
-15
lines changed

fastapi_code_generator/parser.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,19 @@ def get_parameter_type(
304304
)
305305
self.imports.extend(field.imports)
306306
if orig_name != name:
307-
default: Optional[
308-
str
309-
] = f"Query({'...' if field.required else repr(schema.default)}, alias='{orig_name}')"
310-
self.imports.append(Import(from_='fastapi', import_='Query'))
307+
has_in = parameter.get('in')
308+
if has_in and isinstance(has_in, str):
309+
param_is = has_in.lower().capitalize()
310+
self.imports.append(Import(from_='fastapi', import_=param_is))
311+
default: Optional[
312+
str
313+
] = f"{param_is}({'...' if field.required else repr(schema.default)}, alias='{orig_name}')"
314+
else:
315+
# https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#parameterObject
316+
# the spec says 'in' is a str type
317+
raise TypeError(
318+
f'Issue processing parameter for "in", expected a str, but got something else: {str(parameter)}'
319+
)
311320
else:
312321
default = repr(schema.default) if schema.has_default else None
313322
return Argument(

tests/data/expected/openapi/custom_template_security/custom_security/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from pydantic import BaseModel
1010

11-
from fastapi import Depends, FastAPI, HTTPException, Query
11+
from fastapi import Depends, FastAPI, HTTPException, Path, Query
1212
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
1313
from starlette import status
1414

@@ -66,14 +66,14 @@ def post_pets(body: PetForm, user: User = Depends(valid_current_user)) -> None:
6666

6767
@app.get('/pets/{pet_id}', response_model=Pet, tags=['pets'])
6868
def show_pet_by_id(
69-
pet_id: str = Query(..., alias='petId'), user: User = Depends(valid_current_user)
69+
pet_id: str = Path(..., alias='petId'), user: User = Depends(valid_current_user)
7070
) -> Pet:
7171
pass
7272

7373

7474
@app.put('/pets/{pet_id}', response_model=None, tags=['pets'])
7575
def put_pets_pet_id(
76-
pet_id: str = Query(..., alias='petId'),
76+
pet_id: str = Path(..., alias='petId'),
7777
body: PetForm = None,
7878
user: User = Depends(valid_current_user),
7979
) -> None:

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from typing import List, Optional
88

9-
from fastapi import FastAPI, Query
9+
from fastapi import FastAPI, Path, Query
1010
from starlette.requests import Request
1111

1212
from .models import (
@@ -78,7 +78,7 @@ def post_pets(body: PetForm) -> None:
7878

7979

8080
@app.get('/pets/{pet_id}', response_model=Pet)
81-
def show_pet_by_id(pet_id: str = Query(..., alias='petId')) -> Pet:
81+
def show_pet_by_id(pet_id: str = Path(..., alias='petId')) -> Pet:
8282
"""
8383
Info for a specific pet
8484
"""
@@ -87,7 +87,7 @@ def show_pet_by_id(pet_id: str = Query(..., alias='petId')) -> Pet:
8787

8888
@app.put('/pets/{pet_id}', response_model=None)
8989
def put_pets_pet_id(
90-
pet_id: str = Query(..., alias='petId'), body: PetForm = None
90+
pet_id: str = Path(..., alias='petId'), body: PetForm = None
9191
) -> None:
9292
"""
9393
update a pet

tests/data/expected/openapi/default_template/simple/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from typing import Optional
88

9-
from fastapi import FastAPI, Query
9+
from fastapi import FastAPI, Path
1010

1111
from .models import Pets
1212

@@ -35,7 +35,7 @@ def create_pets() -> None:
3535

3636

3737
@app.get('/pets/{pet_id}', response_model=Pets)
38-
def show_pet_by_id(pet_id: str = Query(..., alias='petId')) -> Pets:
38+
def show_pet_by_id(pet_id: str = Path(..., alias='petId')) -> Pets:
3939
"""
4040
Info for a specific pet
4141
"""

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from typing import List, Optional
88

9-
from fastapi import FastAPI, Query
9+
from fastapi import FastAPI, Path, Query
1010

1111
from .models import Pet, PetForm
1212

@@ -62,7 +62,7 @@ def post_pets(body: PetForm) -> None:
6262

6363

6464
@app.get('/pets/{pet_id}', response_model=Pet)
65-
def show_pet_by_id(pet_id: str = Query(..., alias='petId')) -> Pet:
65+
def show_pet_by_id(pet_id: str = Path(..., alias='petId')) -> Pet:
6666
"""
6767
Info for a specific pet
6868
"""
@@ -71,7 +71,7 @@ def show_pet_by_id(pet_id: str = Query(..., alias='petId')) -> Pet:
7171

7272
@app.put('/pets/{pet_id}', response_model=None)
7373
def put_pets_pet_id(
74-
pet_id: str = Query(..., alias='petId'), body: PetForm = None
74+
pet_id: str = Path(..., alias='petId'), body: PetForm = None
7575
) -> None:
7676
"""
7777
update a pet

0 commit comments

Comments
 (0)