Skip to content

Add --openapi-include-paths option for path-based model filtering#2894

Merged
koxudaxi merged 2 commits intomainfrom
feature/openapi-include-paths
Jan 2, 2026
Merged

Add --openapi-include-paths option for path-based model filtering#2894
koxudaxi merged 2 commits intomainfrom
feature/openapi-include-paths

Conversation

@koxudaxi
Copy link
Copy Markdown
Owner

@koxudaxi koxudaxi commented Jan 2, 2026

Fixes: #2284

Summary by CodeRabbit

  • New Features

    • Added --openapi-include-paths CLI option to selectively include OpenAPI paths in model generation using fnmatch-style pattern matching. Includes validation to ensure the Paths scope is enabled.
  • Documentation

    • Updated CLI reference guides with comprehensive documentation and examples for the new --openapi-include-paths option.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jan 2, 2026

Warning

Rate limit exceeded

@koxudaxi has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 11 minutes and 44 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 240c575 and 7a9c5ce.

📒 Files selected for processing (16)
  • docs/cli-reference/index.md
  • docs/cli-reference/openapi-only-options.md
  • docs/cli-reference/quick-reference.md
  • src/datamodel_code_generator/__init__.py
  • src/datamodel_code_generator/__main__.py
  • src/datamodel_code_generator/_types/generate_config_dict.py
  • src/datamodel_code_generator/_types/parser_config_dicts.py
  • src/datamodel_code_generator/arguments.py
  • src/datamodel_code_generator/cli_options.py
  • src/datamodel_code_generator/config.py
  • src/datamodel_code_generator/parser/openapi.py
  • src/datamodel_code_generator/prompt_data.py
  • tests/data/expected/main/input_model/config_class.py
  • tests/data/expected/main/openapi/openapi_include_paths/pets_only.py
  • tests/main/openapi/test_main_openapi.py
  • tests/main/test_public_api_signature_baseline.py
📝 Walkthrough

Walkthrough

This PR introduces a new --openapi-include-paths CLI option that filters OpenAPI paths during code generation using fnmatch-style pattern matching. The option is propagated through configuration layers, implemented with path normalization and matching logic in the OpenAPI parser, and documented with test coverage including warning validation for missing scopes.

Changes

Cohort / File(s) Summary
Documentation
docs/cli-reference/index.md, docs/cli-reference/openapi-only-options.md, docs/cli-reference/quick-reference.md
Added new --openapi-include-paths option documentation with descriptions, usage examples, and OpenAPI schema demonstrations in reference guides.
Configuration & Type Definitions
src/datamodel_code_generator/__init__.py, src/datamodel_code_generator/__main__.py, src/datamodel_code_generator/config.py, src/datamodel_code_generator/_types/generate_config_dict.py, src/datamodel_code_generator/_types/parser_config_dicts.py
Added optional field openapi_include_paths: Optional[list[str]] = None across GenerateConfig and OpenAPIParserConfig classes with proper type annotations and propagation.
CLI Argument Registration
src/datamodel_code_generator/arguments.py, src/datamodel_code_generator/cli_options.py, src/datamodel_code_generator/prompt_data.py
Registered new --openapi-include-paths argument accepting multiple pattern strings with fnmatch-style matching; added to option metadata and descriptions.
Path Filtering Implementation
src/datamodel_code_generator/parser/openapi.py
Implemented path filtering with _normalize_path() and _matches_path_pattern() methods; added conditional path item processing via apply_path_filter parameter; includes initialization warning when used without paths scope enabled.
Test Coverage & Baselines
tests/main/openapi/test_main_openapi.py, tests/main/test_public_api_signature_baseline.py, tests/data/expected/main/openapi/openapi_include_paths/pets_only.py, tests/data/expected/main/input_model/config_class.py
Added three new test functions validating path filtering behavior with and without leading slashes and warning on missing paths scope; updated baseline signature and expected output models.

Sequence Diagram(s)

