From 1e0ad45882a311a49fd5fe9c2b43421263c8bf97 Mon Sep 17 00:00:00 2001 From: Koudai Aono Date: Sun, 31 May 2026 14:16:05 +0000 Subject: [PATCH 1/3] Add formatter pyproject regression coverage --- tests/main/test_main.py | 59 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/tests/main/test_main.py b/tests/main/test_main.py index 4aa8f0dc..8909ea85 100644 --- a/tests/main/test_main.py +++ b/tests/main/test_main.py @@ -263,6 +263,65 @@ def test_generate_from_json_input(tmp_path: Path, output_dir: Path) -> None: validate_generated_code(output_dir) +@freeze_time("2020-06-19") +def test_generate_with_parent_pyproject_formatter_settings(tmp_path: Path) -> None: + project_dir = tmp_path / "project" + output_dir = project_dir / "app3" + input_path = project_dir / "api.yaml" + project_dir.mkdir() + project_dir.joinpath("pyproject.toml").write_text( + '[tool.poetry]\nname = "openapi-srv"\nversion = "0.1.0"\n', + encoding="utf-8", + ) + input_path.write_text( + """openapi: "3.0.0" +info: + version: 1.0.0 + title: Swagger Petstore +paths: + /pets: + get: + operationId: listPets + responses: + '200': + description: A paged array of pets + content: + application/json: + schema: + $ref: "#/components/schemas/Pets" +components: + schemas: + Pet: + required: + - id + - name + properties: + id: + type: integer + format: int64 + name: + type: string + Pets: + type: array + items: + $ref: "#/components/schemas/Pet" +""", + encoding="utf-8", + ) + + assert ( + run_main_with_args(["--input", str(input_path), "--output", str(output_dir)]) + == 0 + ) + + main_text = output_dir.joinpath("main.py").read_text(encoding="utf-8") + models_text = output_dir.joinpath("models.py").read_text(encoding="utf-8") + + assert "@app.get('/pets', response_model=List[Pet])" in main_text + assert "class Pet(BaseModel):" in models_text + validate_generated_code(output_dir) + + @freeze_time("2020-06-19") def test_generate_discriminated_union_with_simple_type(output_dir: Path) -> None: run_cli_and_assert( From 811ca431b67903893ac4d2377b9761915b1c5511 Mon Sep 17 00:00:00 2001 From: Koudai Aono Date: Sun, 31 May 2026 14:24:44 +0000 Subject: [PATCH 2/3] Move formatter regression to fixtures --- docs/supported_formats.md | 2 +- .../formatter_parent_pyproject/main.py | 20 +++++++ .../formatter_parent_pyproject/models.py | 11 ++++ .../formatter_parent_pyproject/api.yaml | 31 ++++++++++ .../formatter_parent_pyproject/pyproject.toml | 3 + tests/main/test_main.py | 57 +++---------------- 6 files changed, 74 insertions(+), 50 deletions(-) create mode 100644 tests/data/expected/openapi/coverage/formatter_parent_pyproject/main.py create mode 100644 tests/data/expected/openapi/coverage/formatter_parent_pyproject/models.py create mode 100644 tests/data/openapi/coverage/formatter_parent_pyproject/api.yaml create mode 100644 tests/data/openapi/coverage/formatter_parent_pyproject/pyproject.toml diff --git a/docs/supported_formats.md b/docs/supported_formats.md index 83e1afb5..8d34a160 100644 --- a/docs/supported_formats.md +++ b/docs/supported_formats.md @@ -7,7 +7,7 @@ Run `tox run -e schema-docs` after changing supported inputs or model backends. | Format | Status | Evidence | Notes | |--------|--------|----------|-------| -| OpenAPI YAML | tested | `tests/data/openapi/**/*.yaml` (28 fixtures) | Primary fixture format exercised under `tests/data/openapi/**/*.yaml`. | +| OpenAPI YAML | tested | `tests/data/openapi/**/*.yaml` (29 fixtures) | Primary fixture format exercised under `tests/data/openapi/**/*.yaml`. | | 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`. | | 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. | diff --git a/tests/data/expected/openapi/coverage/formatter_parent_pyproject/main.py b/tests/data/expected/openapi/coverage/formatter_parent_pyproject/main.py new file mode 100644 index 00000000..3763b7a0 --- /dev/null +++ b/tests/data/expected/openapi/coverage/formatter_parent_pyproject/main.py @@ -0,0 +1,20 @@ +# generated by fastapi-codegen: +# filename: api.yaml + +from __future__ import annotations + +from typing import List + +from fastapi import FastAPI + +from .models import Pet + +app = FastAPI( + version='1.0.0', + title='Swagger Petstore', +) + + +@app.get('/pets', response_model=List[Pet]) +def list_pets() -> List[Pet]: + pass diff --git a/tests/data/expected/openapi/coverage/formatter_parent_pyproject/models.py b/tests/data/expected/openapi/coverage/formatter_parent_pyproject/models.py new file mode 100644 index 00000000..a38b4480 --- /dev/null +++ b/tests/data/expected/openapi/coverage/formatter_parent_pyproject/models.py @@ -0,0 +1,11 @@ +# generated by fastapi-codegen: +# filename: api.yaml + +from __future__ import annotations + +from pydantic import BaseModel + + +class Pet(BaseModel): + id: int + name: str diff --git a/tests/data/openapi/coverage/formatter_parent_pyproject/api.yaml b/tests/data/openapi/coverage/formatter_parent_pyproject/api.yaml new file mode 100644 index 00000000..34981329 --- /dev/null +++ b/tests/data/openapi/coverage/formatter_parent_pyproject/api.yaml @@ -0,0 +1,31 @@ +openapi: "3.0.0" +info: + version: 1.0.0 + title: Swagger Petstore +paths: + /pets: + get: + operationId: listPets + responses: + '200': + description: A paged array of pets + content: + application/json: + schema: + $ref: "#/components/schemas/Pets" +components: + schemas: + Pet: + required: + - id + - name + properties: + id: + type: integer + format: int64 + name: + type: string + Pets: + type: array + items: + $ref: "#/components/schemas/Pet" diff --git a/tests/data/openapi/coverage/formatter_parent_pyproject/pyproject.toml b/tests/data/openapi/coverage/formatter_parent_pyproject/pyproject.toml new file mode 100644 index 00000000..9a1a30ab --- /dev/null +++ b/tests/data/openapi/coverage/formatter_parent_pyproject/pyproject.toml @@ -0,0 +1,3 @@ +[tool.poetry] +name = "openapi-srv" +version = "0.1.0" diff --git a/tests/main/test_main.py b/tests/main/test_main.py index 8909ea85..d3785062 100644 --- a/tests/main/test_main.py +++ b/tests/main/test_main.py @@ -265,62 +265,21 @@ def test_generate_from_json_input(tmp_path: Path, output_dir: Path) -> None: @freeze_time("2020-06-19") def test_generate_with_parent_pyproject_formatter_settings(tmp_path: Path) -> None: + fixture_dir = DATA_PATH / OPEN_API_COVERAGE_DIR_NAME / "formatter_parent_pyproject" project_dir = tmp_path / "project" output_dir = project_dir / "app3" input_path = project_dir / "api.yaml" project_dir.mkdir() - project_dir.joinpath("pyproject.toml").write_text( - '[tool.poetry]\nname = "openapi-srv"\nversion = "0.1.0"\n', - encoding="utf-8", - ) - input_path.write_text( - """openapi: "3.0.0" -info: - version: 1.0.0 - title: Swagger Petstore -paths: - /pets: - get: - operationId: listPets - responses: - '200': - description: A paged array of pets - content: - application/json: - schema: - $ref: "#/components/schemas/Pets" -components: - schemas: - Pet: - required: - - id - - name - properties: - id: - type: integer - format: int64 - name: - type: string - Pets: - type: array - items: - $ref: "#/components/schemas/Pet" -""", - encoding="utf-8", - ) + copy2(fixture_dir / "api.yaml", input_path) + copy2(fixture_dir / "pyproject.toml", project_dir / "pyproject.toml") - assert ( - run_main_with_args(["--input", str(input_path), "--output", str(output_dir)]) - == 0 + run_cli_and_assert( + input_path=input_path, + output_path=output_dir, + expected_path=EXPECTED_OPENAPI_PATH / "coverage" / "formatter_parent_pyproject", + extra_args=["--disable-timestamp"], ) - main_text = output_dir.joinpath("main.py").read_text(encoding="utf-8") - models_text = output_dir.joinpath("models.py").read_text(encoding="utf-8") - - assert "@app.get('/pets', response_model=List[Pet])" in main_text - assert "class Pet(BaseModel):" in models_text - validate_generated_code(output_dir) - @freeze_time("2020-06-19") def test_generate_discriminated_union_with_simple_type(output_dir: Path) -> None: From 14fb2158adf7cbece7370d6d6b16bfd77b9963d4 Mon Sep 17 00:00:00 2001 From: Koudai Aono Date: Sun, 31 May 2026 14:31:33 +0000 Subject: [PATCH 3/3] Update generated prompt docs --- docs/llms-full.txt | 2 +- fastapi_code_generator/prompt_data.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/llms-full.txt b/docs/llms-full.txt index 52274c93..597d4d5d 100644 --- a/docs/llms-full.txt +++ b/docs/llms-full.txt @@ -553,7 +553,7 @@ Run `tox run -e schema-docs` after changing supported inputs or model backends. | Format | Status | Evidence | Notes | |--------|--------|----------|-------| -| OpenAPI YAML | tested | `tests/data/openapi/**/*.yaml` (28 fixtures) | Primary fixture format exercised under `tests/data/openapi/**/*.yaml`. | +| OpenAPI YAML | tested | `tests/data/openapi/**/*.yaml` (29 fixtures) | Primary fixture format exercised under `tests/data/openapi/**/*.yaml`. | | 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`. | | 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. | diff --git a/fastapi_code_generator/prompt_data.py b/fastapi_code_generator/prompt_data.py index 8ced0773..3d5c7cca 100644 --- a/fastapi_code_generator/prompt_data.py +++ b/fastapi_code_generator/prompt_data.py @@ -532,7 +532,7 @@ '\n' '| Format | Status | Evidence | Notes |\n' '|--------|--------|----------|-------|\n' - '| OpenAPI YAML | tested | `tests/data/openapi/**/*.yaml` (28 ' + '| OpenAPI YAML | tested | `tests/data/openapi/**/*.yaml` (29 ' 'fixtures) | Primary fixture format exercised under ' '`tests/data/openapi/**/*.yaml`. |\n' '| OpenAPI JSON | tested | '