Skip to content

Conversation

@koxudaxi
Copy link
Owner

Summary

  • Fix incorrect code generation for GraphQL input fields with empty list defaults
  • Required lists ([String!]! = []) now correctly generate Field(default_factory=list) instead of Field(...)
  • Nullable lists ([String!] = []) now correctly generate Field(default_factory=list) instead of Field([])

Problem

When GraphQL input types have fields with empty list defaults like:

input TestInput {
    requiredList: [String!]! = []
    nullableList: [String!] = []
}

The generated Pydantic code was incorrect:

  • Required lists: Field(...) - completely ignored the default
  • Nullable lists: Field([]) - used mutable default directly (anti-pattern)

Solution

  1. Added early check in _get_default_as_pydantic_model() for any list type with empty default
  2. Updated __str__() to not skip default handling when required=True but has_default=True
  3. Updated field argument generation to not add ... when there's already a default_factory

Test plan

  • Added test test_main_graphql_empty_list_default with both Pydantic v1 and v2 variants
  • All existing GraphQL tests pass
  • All model tests pass
  • Lint and format checks pass

🤖 Generated with Claude Code

@coderabbitai
Copy link

coderabbitai bot commented Jan 10, 2026

Warning

Rate limit exceeded

@koxudaxi has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 19 minutes and 48 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 aa088d6 and 0476ab3.

⛔ Files ignored due to path filters (1)
  • tests/data/graphql/empty_list_default.graphql is excluded by !tests/data/**/*.graphql and included by none
📒 Files selected for processing (4)
  • src/datamodel_code_generator/model/pydantic/base_model.py
  • tests/data/expected/main/graphql/empty_list_default.py
  • tests/data/expected/main/graphql/pydantic_v2_empty_list_default.py
  • tests/main/graphql/test_main_graphql.py

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.

When GraphQL input fields have empty list defaults (e.g., `requiredList: [String!]! = []`),
the generated code was incorrectly producing:
- `Field(...)` for required lists (ignoring the default)
- `Field([])` for nullable lists (using mutable default)

This fix ensures all list fields with empty defaults generate `Field(default_factory=list)`,
which is the correct way to handle mutable defaults in Pydantic.

Changes:
- Add early check in `_get_default_as_pydantic_model()` for any list type with empty default
- Update `__str__()` to not skip default handling when `required=True` but `has_default=True`
- Update field argument generation to not add `...` when there's already a `default_factory`

Fixes issue where GraphQL schemas with default empty arrays would generate incorrect Pydantic code.
@koxudaxi koxudaxi force-pushed the fix/required-list-empty-default branch from fe65bfc to 0476ab3 Compare January 10, 2026 08:21
@github-actions
Copy link
Contributor

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

@koxudaxi
Copy link
Owner Author

Closing in favor of PR #2948

@koxudaxi koxudaxi closed this Jan 10, 2026
@codspeed-hq
Copy link

codspeed-hq bot commented Jan 10, 2026

Merging this PR will degrade performance by 23.72%

⚠️ 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 regressed benchmarks
⏩ 98 skipped benchmarks1

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
WallTime test_perf_large_models_pydantic_v2 3.1 s 4.1 s -23.72%
WallTime test_perf_multiple_files_input 3.2 s 4.1 s -21.9%
WallTime test_perf_duplicate_names 849.3 ms 1,017.8 ms -16.56%
WallTime test_perf_aws_style_openapi_pydantic_v2 1.7 s 2.1 s -21.08%
WallTime test_perf_deep_nested 5.1 s 6.2 s -16.94%
WallTime test_perf_kubernetes_style_pydantic_v2 2.2 s 2.7 s -17.38%
WallTime test_perf_stripe_style_pydantic_v2 1.7 s 2.1 s -21.18%
WallTime test_perf_openapi_large 2.5 s 3.1 s -21.44%
WallTime test_perf_complex_refs 1.7 s 2.2 s -22.55%
WallTime test_perf_graphql_style_pydantic_v2 703.2 ms 902.6 ms -22.09%
WallTime test_perf_all_options_enabled 5.7 s 6.8 s -16.08%

Comparing fix/required-list-empty-default (0476ab3) with main (aa088d6)

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.

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.

2 participants