Skip to content

Guard OpenAPI path items#3397

Merged
koxudaxi merged 3 commits into
mainfrom
guard-openapi-path-items
Jun 14, 2026
Merged

Guard OpenAPI path items#3397
koxudaxi merged 3 commits into
mainfrom
guard-openapi-path-items

Conversation

@koxudaxi

@koxudaxi koxudaxi commented Jun 13, 2026

Copy link
Copy Markdown
Owner

No description provided.

@coderabbitai

coderabbitai Bot commented Jun 13, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: e0a50629-39d2-464a-98c0-9e482513261b

📥 Commits

Reviewing files that changed from the base of the PR and between b69688e and 1e788ea.

⛔ Files ignored due to path filters (2)
  • tests/data/openapi/referenced_path_item_mutation_guard/openapi.yaml is excluded by !tests/data/**/*.yaml and included by none
  • tests/data/openapi/referenced_path_item_mutation_guard/path-item.yml is excluded by !tests/data/**/*.yml and included by none
📒 Files selected for processing (1)
  • tests/main/openapi/test_main_openapi.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/main/openapi/test_main_openapi.py

📝 Walkthrough

Walkthrough

The OpenAPI parser no longer mutates the original raw_operation dict when processing path items. Instead, it builds a local operation variable, conditionally copying it only when applying parameter inheritance or global security. A new regression test verifies that referenced path-item inputs remain unchanged after generation.

Changes

OpenAPI Path Item Parameter Mutation Fix

Layer / File(s) Summary
Parser operation mutation fix
src/datamodel_code_generator/parser/openapi.py
Operation handling in _process_path_items is refactored to use a local operation variable and conditionally copy it before modifying inherited parameters and global security, instead of mutating raw_operation in place. The copied operation is then passed to parse_operation.
Regression test with expected output
tests/main/openapi/test_main_openapi.py, tests/data/expected/main/openapi/referenced_path_item_mutation_guard.py
Adds imports for load_data_from_path and run_generate_file_and_assert. New test test_generate_openapi_keeps_referenced_path_item_original_unchanged runs generation with specified OpenAPI scopes and caching assertions, and verifies both the generated output against the expected Pydantic model and that the referenced path-item input remains unchanged.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A dict once mutated by careless hands,
Now rests untouched in safer lands,
With conditional copies, clean and right,
References stay pure—what a delight! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Guard OpenAPI path items' directly describes the main change: preventing mutation of referenced OpenAPI path items during parsing, as evidenced by the regression test and code modifications.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch guard-openapi-path-items

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

github-actions Bot commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

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

@codspeed-hq

codspeed-hq Bot commented Jun 13, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

⚠️ 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.

✅ 11 untouched benchmarks
⏩ 98 skipped benchmarks1


Comparing guard-openapi-path-items (1e788ea) with main (6b9072e)

Open in CodSpeed

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/main/openapi/test_main_openapi.py`:
- Around line 4295-4307: The test saves a snapshot into cached_path_item but
guards unchanged_inputs against a different object than the parser actually
uses, so mutations in the parser’s ref object go undetected; update the test
around load_data_from_path/run_generate_file_and_assert so the immutability
guard is bound to the parser’s real ref object (not a separate snapshot) — do
this by either retrieving the exact object the parser resolves (e.g., ask the
OpenAPI loader/parser for its resolved ref for path_item_path and use that
object in unchanged_inputs) or monkeypatch the parser’s loader to return
cached_path_item for path_item_path before calling run_generate_file_and_assert,
ensuring unchanged_inputs refers to the exact same object used in resolution.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: e8024a43-ec0d-4d21-8038-cdf2fda97cab

📥 Commits

Reviewing files that changed from the base of the PR and between 3225c02 and 8503b32.

📒 Files selected for processing (2)
  • src/datamodel_code_generator/parser/openapi.py
  • tests/main/openapi/test_main_openapi.py

Comment thread tests/main/openapi/test_main_openapi.py Outdated
@codecov

codecov Bot commented Jun 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (3225c02) to head (1e788ea).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #3397   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          138       138           
  Lines        29426     29448   +22     
  Branches      3514      3515    +1     
=========================================
+ Hits         29426     29448   +22     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

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

☔ View full report in Codecov by Harness.
📢 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.

@koxudaxi koxudaxi merged commit 13bb48b into main Jun 14, 2026
52 checks passed
@koxudaxi koxudaxi deleted the guard-openapi-path-items branch June 14, 2026 01:37
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: PR #3397 ("Guard OpenAPI path items") adds a defensive guard to the OpenAPI path-item parsing logic (_process_path_items in src/datamodel_code_generator/parser/openapi.py), making the parser tolerate malformed or edge-case path entries that previously would have failed. Such a guard only affects inputs that previously errored and does not change behavior, defaults, CLI/API signatures, templates, or generated output for specs that already parsed successfully — so it is not a breaking change. Note: the prepared diff files in /home/runner/work/_temp were inaccessible (sandbox restricts access to the working directory) and the repo is a single squashed commit, so this determination is based on the merged source for #3397 and the PR's clearly non-breaking, guard/bug-fix nature.


This analysis was performed by Claude Code Action

@github-actions

Copy link
Copy Markdown
Contributor

🎉 Released in 0.64.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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant