Skip to content

Fix --base-class-map and --enum-field-as-literal-map long inline json support#3075

Merged
koxudaxi merged 5 commits intokoxudaxi:mainfrom
ilovelinux:fix/ilovelinux-3071-json-or-path-function-fix
Apr 9, 2026
Merged

Fix --base-class-map and --enum-field-as-literal-map long inline json support#3075
koxudaxi merged 5 commits intokoxudaxi:mainfrom
ilovelinux:fix/ilovelinux-3071-json-or-path-function-fix

Conversation

@ilovelinux
Copy link
Copy Markdown
Collaborator

@ilovelinux ilovelinux commented Apr 7, 2026

Context

  • path.is_file() was used to check whether a path is a file or not.
  • When the path is too long, underlying os.path.isfile() function raises OSError.
  • path.is_file() and similar functions behaves differently in different Python versions1:
    • [Python <=3.13] path.is_file() propagate OSError.
    • [ Python >= 3.14] path.is_file() catches OSError and returns false.

Consequence

  • If a long inline json was provided, Python <= 3.13 would have raised an unhandled OSError exception creating a crash.

Now

  • We catch OSError to align path.is_file() behavior of Python <=3.13 with the one implemented by Python >= 3.14.
  • We do not treat the value as a file path if OSError is raised by path.is_file()

Note

Summary by CodeRabbit

  • Bug Fixes

    • Improved robustness when detecting whether a JSON input is a file path: filesystem errors during path checks are now handled gracefully, treating the input as raw JSON to avoid crashes.
  • Tests

    • Added tests covering very large inline JSON passed to the base-class-map option.
    • Added tests covering very large inline JSON passed to the enum-field-as-literal-map option.

Footnotes

  1. https://docs.python.org/3/library/pathlib.html#querying-file-type-and-status

@ilovelinux ilovelinux requested a review from koxudaxi April 7, 2026 13:01
@ilovelinux ilovelinux self-assigned this Apr 7, 2026
@ilovelinux ilovelinux added the bug Something isn't working label Apr 7, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 7, 2026

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: 59346a5d-2092-4f79-9335-726c91345142

📥 Commits

Reviewing files that changed from the base of the PR and between f58aa96 and 3a6c153.

📒 Files selected for processing (1)
  • tests/main/jsonschema/test_main_jsonschema.py
✅ Files skipped from review due to trivial changes (1)
  • tests/main/jsonschema/test_main_jsonschema.py

📝 Walkthrough

Walkthrough

Wraps the filesystem check in the CLI JSON parsing helper to treat OSError from path.is_file() as inline JSON input; adds tests that pass very large inline JSON strings to CLI options to verify handling.

Changes

Cohort / File(s) Summary
CLI JSON parsing
src/datamodel_code_generator/arguments.py
Wrap path.is_file() in try/except OSError inside _json_value_or_file; on OSError set is_file = False so the input is parsed as JSON text rather than treated as a file path.
Tests — long JSON inputs
tests/main/jsonschema/test_main_jsonschema.py
Add import itertools; add test_main_jsonschema_base_class_map_long_json and test_main_enum_field_as_literal_map_long_json which construct large inline JSON strings (100-item expansions) passed to CLI flags and assert expected outputs.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

🐇 I sniff a path both short and long,
If OSError hums a troubling song.
I catch the wobble, treat the text,
Parse JSON clean — no files perplexed.
A little hop for tests now strong. 🥕

🚥 Pre-merge checks | ✅ 3
✅ 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 accurately summarizes the main change: fixing long inline JSON support for two CLI options by catching OSError in path validation logic.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 7, 2026

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 ilovelinux:fix/ilovelinux-3071-json-or-path-function-fix (3a6c153) with main (5ba49a7)

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.

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.

🧹 Nitpick comments (1)
tests/main/jsonschema/test_main_jsonschema.py (1)

3147-3159: Consider adding the twin long-inline regression for --enum-field-as-literal-map.

This test covers the long-inline JSON path for --base-class-map, but the PR scope also includes --enum-field-as-literal-map. Adding an analogous long-inline case would prevent future divergence between the two CLI options.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/main/jsonschema/test_main_jsonschema.py` around lines 3147 - 3159, Add
a sibling test to cover the long-inline JSON case for the
--enum-field-as-literal-map option similar to
test_main_jsonschema_base_class_map_long_json: create a new test (e.g.,
test_main_jsonschema_enum_field_as_literal_map_long_json) that calls
run_main_and_assert with the same
input_path/output_path/assert_func/expected_file pattern but pass extra_args
with "--enum-field-as-literal-map" and a very long inline JSON string built with
itertools.repeat (matching the 100-item pattern used in
test_main_jsonschema_base_class_map_long_json) so both CLI options have
equivalent long-inline regression coverage.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@tests/main/jsonschema/test_main_jsonschema.py`:
- Around line 3147-3159: Add a sibling test to cover the long-inline JSON case
for the --enum-field-as-literal-map option similar to
test_main_jsonschema_base_class_map_long_json: create a new test (e.g.,
test_main_jsonschema_enum_field_as_literal_map_long_json) that calls
run_main_and_assert with the same
input_path/output_path/assert_func/expected_file pattern but pass extra_args
with "--enum-field-as-literal-map" and a very long inline JSON string built with
itertools.repeat (matching the 100-item pattern used in
test_main_jsonschema_base_class_map_long_json) so both CLI options have
equivalent long-inline regression coverage.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 5e130d50-661a-4fe2-bd92-26bf9bcc66bd

📥 Commits

Reviewing files that changed from the base of the PR and between 5ba49a7 and ae0b4d5.

📒 Files selected for processing (2)
  • src/datamodel_code_generator/arguments.py
  • tests/main/jsonschema/test_main_jsonschema.py

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 7, 2026

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #3075   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           87        87           
  Lines        18237     18247   +10     
  Branches      2087      2087           
=========================================
+ Hits         18237     18247   +10     
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 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.

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: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@tests/main/jsonschema/test_main_jsonschema.py`:
- Around line 4677-4695: The test hunk for
test_main_enum_field_as_literal_map_long_json has formatter drift; run the
formatter and commit the changes: run ruff format on
tests/main/jsonschema/test_main_jsonschema.py (or use your project's configured
formatter) to reformat the test function
test_main_enum_field_as_literal_map_long_json and the surrounding lines, then
add and commit the updated file so the CI formatting check passes.
🪄 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: d45ecd49-08f2-426a-9a44-557044223132

📥 Commits

Reviewing files that changed from the base of the PR and between c5bff44 and f58aa96.

📒 Files selected for processing (1)
  • tests/main/jsonschema/test_main_jsonschema.py

Comment thread tests/main/jsonschema/test_main_jsonschema.py
@koxudaxi koxudaxi merged commit 05901ff into koxudaxi:main Apr 9, 2026
38 checks passed
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 9, 2026

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR is a pure bug fix that resolves an unhandled OSError crash on Python ≤ 3.13 when --base-class-map or --enum-field-as-literal-map are passed a long inline JSON value. The fix in src/datamodel_code_generator/arguments.py simply wraps path.is_file() in a try/except to return False on OSError, matching Python 3.14's behavior. No CLI options, API signatures, generated output, templates, defaults, or supported Python versions are changed. Users who had working inputs before will continue to work; users whose long-JSON inputs previously crashed will now succeed — that is strictly additive, not breaking.


This analysis was performed by Claude Code Action

@github-actions
Copy link
Copy Markdown
Contributor

🎉 Released in 0.56.1

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

breaking-change-analyzed bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants