Skip to content

Commit

Permalink
Merge pull request #309 from n0nvme/add-tags
Browse files Browse the repository at this point in the history
add tags to default template
  • Loading branch information
koxudaxi authored Jan 16, 2023
2 parents bb92f98 + 3a1c3ff commit 958ef86
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 27 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ You can use the following variables in the jinja2 templates
- `operation.snake_case_arguments` Snake-cased function arguments
- `operation.security` [Security](https://swagger.io/docs/specification/authentication/)
- `operation.summary` a summary
- `operation.tags` [Tags](https://swagger.io/docs/specification/grouping-operations-with-tags/)

### default template
`main.jinja2`
Expand Down
3 changes: 3 additions & 0 deletions fastapi_code_generator/template/main.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ app = FastAPI(
}{% if not loop.last %},{% endif %}
{% endfor %}
}
{% endif %}
{% if operation.tags %}
, tags={{operation.tags}}
{% endif %})
def {{operation.function_name}}({{operation.snake_case_arguments}}) -> {{operation.return_type}}:
{%- if operation.summary %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,30 @@
)


@app.post('/bar', response_model=None)
@app.post('/bar', response_model=None, tags=['bar'])
def post_bar(request: Request) -> None:
"""
Create a bar
"""
pass


@app.get('/foo', response_model=str)
@app.get('/foo', response_model=str, tags=['foo'])
def get_foo(foo: Optional[str] = None) -> str:
pass


@app.post('/food', response_model=None, responses={'default': {'model': str}})
@app.post(
'/food', response_model=None, responses={'default': {'model': str}}, tags=['pets']
)
def post_food(body: str) -> Union[None, str]:
"""
Create a food
"""
pass


@app.get('/food/{food_id}', response_model=List[int])
@app.get('/food/{food_id}', response_model=List[int], tags=['foods'])
def show_food_by_id(
food_id: str, message_texts: Optional[List[str]] = None
) -> List[int]:
Expand All @@ -59,7 +61,12 @@ def show_food_by_id(
pass


@app.get('/pets', response_model=List[Pet], responses={'default': {'model': Error}})
@app.get(
'/pets',
response_model=List[Pet],
responses={'default': {'model': Error}},
tags=['pets'],
)
def list_pets(
limit: Optional[int] = 0,
home_address: Optional[str] = Query('Unknown', alias='HomeAddress'),
Expand All @@ -71,23 +78,35 @@ def list_pets(
pass


@app.post('/pets', response_model=None, responses={'default': {'model': Error}})
@app.post(
'/pets', response_model=None, responses={'default': {'model': Error}}, tags=['pets']
)
def post_pets(body: PetForm) -> Union[None, Error]:
"""
Create a pet
"""
pass


@app.get('/pets/{pet_id}', response_model=Pet, responses={'default': {'model': Error}})
@app.get(
'/pets/{pet_id}',
response_model=Pet,
responses={'default': {'model': Error}},
tags=['pets'],
)
def show_pet_by_id(pet_id: str = Path(..., alias='petId')) -> Union[Pet, Error]:
"""
Info for a specific pet
"""
pass


@app.put('/pets/{pet_id}', response_model=None, responses={'default': {'model': Error}})
@app.put(
'/pets/{pet_id}',
response_model=None,
responses={'default': {'model': Error}},
tags=['pets'],
)
def put_pets_pet_id(
pet_id: str = Path(..., alias='petId'), body: PetForm = None
) -> Union[None, Error]:
Expand All @@ -97,27 +116,29 @@ def put_pets_pet_id(
pass


@app.get('/user', response_model=UserGetResponse)
@app.get('/user', response_model=UserGetResponse, tags=['user'])
def get_user() -> UserGetResponse:
pass


@app.post('/user', response_model=None)
@app.post('/user', response_model=None, tags=['user'])
def post_user(body: UserPostRequest) -> None:
pass


@app.get('/users', response_model=List[UsersGetResponse])
@app.get('/users', response_model=List[UsersGetResponse], tags=['user'])
def get_users() -> List[UsersGetResponse]:
pass


@app.post('/users', response_model=None)
@app.post('/users', response_model=None, tags=['user'])
def post_users(body: List[UsersPostRequest]) -> None:
pass


@app.post('/{ue_id}/sdm-subscriptions', response_model=None)
@app.post(
'/{ue_id}/sdm-subscriptions', response_model=None, tags=['Subscription Creation']
)
def subscribe(ue_id: str = Path(..., alias='ueId'), body: Pet = ...) -> None:
"""
subscribe to notifications
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
)


@app.get('/{supi_or_suci}/security-information-rg', response_model=None)
@app.get(
'/{supi_or_suci}/security-information-rg',
response_model=None,
tags=['Get Auth Data for FN-RG'],
)
def get_rg_auth_data(plmn_id: Optional[Pet] = Query(None, alias='plmn-id')) -> None:
"""
Get authentication data for the FN-RG
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
)


@app.get('/pets', response_model=List[Pet], responses={'default': {'model': Error}})
@app.get(
'/pets',
response_model=List[Pet],
responses={'default': {'model': Error}},
tags=['pets'],
)
def list_pets(limit: Optional[int] = 0) -> Union[List[Pet], Error]:
"""
List all pets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@
)


@app.get('/pets', response_model=Pets, responses={'default': {'model': Error}})
@app.get(
'/pets', response_model=Pets, responses={'default': {'model': Error}}, tags=['pets']
)
def list_pets(limit: Optional[int] = None) -> Union[Pets, Error]:
"""
List all pets
"""
pass


@app.post('/pets', response_model=None, responses={'default': {'model': Error}})
@app.post(
'/pets', response_model=None, responses={'default': {'model': Error}}, tags=['pets']
)
def create_pets() -> Union[None, Error]:
"""
Create a pet
Expand All @@ -39,6 +43,7 @@ def create_pets() -> Union[None, Error]:
'/pets/{pet_id}',
response_model=Pets,
responses={'404': {'model': Error}, 'default': {'model': Error}},
tags=['pets'],
)
def show_pet_by_id(pet_id: str = Path(..., alias='petId')) -> Union[Pets, Error]:
"""
Expand Down
15 changes: 12 additions & 3 deletions tests/data/expected/openapi/default_template/simple/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,32 @@
)


@app.get('/pets', response_model=Pets, responses={'default': {'model': Error}})
@app.get(
'/pets', response_model=Pets, responses={'default': {'model': Error}}, tags=['pets']
)
def list_pets(limit: Optional[int] = None) -> Union[Pets, Error]:
"""
List all pets
"""
pass


@app.post('/pets', response_model=None, responses={'default': {'model': Error}})
@app.post(
'/pets', response_model=None, responses={'default': {'model': Error}}, tags=['pets']
)
def create_pets() -> Union[None, Error]:
"""
Create a pet
"""
pass


@app.get('/pets/{pet_id}', response_model=Pets, responses={'default': {'model': Error}})
@app.get(
'/pets/{pet_id}',
response_model=Pets,
responses={'default': {'model': Error}},
tags=['pets'],
)
def show_pet_by_id(pet_id: str = Path(..., alias='petId')) -> Union[Pets, Error]:
"""
Info for a specific pet
Expand Down
33 changes: 26 additions & 7 deletions tests/data/expected/openapi/remote_ref/body_and_parameters/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,22 @@
)


@app.get('/foo', response_model=str)
@app.get('/foo', response_model=str, tags=['foo'])
def get_foo(foo: Optional[str] = None) -> str:
pass


@app.post('/food', response_model=None, responses={'default': {'model': str}})
@app.post(
'/food', response_model=None, responses={'default': {'model': str}}, tags=['pets']
)
def post_food(body: str) -> Union[None, str]:
"""
Create a food
"""
pass


@app.get('/food/{food_id}', response_model=List[int])
@app.get('/food/{food_id}', response_model=List[int], tags=['foods'])
def show_food_by_id(
food_id: str, message_texts: Optional[List[str]] = None
) -> List[int]:
Expand All @@ -42,7 +44,12 @@ def show_food_by_id(
pass


@app.get('/pets', response_model=List[Pet], responses={'default': {'model': Error}})
@app.get(
'/pets',
response_model=List[Pet],
responses={'default': {'model': Error}},
tags=['pets'],
)
def list_pets(
limit: Optional[int] = 0,
home_address: Optional[str] = Query('Unknown', alias='HomeAddress'),
Expand All @@ -54,23 +61,35 @@ def list_pets(
pass


@app.post('/pets', response_model=None, responses={'default': {'model': Error}})
@app.post(
'/pets', response_model=None, responses={'default': {'model': Error}}, tags=['pets']
)
def post_pets(body: PetForm) -> Union[None, Error]:
"""
Create a pet
"""
pass


@app.get('/pets/{pet_id}', response_model=Pet, responses={'default': {'model': Error}})
@app.get(
'/pets/{pet_id}',
response_model=Pet,
responses={'default': {'model': Error}},
tags=['pets'],
)
def show_pet_by_id(pet_id: str = Path(..., alias='petId')) -> Union[Pet, Error]:
"""
Info for a specific pet
"""
pass


@app.put('/pets/{pet_id}', response_model=None, responses={'default': {'model': Error}})
@app.put(
'/pets/{pet_id}',
response_model=None,
responses={'default': {'model': Error}},
tags=['pets'],
)
def put_pets_pet_id(
pet_id: str = Path(..., alias='petId'), body: PetForm = None
) -> Union[None, Error]:
Expand Down

0 comments on commit 958ef86

Please sign in to comment.