Skip to content

Commit ed2de6f

Browse files
Add warning when components/schemas is empty but paths exist (#2622)
* Add warning for empty schemas when paths are defined * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Simplify warning handling for empty schemas in test cases --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 27d5f07 commit ed2de6f

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ filterwarnings = [
208208
"ignore:^.*black doesn't support `experimental-string-processing` option for wrapping string literal in .*",
209209
"ignore:^.*jsonschema.exceptions.RefResolutionError is deprecated as of version 4.18.0. If you wish to catch potential reference resolution errors, directly catch referencing.exceptions.Unresolvable..*",
210210
"ignore:^.*`experimental string processing` has been included in `preview` and deprecated. Use `preview` instead..*",
211+
"ignore:^.*No schemas found in components/schemas.*",
211212
]
212213
norecursedirs = "tests/data/*"
213214
verbosity_assertions = 2

src/datamodel_code_generator/parser/openapi.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,15 @@ def parse_raw(self) -> None: # noqa: PLR0912, PLR0914, PLR0915
719719
self.raw_obj = specification
720720
self._collect_discriminator_schemas()
721721
schemas: dict[str, Any] = specification.get("components", {}).get("schemas", {})
722+
paths: dict[str, Any] = specification.get("paths", {})
722723
security: list[dict[str, list[str]]] | None = specification.get("security")
724+
# Warn if schemas is empty but paths exist and only Schemas scope is used
725+
if not schemas and self.open_api_scopes == [OpenAPIScope.Schemas] and paths:
726+
warn(
727+
"No schemas found in components/schemas. If your schemas are defined in "
728+
"external files referenced from paths, consider using --openapi-scopes paths",
729+
stacklevel=2,
730+
)
723731
if OpenAPIScope.Schemas in self.open_api_scopes:
724732
for obj_name, raw_obj in schemas.items():
725733
self.parse_raw_obj(
@@ -728,7 +736,6 @@ def parse_raw(self) -> None: # noqa: PLR0912, PLR0914, PLR0915
728736
[*path_parts, "#/components", "schemas", obj_name],
729737
)
730738
if OpenAPIScope.Paths in self.open_api_scopes:
731-
paths: dict[str, Any] = specification.get("paths", {})
732739
parameters: list[dict[str, Any]] = [
733740
self._get_ref_body(p["$ref"]) if "$ref" in p else p
734741
for p in paths.get("parameters", [])

tests/main/openapi/test_main_openapi.py

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

33
from __future__ import annotations
44

5+
import contextlib
56
import json
67
import platform
78
import warnings
@@ -3242,3 +3243,23 @@ def test_main_openapi_circular_imports_mixed_prefixes(output_dir: Path) -> None:
32423243
expected_directory=EXPECTED_OPENAPI_PATH / "circular_imports_mixed_prefixes",
32433244
input_file_type="openapi",
32443245
)
3246+
3247+
3248+
def test_warning_empty_schemas_with_paths(tmp_path: Path) -> None:
3249+
"""Test warning when components/schemas is empty but paths exist."""
3250+
openapi_file = tmp_path / "openapi.yaml"
3251+
openapi_file.write_text("""
3252+
openapi: 3.1.0
3253+
info:
3254+
title: Test
3255+
version: '1'
3256+
paths:
3257+
/test:
3258+
get:
3259+
responses:
3260+
200:
3261+
description: OK
3262+
""")
3263+
3264+
with pytest.warns(UserWarning, match=r"No schemas found.*--openapi-scopes paths"), contextlib.suppress(Exception):
3265+
generate(openapi_file)

0 commit comments

Comments
 (0)