sequenceDiagram
    actor User
    participant CLI as CLI Parser
    participant Config as Config Layer
    participant OpenAPIParser as OpenAPI Parser
    participant PathFilter as Path Matching

    User->>CLI: Run with --openapi-include-paths /pets* /store*
    CLI->>Config: Store patterns in openapi_include_paths
    Config->>OpenAPIParser: Pass openapi_include_paths to parser
    
    alt Paths scope enabled
        OpenAPIParser->>OpenAPIParser: Store patterns, no warning
    else Paths scope not enabled
        OpenAPIParser->>User: ⚠️ Warning: include-paths only work with paths scope
    end
    
    Note over OpenAPIParser: During path processing
    loop For each OpenAPI path item
        OpenAPIParser->>PathFilter: _normalize_path(path)
        PathFilter-->>OpenAPIParser: normalized_path
        
        alt apply_path_filter = True
            OpenAPIParser->>PathFilter: _matches_path_pattern(normalized_path)
            PathFilter-->>OpenAPIParser: matches (bool)
            
            alt Pattern matches
                OpenAPIParser->>OpenAPIParser: Process path item → Generate model
            else Pattern does not match
                OpenAPIParser->>OpenAPIParser: Skip path item
            end
        else apply_path_filter = False (e.g., webhooks)
            OpenAPIParser->>OpenAPIParser: Process all items (no filtering)
        end
    end
    
    OpenAPIParser-->>User: Generated code with filtered paths
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested labels

safe-to-fix

Suggested reviewers

  • ilovelinux

Poem

🐰 A filter upon the paths I hop,
With fnmatch patterns, the filtering won't stop,
Through OpenAPI woods, I bound with grace,
Including only the pets I embrace!

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and accurately describes the main change: adding a new --openapi-include-paths CLI option for filtering OpenAPI paths during model generation.
Docstring Coverage ✅ Passed Docstring coverage is 92.31% which is sufficient. The required threshold is 80.00%.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 2, 2026

📚 Docs Preview: https://pr-2894.datamodel-code-generator.pages.dev

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Jan 2, 2026

CodSpeed Performance Report

Merging #2894 will not alter performance

Comparing feature/openapi-include-paths (7a9c5ce) with main (f4b240a)

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

Summary

✅ 11 untouched
⏩ 98 skipped1

Footnotes

  1. 98 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (3)
docs/cli-reference/openapi-only-options.md (1)

8-528: Docs look good; consider clarifying scope and pattern semantics

The new table row and --openapi-include-paths section are consistent with how other OpenAPI options are documented and match the new tests/golden output.

Two small clarity improvements would help users:

  • Explicitly state that --openapi-include-paths only has an effect when --openapi-scopes includes paths (there is a runtime warning for this, and your example already uses paths schemas).
  • Mention that patterns are matched using fnmatch-style globbing (e.g. /pets*, pets/*), to align the prose with the CLI help text.

These can be added in the short description or just below it without changing any behavior.

tests/main/openapi/test_main_openapi.py (1)

4728-4749: Warning test is solid; inner warnings import is optional

The warning assertion for using --openapi-include-paths without paths in --openapi-scopes is well structured and matches the intended message text.

Minor style nit: the function re-imports warnings locally even though the module imports it at the top. You can safely drop the inner import warnings and use the module-level import instead to keep things a bit cleaner.

src/datamodel_code_generator/__main__.py (1)

554-554: Remove unused noqa directive.

The # noqa: UP045 directive is unnecessary here, as confirmed by Ruff static analysis.

🔎 Proposed fix
-    openapi_include_paths: Optional[list[str]] = None  # noqa: UP045
+    openapi_include_paths: Optional[list[str]] = None
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f4b240a and 240c575.

📒 Files selected for processing (16)
  • docs/cli-reference/index.md
  • docs/cli-reference/openapi-only-options.md
  • docs/cli-reference/quick-reference.md
  • src/datamodel_code_generator/__init__.py
  • src/datamodel_code_generator/__main__.py
  • src/datamodel_code_generator/_types/generate_config_dict.py
  • src/datamodel_code_generator/_types/parser_config_dicts.py
  • src/datamodel_code_generator/arguments.py
  • src/datamodel_code_generator/cli_options.py
  • src/datamodel_code_generator/config.py
  • src/datamodel_code_generator/parser/openapi.py
  • src/datamodel_code_generator/prompt_data.py
  • tests/data/expected/main/input_model/config_class.py
  • tests/data/expected/main/openapi/openapi_include_paths/pets_only.py
  • tests/main/openapi/test_main_openapi.py
  • tests/main/test_public_api_signature_baseline.py
🧰 Additional context used
🧬 Code graph analysis (1)
src/datamodel_code_generator/parser/openapi.py (2)
src/datamodel_code_generator/enums.py (1)
  • OpenAPIScope (70-78)
src/datamodel_code_generator/model/base.py (1)
  • path (908-910)
🪛 GitHub Actions: Test
src/datamodel_code_generator/__main__.py

[error] 868-868: OpenAPI path processing failed during code generation due to the above TypeError.

src/datamodel_code_generator/parser/openapi.py

[error] 359-359: TypeError: OpenAPIParser._normalize_path() takes 1 positional argument but 2 were given during path pattern matching (openapi.py:359). This occurred while running pytest for OpenAPI include-path tests (Command: pytest -n auto --color=yes -m 'not perf' ... tests).


[error] 741-741: Traceback indicates the TypeError originates in _normalize_path and bubbles up to parse_raw during OpenAPI parsing (openapi.py:741). Command: pytest -n auto --color=yes -m 'not perf' -p no:codspeed --benchmark-disable --cov ...

tests/main/openapi/test_main_openapi.py

[error] 4706-4706: OpenAPIParser._normalize_path() takes 1 positional argument but 2 were given. (Triggered during test_main_openapi_include_paths)


[error] 4718-4718: OpenAPIParser._normalize_path() takes 1 positional argument but 2 were given. (Triggered during test_main_openapi_include_paths_without_leading_slash)

🪛 Ruff (0.14.10)
src/datamodel_code_generator/__main__.py

554-554: Unused noqa directive (non-enabled: UP045)

Remove unused noqa directive

(RUF100)

🔇 Additional comments (21)
tests/data/expected/main/openapi/openapi_include_paths/pets_only.py (1)

1-27: LGTM! Well-structured test fixture.

The expected output file demonstrates clean model generation with proper type annotations and modern Python syntax. The models correctly represent filtered OpenAPI path schemas.

src/datamodel_code_generator/config.py (2)

143-143: LGTM! Clean config field addition.

The openapi_include_paths field is properly typed and positioned logically alongside related OpenAPI options. The optional list type with None default is appropriate for this filtering feature.


352-352: LGTM! Parser config properly extended.

The field addition to OpenAPIParserConfig correctly mirrors the GenerateConfig field, ensuring the option is available at the parser level for filtering logic.

docs/cli-reference/quick-reference.md (2)

158-158: LGTM! Documentation properly updated.

The new option is correctly added to the OpenAPI-only Options table with appropriate description and linking.


278-278: LGTM! Alphabetical index updated correctly.

The option is properly inserted in alphabetical order within the index with consistent formatting and linking.

src/datamodel_code_generator/__init__.py (1)

742-742: LGTM! Option correctly propagated to parser.

The openapi_include_paths configuration is properly added to the OpenAPI parser options dictionary, ensuring the filtering configuration reaches the parsing layer where it's needed.

docs/cli-reference/index.md (2)

16-16: LGTM! Option count updated correctly.

The OpenAPI-only Options count is accurately incremented from 6 to 7 to reflect the new --openapi-include-paths option.


139-139: LGTM! Index entry properly added.

The new option is correctly placed in alphabetical order within the O section with proper formatting and linking to the detailed documentation.

src/datamodel_code_generator/_types/generate_config_dict.py (1)

105-105: LGTM!

The new field addition is well-typed and consistently placed with other OpenAPI configuration options.

src/datamodel_code_generator/_types/parser_config_dicts.py (1)

165-165: LGTM!

The field addition to OpenAPIParserConfigDict is consistent with the corresponding change in GenerateConfigDict and properly placed within the OpenAPI-specific configuration.

tests/data/expected/main/input_model/config_class.py (1)

178-178: LGTM!

The test expectation correctly reflects the new configuration field with consistent type annotation and placement.

src/datamodel_code_generator/cli_options.py (1)

239-239: LGTM!

The CLI option metadata entry is correctly categorized and positioned with other OpenAPI-specific options.

src/datamodel_code_generator/arguments.py (1)

927-938: Scope enforcement verified.

The CLI argument definition is well-structured with clear help text, appropriate parameter configuration (nargs="+" for multiple patterns), and explicit documentation of the scope requirement. The fnmatch pattern examples are helpful for users.

Scope enforcement is confirmed: when --openapi-include-paths is used without 'paths' in --openapi-scopes, the code issues a warning at initialization time (src/datamodel_code_generator/parser/openapi.py:213) with the message "--openapi-include-paths has no effect without --openapi-scopes paths". Test coverage exists for this behavior.

src/datamodel_code_generator/prompt_data.py (1)

82-82: New option description is consistent and well‑placed

"--openapi-include-paths": "Filter OpenAPI paths to include in model generation." matches the CLI help and nearby OpenAPI options; no issues from a prompt-data/metadata perspective.

tests/main/test_public_api_signature_baseline.py (1)

56-125: Baseline signature update for openapi_include_paths is aligned

Adding openapi_include_paths: list[str] | None = None to _baseline_generate keeps the baseline in sync with the new GenerateConfig field and matches the intended type/default. The surrounding tests (test_generate_signature_matches_baseline, default checks) will enforce consistency with GenerateConfigDict and GenerateConfig, so this change is correctly wired into the signature contract.

tests/main/openapi/test_main_openapi.py (1)

4716-4725: Good coverage for patterns without leading slash

test_main_openapi_include_paths_without_leading_slash nicely verifies that patterns like "pets*" are normalized the same as "/pets*" and still filter to the pets_only.py golden output. This is valuable coverage for user ergonomics around leading slashes in path patterns.

src/datamodel_code_generator/__main__.py (1)

932-932: LGTM!

The new openapi_include_paths parameter is correctly forwarded to the code generation function, following the established pattern for other configuration options.

src/datamodel_code_generator/parser/openapi.py (4)

9-9: LGTM!

The fnmatch module is the appropriate choice for implementing shell-style wildcard pattern matching for OpenAPI paths.


212-217: LGTM!

Good validation logic to warn users when --openapi-include-paths is specified but would have no effect because the paths scope is not enabled.


373-379: LGTM!

The conditional path filtering implementation is well-designed. The apply_path_filter parameter allows selective filtering, which is essential for handling webhooks (which should not be filtered) differently from paths (which should be filtered).


745-747: LGTM!

Correctly excludes webhooks from path filtering by passing apply_path_filter=False. This is the appropriate behavior since webhooks are conceptually distinct from API paths and the filtering option is specifically named --openapi-include-paths.

Comment thread src/datamodel_code_generator/parser/openapi.py Outdated
Comment thread tests/main/openapi/test_main_openapi.py
@koxudaxi koxudaxi force-pushed the feature/openapi-include-paths branch from 240c575 to 2f6ac48 Compare January 2, 2026 10:17
@koxudaxi koxudaxi force-pushed the feature/openapi-include-paths branch from bae8068 to 4a7eb0a Compare January 2, 2026 10:25
@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 2, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.37%. Comparing base (f4b240a) to head (7a9c5ce).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2894   +/-   ##
=======================================
  Coverage   99.37%   99.37%           
=======================================
  Files          92       92           
  Lines       16196    16228   +32     
  Branches     1915     1919    +4     
=======================================
+ Hits        16095    16127   +32     
  Misses         52       52           
  Partials       49       49           
Flag Coverage Δ
unittests 99.37% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ilovelinux ilovelinux added the enhancement New feature or request label Jan 2, 2026
@koxudaxi koxudaxi merged commit 87445c8 into main Jan 2, 2026
42 checks passed
@koxudaxi koxudaxi deleted the feature/openapi-include-paths branch January 2, 2026 14:09
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 2, 2026

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR adds a new optional CLI flag --openapi-include-paths and corresponding API parameter. All changes are purely additive: (1) New optional parameter with default None means existing behavior is unchanged, (2) No changes to generated code output format, (3) No template modifications required, (4) No changes to default values - existing configurations work identically, (5) No Python version changes, (6) Warning for misuse is new functionality, not changed error handling. Users who don't use this new option will see no difference in behavior.


This analysis was performed by Claude Code Action

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 2, 2026

🎉 Released in 0.52.0

This PR is now available in the latest release. See the release notes for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants