Skip to content

Commit b35b00f

Browse files
committed
Merge main into formatter coverage
2 parents ea0f245 + 3bf3c02 commit b35b00f

28 files changed

Lines changed: 299 additions & 8 deletions

File tree

docs/llms-full.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ Run `tox run -e schema-docs` after changing supported inputs or model backends.
553553

554554
| Format | Status | Evidence | Notes |
555555
|--------|--------|----------|-------|
556-
| OpenAPI YAML | tested | `tests/data/openapi/**/*.yaml` (34 fixtures) | Primary fixture format exercised under `tests/data/openapi/**/*.yaml`. |
556+
| OpenAPI YAML | tested | `tests/data/openapi/**/*.yaml` (39 fixtures) | Primary fixture format exercised under `tests/data/openapi/**/*.yaml`. |
557557
| OpenAPI JSON | tested | `tests/main/test_main.py::test_generate_from_json_input` | Covered by the JSON conversion CLI test in `tests/main/test_main.py`. |
558558
| Remote HTTP `$ref` targets | tested | `tests/main/test_main.py::test_generate_remote_ref` | Covered by the remote `$ref` generation test against a live HTTP server. |
559559

@@ -562,7 +562,7 @@ Run `tox run -e schema-docs` after changing supported inputs or model backends.
562562
| Suite | Fixtures | Example files | Notes |
563563
|-------|----------|---------------|-------|
564564
| Default template | 20 | `body_and_parameters.yaml`, `content_in_parameters.yaml`, `content_in_parameters_inline.yaml` | Core single-file generation scenarios exercised by the main CLI tests. |
565-
| Coverage fixtures | 9 | `callbacks.yaml`, `callbacks_with_operation_id.yaml`, `faux_immutability.yaml` | Focused fixtures for callbacks, non-200 responses, and other regression edges. |
565+
| Coverage fixtures | 14 | `callbacks.yaml`, `callbacks_with_operation_id.yaml`, `faux_immutability.yaml` | Focused fixtures for callbacks, non-200 responses, and other regression edges. |
566566
| Custom template overrides | 1 | `custom_security.yaml` | Template override coverage for `--template-dir`. |
567567
| Timestamp suppression | 1 | `simple.yaml` | Fixtures that exercise `--disable-timestamp`. |
568568
| Remote references | 1 | `body_and_parameters.yaml` | Fixtures whose `$ref` targets are resolved over HTTP at test time. |

docs/supported_formats.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Run `tox run -e schema-docs` after changing supported inputs or model backends.
77

88
| Format | Status | Evidence | Notes |
99
|--------|--------|----------|-------|
10-
| OpenAPI YAML | tested | `tests/data/openapi/**/*.yaml` (34 fixtures) | Primary fixture format exercised under `tests/data/openapi/**/*.yaml`. |
10+
| OpenAPI YAML | tested | `tests/data/openapi/**/*.yaml` (39 fixtures) | Primary fixture format exercised under `tests/data/openapi/**/*.yaml`. |
1111
| OpenAPI JSON | tested | `tests/main/test_main.py::test_generate_from_json_input` | Covered by the JSON conversion CLI test in `tests/main/test_main.py`. |
1212
| Remote HTTP `$ref` targets | tested | `tests/main/test_main.py::test_generate_remote_ref` | Covered by the remote `$ref` generation test against a live HTTP server. |
1313

@@ -16,7 +16,7 @@ Run `tox run -e schema-docs` after changing supported inputs or model backends.
1616
| Suite | Fixtures | Example files | Notes |
1717
|-------|----------|---------------|-------|
1818
| Default template | 20 | `body_and_parameters.yaml`, `content_in_parameters.yaml`, `content_in_parameters_inline.yaml` | Core single-file generation scenarios exercised by the main CLI tests. |
19-
| Coverage fixtures | 9 | `callbacks.yaml`, `callbacks_with_operation_id.yaml`, `faux_immutability.yaml` | Focused fixtures for callbacks, non-200 responses, and other regression edges. |
19+
| Coverage fixtures | 14 | `callbacks.yaml`, `callbacks_with_operation_id.yaml`, `faux_immutability.yaml` | Focused fixtures for callbacks, non-200 responses, and other regression edges. |
2020
| Custom template overrides | 1 | `custom_security.yaml` | Template override coverage for `--template-dir`. |
2121
| Timestamp suppression | 1 | `simple.yaml` | Fixtures that exercise `--disable-timestamp`. |
2222
| Remote references | 1 | `body_and_parameters.yaml` | Fixtures whose `$ref` targets are resolved over HTTP at test time. |

