Skip to content

fix: skip non-model types in __change_field_name#2717

Merged
koxudaxi merged 1 commit intomainfrom
fix/issue-2405-graphql-union-snake-case
Dec 21, 2025
Merged

fix: skip non-model types in __change_field_name#2717
koxudaxi merged 1 commit intomainfrom
fix/issue-2405-graphql-union-snake-case

Conversation

@koxudaxi
Copy link
Copy Markdown
Owner

@koxudaxi koxudaxi commented Dec 20, 2025

Fixes: #2405

Summary by CodeRabbit

  • Bug Fixes

    • Fixed GraphQL union type handling to prevent incorrect snake_case field conversion on union type references.
  • Tests

    • Added test coverage for GraphQL union types with snake_case field conversion.

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

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Dec 20, 2025

Walkthrough

The fix prevents the --snake-case-field flag from incorrectly converting union type reference names to snake_case. A guard was added to skip field name changes for models without a BASE_CLASS attribute, ensuring union variant type names remain in their original casing while field names within models still get snake_case conversion applied.

Changes

Cohort / File(s) Summary
Parser logic fix
src/datamodel_code_generator/parser/base.py
Added guard condition in __change_field_name to skip models lacking a BASE_CLASS attribute, preventing field name mutations on union type references.
Test expected output
tests/data/expected/main/graphql/union_snake_case_field.py
New generated test fixture for GraphQL union with snake_case fields. Defines IResource base interface, Car and Employee models with Field aliases for snake_case→camelCase field mapping, scalar type aliases (Boolean, ID, Int, String), and union type aliases (Resource, TechnicalResource).
Test coverage
tests/main/graphql/test_main_graphql.py
Added test function test_main_graphql_union_snake_case_field verifying union type names are not converted to snake_case when the --snake-case-field flag is enabled.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Parser change: Single conditional guard addition with minimal logic impact; verify it correctly identifies models without BASE_CLASS
  • Test fixture validation: Review generated output structure (scalar aliases, model definitions, union type alias syntax) for correctness
  • Test case clarity: Confirm test properly validates union names remain unconverted while fields use snake_case

Poem

🐰 A union's name, now safe from snake's embrace!
The guard stands tall, each type keeps its face.
Fields bend to the case, but names stay their own,
GraphQL's structure beautifully shown! ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: skip non-model types in __change_field_name' is clear, specific, and directly related to the main code change that adds a guard in __change_field_name to skip non-model types.
Linked Issues check ✅ Passed The PR directly addresses issue #2405 by preventing snake_case conversion from being applied to union type names in GraphQL schemas.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the snake_case field conversion issue: the core fix in base.py, the test case for union_snake_case_field, and the corresponding test method.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/issue-2405-graphql-union-snake-case

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.

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

🧹 Nitpick comments (1)
src/datamodel_code_generator/parser/base.py (1)

1808-1809: Consider using an explicit type check instead of checking BASE_CLASS.

The guard if not model.BASE_CLASS: works but is indirect. Since TypeAliasBase is already imported (line 65), consider using an explicit type check for clarity and safety:

            if "Enum" in model.base_class:
                continue
-            if not model.BASE_CLASS:
+            if isinstance(model, TypeAliasBase):
                continue

This makes the intent clearer (skip type aliases from field name transformation) and avoids potential AttributeError if BASE_CLASS doesn't exist on some model types.

🔎 Verify which model types lack BASE_CLASS
#!/bin/bash
# Check if BASE_CLASS is consistently defined across DataModel subclasses
ast-grep --pattern $'class $CLASS($$$):
  $$$
  BASE_CLASS = $$$
  $$$'
📜 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 3e7b16b and 446d8be.

📒 Files selected for processing (3)
  • src/datamodel_code_generator/parser/base.py (1 hunks)
  • tests/data/expected/main/graphql/union_snake_case_field.py (1 hunks)
  • tests/main/graphql/test_main_graphql.py (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
tests/main/graphql/test_main_graphql.py (2)
tests/test_main_kr.py (1)
  • output_file (44-46)
tests/main/conftest.py (2)
  • output_file (94-96)
  • run_main_and_assert (196-352)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (12)
  • GitHub Check: py312-isort7 on Ubuntu
  • GitHub Check: py312-isort5 on Ubuntu
  • GitHub Check: 3.10 on Ubuntu
  • GitHub Check: 3.11 on Ubuntu
  • GitHub Check: 3.12 on Ubuntu
  • GitHub Check: 3.10 on Windows
  • GitHub Check: 3.11 on Windows
  • GitHub Check: 3.13 on Windows
  • GitHub Check: 3.12 on Windows
  • GitHub Check: 3.14 on Windows
  • GitHub Check: Analyze (python)
  • GitHub Check: benchmarks
🔇 Additional comments (1)
tests/main/graphql/test_main_graphql.py (1)

606-615: LGTM! Test correctly validates the fix.

The test properly verifies that union type references are not converted to snake_case when --snake-case-field is enabled. The test uses the same union.graphql input as the existing test_main_graphql_union test but with the --snake-case-field flag and pydantic_v2.BaseModel output type, which is exactly what's needed to validate the fix for issue #2405.

@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 20, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.34%. Comparing base (3e7b16b) to head (446d8be).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2717   +/-   ##
=======================================
  Coverage   99.34%   99.34%           
=======================================
  Files          82       82           
  Lines       11611    11615    +4     
  Branches     1405     1406    +1     
=======================================
+ Hits        11535    11539    +4     
  Misses         45       45           
  Partials       31       31           
Flag Coverage Δ
unittests 99.34% <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.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Dec 20, 2025

CodSpeed Performance Report

Merging #2717 will not alter performance

Comparing fix/issue-2405-graphql-union-snake-case (446d8be) with main (3e7b16b)

Summary

✅ 59 untouched
⏩ 10 skipped1

Footnotes

  1. 10 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.

@koxudaxi koxudaxi merged commit f4b074f into main Dec 21, 2025
38 checks passed
@koxudaxi koxudaxi deleted the fix/issue-2405-graphql-union-snake-case branch December 21, 2025 01:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

snake-case-field incorrectly applies to union names for graphql schema.

1 participant