Skip to content

Commit f7d2863

Browse files
committed
Fix header and cookie parameter generation
1 parent 6132045 commit f7d2863

8 files changed

Lines changed: 85 additions & 6 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` (39 fixtures) | Primary fixture format exercised under `tests/data/openapi/**/*.yaml`. |
556+
| OpenAPI YAML | tested | `tests/data/openapi/**/*.yaml` (40 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 | 14 | `callbacks.yaml`, `callbacks_with_operation_id.yaml`, `faux_immutability.yaml` | Focused fixtures for callbacks, non-200 responses, and other regression edges. |
565+
| Coverage fixtures | 15 | `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` (39 fixtures) | Primary fixture format exercised under `tests/data/openapi/**/*.yaml`. |
10+
| OpenAPI YAML | tested | `tests/data/openapi/**/*.yaml` (40 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 | 14 | `callbacks.yaml`, `callbacks_with_operation_id.yaml`, `faux_immutability.yaml` | Focused fixtures for callbacks, non-200 responses, and other regression edges. |
19+
| Coverage fixtures | 15 | `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/parser.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,16 @@ def get_parameter_type(
466466
default_value = '...' if field.required else repr(schema.default)
467467
alias = f", alias={orig_name!r}" if orig_name != name else ''
468468
default = f"Query({default_value}{alias})"
469+
elif parameter_location in (ParameterLocation.header, ParameterLocation.cookie):
470+
assert parameter_location is not None # pragma: no cover
471+
param_is = parameter_location.value.lower().capitalize()
472+
self.imports_for_fastapi.append(Import(from_='fastapi', import_=param_is))
473+
default_value = '...' if field.required else repr(schema.default)
474+
needs_alias = orig_name != name or (
475+
parameter_location == ParameterLocation.header and "_" in orig_name
476+
)
477+
alias = f", alias={orig_name!r}" if needs_alias else ''
478+
default = f"{param_is}({default_value}{alias})"
469479
elif orig_name != name:
470480
assert parameter_location is not None # pragma: no cover
471481
param_is = parameter_location.value.lower().capitalize()

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` (39 '
535+
'| OpenAPI YAML | tested | `tests/data/openapi/**/*.yaml` (40 '
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 | 14 | `callbacks.yaml`, '
555+
'| Coverage fixtures | 15 | `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'
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# generated by fastapi-codegen:
2+
# filename: header_cookie_parameters.yaml
3+
4+
from __future__ import annotations
5+
6+
from typing import Optional
7+
8+
from fastapi import Cookie, FastAPI, Header
9+
10+
app = FastAPI(
11+
title='Parameter API',
12+
version='1.0.0',
13+
)
14+
15+
16+
@app.get('/items/{item_id}', response_model=None)
17+
def get_item(
18+
item_id: int,
19+
limit: Optional[int] = None,
20+
trace_id: str = Header(..., alias='trace_id'),
21+
session_id: Optional[str] = Cookie(None),
22+
) -> None:
23+
pass
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# generated by fastapi-codegen:
2+
# filename: header_cookie_parameters.yaml
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Parameter API
4+
version: 1.0.0
5+
paths:
6+
/items/{item_id}:
7+
get:
8+
operationId: getItem
9+
parameters:
10+
- name: item_id
11+
in: path
12+
required: true
13+
schema:
14+
type: integer
15+
- name: limit
16+
in: query
17+
required: false
18+
schema:
19+
type: integer
20+
- name: trace_id
21+
in: header
22+
required: true
23+
schema:
24+
type: string
25+
- name: session_id
26+
in: cookie
27+
required: false
28+
schema:
29+
type: string
30+
responses:
31+
"200":
32+
description: OK

tests/main/test_main.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,18 @@ def test_generate_non_200_success_status_code(output_dir: Path) -> None:
899899
)
900900

901901

902+
@freeze_time("2020-06-19")
903+
def test_generate_header_and_cookie_parameters(output_dir: Path) -> None:
904+
run_cli_and_assert(
905+
input_path=DATA_PATH
906+
/ OPEN_API_COVERAGE_DIR_NAME
907+
/ "header_cookie_parameters.yaml",
908+
output_path=output_dir,
909+
expected_path=EXPECTED_OPENAPI_PATH / "coverage" / "header_cookie_parameters",
910+
extra_args=["--disable-timestamp"],
911+
)
912+
913+
902914
@freeze_time("2020-06-19")
903915
def test_generate_union_request_body_imports(output_dir: Path) -> None:
904916
run_cli_and_assert(

0 commit comments

Comments
 (0)