fastapi_code_generator/cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,8 @@ def generate_code(
334334

335335
for target in template_dir.rglob("*"):
336336
relative_path = target.relative_to(template_dir)
337+
if generate_routers and relative_path.name.startswith("routers."):
338+
continue
337339
template = environment.get_template(str(relative_path))
338340
result = template.render(template_vars)
339341
results[relative_path] = _normalize_pydantic_v2_code(

fastapi_code_generator/modular_template/routers.jinja2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ router = APIRouter(
1111
{% for operation in operations %}
1212
{% if operation.tags[0] == tag %}
1313
@router.{{operation.type}}('{{operation.path}}', response_model={{operation.response}}
14+
{% if operation.status_code %}
15+
, status_code={{operation.status_code}}
16+
{% endif %}
1417
{% if operation.additional_responses %}
1518
, responses={
1619
{% for status_code, models in operation.additional_responses.items() %}

fastapi_code_generator/parser.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ class Operation(CachedPropertyModel):
199199
tags: Optional[List[str]] = []
200200
request: Optional[Argument] = None
201201
response: str = ''
202+
status_code: Optional[int] = None
202203
additional_responses: Dict[Union[str, int], Dict[str, str]] = {}
203204
return_type: str = ''
204205
callbacks: Dict[UsefulStr, List["Operation"]] = {}
@@ -396,7 +397,7 @@ def parse_info(self) -> Optional[Dict[str, Any]]:
396397
info = self.raw_obj.get('info')
397398
if not isinstance(info, dict): # pragma: no cover
398399
return None
399-
result = info.copy()
400+
result = {key: value for key, value in info.items() if not key.startswith('x-')}
400401
servers = self.raw_obj.get('servers')
401402
if servers:
402403
result['servers'] = servers
@@ -727,6 +728,17 @@ def parse_responses( # type: ignore[override]
727728
data_type = DataType(type='None')
728729
type_hint = data_type.type_hint # TODO: change to lazy loading
729730
self._temporary_operation['response'] = type_hint
731+
success_status_codes = [
732+
int(status_code)
733+
for status_code in responses
734+
if str(status_code).isdigit() and 200 <= int(status_code) < 300
735+
]
736+
if '200' not in responses and success_status_codes:
737+
selected_status_code = min(success_status_codes)
738+
if selected_status_code == 204 and not data_types.get(
739+
str(selected_status_code)
740+
):
741+
self._temporary_operation['status_code'] = selected_status_code
730742
return_types = {type_hint: data_type}
731743
for status_code, additional_responses in data_types.items():
732744
if status_code != '200' and additional_responses: # 200 is processed above

fastapi_code_generator/prompt_data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@
532532
'\n'
533533
'| Format | Status | Evidence | Notes |\n'
534534
'|--------|--------|----------|-------|\n'
535-
'| OpenAPI YAML | tested | `tests/data/openapi/**/*.yaml` (34 '
535+
'| OpenAPI YAML | tested | `tests/data/openapi/**/*.yaml` (39 '
536536
'fixtures) | Primary fixture format exercised under '
537537
'`tests/data/openapi/**/*.yaml`. |\n'
538538
'| OpenAPI JSON | tested | '
@@ -552,7 +552,7 @@
552552
'`content_in_parameters.yaml`, '
553553
'`content_in_parameters_inline.yaml` | Core single-file '
554554
'generation scenarios exercised by the main CLI tests. |\n'
555-
'| Coverage fixtures | 9 | `callbacks.yaml`, '
555+
'| Coverage fixtures | 14 | `callbacks.yaml`, '
556556
'`callbacks_with_operation_id.yaml`, `faux_immutability.yaml` '
557557
'| Focused fixtures for callbacks, non-200 responses, and '
558558
'other regression edges. |\n'

fastapi_code_generator/template/main.jinja2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ app = FastAPI(
1616

1717
{% for operation in operations %}
1818
@app.{{operation.type}}('{{operation.path}}', response_model={{operation.response}}
19+
{% if operation.status_code %}
20+
, status_code={{operation.status_code}}
21+
{% endif %}
1922
{% if operation.additional_responses %}
2023
, responses={
2124
{% for status_code, models in operation.additional_responses.items() %}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from fastapi import FastAPI
2+
{% if routers %}
3+
from .routers import {{ routers|join(", ") }}
4+
{% endif %}
5+
6+
app = FastAPI()
7+
{% for router in routers %}
8+
app.include_router({{ router }}.router)
9+
{% endfor %}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from anymodule import {{ tag.strip() }}
2+
from fastapi import APIRouter
3+
4+
router = APIRouter(tags=[{{ tag.strip() }}.__name__])
5+
6+
7+
@router.get('/items', tags=[{{ tag.strip() }}.__name__])
8+
def get_items() -> None:
9+
pass
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# generated by fastapi-codegen:
2+
# filename: field_examples.yaml
3+
4+
from __future__ import annotations
5+
6+
from fastapi import FastAPI
7+
8+
from .models import StatusResponse
9+
10+
app = FastAPI(
11+
title='Example',
12+
version='1.0.0',
13+
)
14+
15+
16+
@app.get('/status', response_model=StatusResponse)
17+
def get_status() -> StatusResponse:
18+
pass

0 commit comments

Comments
 (0)