Skip to content

Add --default-values CLI option for overriding field defaults#2897

Merged
koxudaxi merged 12 commits intomainfrom
feature/default-values-override
Jan 2, 2026
Merged

Add --default-values CLI option for overriding field defaults#2897
koxudaxi merged 12 commits intomainfrom
feature/default-values-override

Conversation

@koxudaxi
Copy link
Copy Markdown
Owner

@koxudaxi koxudaxi commented Jan 2, 2026

Fixes: #2457

Summary by CodeRabbit

  • New Features

    • Added a --default-values CLI option to override field default values from an external JSON file (supports scoped and flat keys; applies in one-shot and watch modes).
  • Documentation

    • CLI reference, quick-reference and usage docs updated with option details, precedence rules, examples and sample outputs.
  • Tests

    • Added end-to-end and unit tests covering valid overrides, allOf handling, parameter/field cases, invalid JSON and non-object JSON error conditions.

✏️ 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 2 minutes and 34 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 0f88967 and 78818a5.

📒 Files selected for processing (13)
  • docs/cli-reference/index.md
  • docs/cli-reference/quick-reference.md
  • 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/graphql.py
  • src/datamodel_code_generator/prompt_data.py
  • tests/data/expected/main/input_model/config_class.py
  • tests/main/graphql/test_main_graphql.py
  • tests/main/test_public_api_signature_baseline.py
📝 Walkthrough

Walkthrough

Adds a --default-values CLI option and threads a default-value overrides mapping from CLI/config through main, watch, ModelResolver, and parsers (GraphQL, JSON Schema, OpenAPI); updates config/TypedDicts, CLI metadata, docs, tests, and expected outputs.

Changes

Cohort / File(s) Summary
Docs
docs/cli-reference/field-customization.md, docs/cli-reference/index.md, docs/cli-reference/quick-reference.md
Document --default-values option, usage, examples and index entries (one doc contains a duplicated section).
CLI args & metadata
src/datamodel_code_generator/arguments.py, src/datamodel_code_generator/cli_options.py, src/datamodel_code_generator/prompt_data.py
Register --default-values arg; add CLI metadata and option description.
Main & watch wiring
src/datamodel_code_generator/__main__.py, src/datamodel_code_generator/watch.py
Load/validate JSON mapping via helper, introduce and propagate default_value_overrides into run_generate_from_config, generate, and watch flows.
Config & TypedDicts
src/datamodel_code_generator/config.py, src/datamodel_code_generator/_types/generate_config_dict.py, src/datamodel_code_generator/_types/parser_config_dicts.py
Add default_value_overrides / default_values fields to Config/ParserConfig and TypedDicts (public surface change).
Resolver / reference
src/datamodel_code_generator/reference.py, src/datamodel_code_generator/parser/base.py
ModelResolver stores default_value_overrides and exposes resolve_default_value(); parser initialization forwards overrides to resolver.
Parsers — GraphQL
src/datamodel_code_generator/parser/graphql.py
Extend parse_field() with original_field_name and class_name; resolve effective defaults via resolver and apply them (may flip required).
Parsers — JSON Schema
src/datamodel_code_generator/parser/jsonschema.py
Thread effective_default/effective_has_default through parse_object_fields() and get_object_field(); use effective values for field construction and nullability/required decisions.
Parsers — OpenAPI
src/datamodel_code_generator/parser/openapi.py
Compute effective defaults/has_default for parameters and schemas, derive effective_required, and apply effective values to field construction and nullability logic.
Public API / types (tests baseline)
tests/main/test_public_api_signature_baseline.py, tests/data/expected/main/input_model/config_class.py
Added default_value_overrides parameter to baseline signatures and expected TypedDict output.
Tests & fixtures
tests/main/conftest.py, tests/main/test_main_general.py, tests/main/graphql/test_main_graphql.py, tests/main/jsonschema/test_main_jsonschema.py, tests/main/openapi/test_main_openapi.py
New DEFAULT_VALUES_DATA_PATH; added integration and error-handling tests for --default-values; some duplicated test insertions present.
Expected generated outputs
tests/data/expected/main/graphql/default_values_required_use_default.py, tests/data/expected/main/jsonschema/jsonschema_default_values_override.py, tests/data/expected/main/openapi/default_values_parameters_use_default.py, tests/data/expected/main/jsonschema/jsonschema_default_values_allof.py
New expected output files demonstrating applied default overrides.

Sequence Diagram(s)

sequenceDiagram
    actor User
    participant CLI as __main__.py
    participant FS as File I/O
    participant Config
    participant Parser as Parser (GraphQL/JSONSchema/OpenAPI)
    participant Resolver as ModelResolver

    User->>CLI: invoke CLI with --default-values file.json
    CLI->>FS: read file.json
    FS-->>CLI: raw JSON
    CLI->>CLI: validate JSON is object (_load_json_config)
    alt invalid JSON or non-object
        CLI->>User: print error and exit
    else valid mapping
        CLI->>Config: attach default_value_overrides
        CLI->>Parser: init parsers with config (includes overrides)
        Parser->>Resolver: pass default_value_overrides into ModelResolver
        loop for each field
            Parser->>Resolver: resolve_default_value(field_name, original_default, has_default, class_name)
            Resolver-->>Parser: effective_default, effective_has_default
            Parser->>Parser: apply effective values to DataModelField (may set required=False)
        end
        Parser->>User: emit generated code with applied defaults
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested labels

breaking-change-analyzed

Poem

🐰 I nibble JSON in moonlit rows,
Scoped keys hop where the default goes,
Parsers ask, resolvers know,
Fields wake soft with values in tow,
Hooray — a rabbit’s cozy codegen prose.

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 concisely describes the main change: adding a new CLI option called --default-values for field default value overrides.
Docstring Coverage ✅ Passed Docstring coverage is 83.33% 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.

koxudaxi and others added 2 commits January 2, 2026 14:13
…s-override

# Conflicts:
#	tests/main/jsonschema/test_main_jsonschema.py
#	tests/main/openapi/test_main_openapi.py
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 2, 2026

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

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Jan 2, 2026

CodSpeed Performance Report

Merging #2897 will not alter performance

Comparing feature/default-values-override (78818a5) with main (99aac7b)

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

@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.38%. Comparing base (99aac7b) to head (78818a5).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2897   +/-   ##
=======================================
  Coverage   99.37%   99.38%           
=======================================
  Files          92       92           
  Lines       16268    16335   +67     
  Branches     1921     1932   +11     
=======================================
+ Hits        16167    16234   +67     
  Misses         52       52           
  Partials       49       49           
Flag Coverage Δ
unittests 99.38% <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.

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 (8)
docs/cli-reference/field-customization.md (1)

561-628: Consider documenting JSON mapping shape and precedence rules

The --default-values section is accurate but a bit terse compared to the CLI help. To make the feature easier to adopt, I’d suggest (optionally) adding:

  • A small JSON snippet showing flat vs scoped keys (e.g. {"User.status": "active", "page": 1}).
  • A short note that scoped keys override flat ones and that required fields stay required unless --use-default is set.

This would align the docs with the CLI help text and avoid ambiguity about how overrides are resolved.

src/datamodel_code_generator/parser/jsonschema.py (1)

1044-1046: Consider simplifying redundant conditional logic.

The condition if effective_has_default is not None is always True when called from parse_object_fields (line 2422-2427), since resolve_default_value always returns a boolean for the second tuple element. The logic works correctly because resolve_default_value returns the original values when no override exists, but the conditional check is misleading.

Consider either:

  1. Removing the conditional if all callers always provide these parameters
  2. Documenting that the parameters are optional for backward compatibility
  3. Using a sentinel value to explicitly distinguish "no override attempted" from "override returned original value"
🔎 Verify all callers provide effective values
#!/bin/bash
# Find all calls to get_object_field to check if effective parameters are always provided
ast-grep --pattern $'$_.get_object_field(
  $$$
)'
tests/main/jsonschema/test_main_jsonschema.py (1)

7461-7483: New --default-values CLI-doc test is wired correctly; consider adding related options metadata

The cli-doc metadata, schema/golden paths, and run_main_and_assert usage all follow the established pattern (mirroring the aliases tests), and the override file is correctly referenced via DEFAULT_VALUES_DATA_PATH. You might optionally add related_options=["--use-default"] (and/or other interacting flags) to the @pytest.mark.cli_doc for better cross-linking in the CLI docs, but the test itself looks solid.

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

505-635: Default override + required/nullable handling in parse_all_parameters is consistent with JsonSchema behavior

The new effective_default / effective_has_default handling for both schema_ and content branches, plus recomputing effective_required when apply_default_values_for_required_fields is enabled, matches the JsonSchema path and gives the right behavior for required parameters gaining defaults (including via overrides). The updated nullable computation in the content branch using effective_has_default / effective_required also stays in sync with get_object_field’s strict-nullable semantics. This overall wiring looks solid.

Clean up unused noqa on parse_all_parameters

Ruff reports # noqa: PLR0912, PLR0914 as unused for this function because those checks aren’t enabled; removing the directive avoids the RUF100 warning without affecting lint behavior.

Proposed diff to drop the unused noqa
-    def parse_all_parameters(  # noqa: PLR0912, PLR0914
+    def parse_all_parameters(
         self,
         name: str,
         parameters: list[ReferenceObject | ParameterObject],
         path: list[str],
     ) -> DataType | None:

Verify class-scoped override key choice for parameters

One subtle point: aliases for parameters are resolved with class_name=name in get_valid_field_name_and_alias, while default overrides are resolved with class_name=reference.name in resolve_default_value. If users rely on class-scoped keys (e.g. MyParams.foo) for both aliases and default overrides, this split may require them to use different class names for the two features if reference.name ever diverges from name due to uniquification.

If the intended contract is “use the generated Python class name” for both aliases and default overrides, it might be worth aligning the class_name used in alias resolution with reference.name; if the intended contract is instead “use the logical base name from the spec,” then keeping them different is correct and should be documented.

src/datamodel_code_generator/parser/graphql.py (1)

432-443: Passing original field names into override/alias resolution in parse_object_like is a good improvement

Iterating as for original_field_name, field in obj.fields.items() and then:

  • feeding original_field_name into get_valid_field_name_and_alias with class_name=obj.name, and
  • passing both original_field_name and class_name=obj.name into parse_field

makes scoped aliasing and default overrides behave predictably for GraphQL types. The overall change reads clean and keeps the exclude_field_names logic unmodified.

If you ever decide you want DataModelFieldBase.original_name to consistently reflect the source schema name (here, the GraphQL field), you could consider switching original_name=field_name to original_name=original_field_name in parse_field. That would be a behavior change, though, so only do it if it aligns with how other parsers use original_name.

src/datamodel_code_generator/reference.py (1)

1199-1237: Remove unused noqa directive.

The logic for hierarchical default value resolution is well-structured and correctly implements scoped (ClassName.field_name) and flat (field_name) override resolution. However, the # noqa: FBT001 directive on line 1203 appears to be unused according to static analysis.

🔎 Proposed fix
         field_name: str,
         original_default: Any,
-        has_default: bool,  # noqa: FBT001
+        has_default: bool,
         class_name: str | None = None,
     ) -> tuple[Any, bool]:
src/datamodel_code_generator/__main__.py (2)

506-506: Remove unused noqa directive.

The field definition is correct, but the # noqa: UP045 directive is flagged as unused by static analysis.

🔎 Proposed fix
-    default_values: Optional[TextIOBase] = None  # noqa: UP045
+    default_values: Optional[TextIOBase] = None

1196-1212: Clean up unused noqa directives.

The validation logic correctly mirrors the pattern used for aliases and provides clear error messages. However, static analysis flags several unused # noqa: T201 directives.

🔎 Proposed fix
             try:
                 default_value_overrides = json.load(data)
             except json.JSONDecodeError as e:
-                print(f"Unable to load default values mapping: {e}", file=sys.stderr)  # noqa: T201
+                print(f"Unable to load default values mapping: {e}", file=sys.stderr)
                 return Exit.ERROR
         if not isinstance(default_value_overrides, dict):
-            print("Unable to load default values mapping: must be a JSON object", file=sys.stderr)  # noqa: T201
+            print("Unable to load default values mapping: must be a JSON object", file=sys.stderr)
             return Exit.ERROR
         if not all(isinstance(k, str) for k in default_value_overrides):  # pragma: no cover
-            print("Unable to load default values mapping: all keys must be strings", file=sys.stderr)  # noqa: T201
+            print("Unable to load default values mapping: all keys must be strings", file=sys.stderr)
             return Exit.ERROR  # pragma: no cover
📜 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 87445c8 and 2793acb.

⛔ Files ignored due to path filters (8)
  • tests/data/default_values/graphql_user_defaults.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/default_values/invalid_json.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/default_values/non_dict.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/default_values/openapi_params_defaults.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/default_values/scoped_defaults.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/graphql/default_values_required.graphql is excluded by !tests/data/**/*.graphql and included by none
  • tests/data/jsonschema/default_values_override.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/openapi/default_values_parameters.yaml is excluded by !tests/data/**/*.yaml and included by none
📒 Files selected for processing (26)
  • docs/cli-reference/field-customization.md
  • docs/cli-reference/index.md
  • docs/cli-reference/quick-reference.md
  • 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/base.py
  • src/datamodel_code_generator/parser/graphql.py
  • src/datamodel_code_generator/parser/jsonschema.py
  • src/datamodel_code_generator/parser/openapi.py
  • src/datamodel_code_generator/prompt_data.py
  • src/datamodel_code_generator/reference.py
  • src/datamodel_code_generator/watch.py
  • tests/data/expected/main/graphql/default_values_required_use_default.py
  • tests/data/expected/main/input_model/config_class.py
  • tests/data/expected/main/jsonschema/jsonschema_default_values_override.py
  • tests/data/expected/main/openapi/default_values_parameters_use_default.py
  • tests/main/conftest.py
  • tests/main/graphql/test_main_graphql.py
  • tests/main/jsonschema/test_main_jsonschema.py
  • tests/main/openapi/test_main_openapi.py
  • tests/main/test_main_general.py
  • tests/main/test_public_api_signature_baseline.py
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2026-01-02T08:25:19.839Z
Learnt from: koxudaxi
Repo: koxudaxi/datamodel-code-generator PR: 2890
File: tests/data/expected/main/jsonschema/ref_nullable_with_constraint.py:14-15
Timestamp: 2026-01-02T08:25:19.839Z
Learning: The datamodel-code-generator currently generates RootModel subclasses with an explicit `root` field annotation (e.g., `class StringType(RootModel[str]): root: str`). This is existing behavior of the code generator and should not be flagged as an issue introduced by new changes.

Applied to files:

  • tests/data/expected/main/input_model/config_class.py
  • tests/data/expected/main/jsonschema/jsonschema_default_values_override.py
🧬 Code graph analysis (10)
tests/main/jsonschema/test_main_jsonschema.py (1)
tests/main/conftest.py (1)
  • run_main_and_assert (245-409)
tests/main/graphql/test_main_graphql.py (1)
tests/main/conftest.py (1)
  • run_main_and_assert (245-409)
tests/main/openapi/test_main_openapi.py (1)
tests/main/conftest.py (2)
  • output_file (99-101)
  • run_main_and_assert (245-409)
src/datamodel_code_generator/reference.py (2)
src/datamodel_code_generator/parser/jsonschema.py (1)
  • has_default (422-424)
src/datamodel_code_generator/model/base.py (2)
  • class_name (848-850)
  • class_name (853-857)
src/datamodel_code_generator/parser/jsonschema.py (2)
src/datamodel_code_generator/model/base.py (4)
  • DataModelFieldBase (151-469)
  • field (404-406)
  • class_name (848-850)
  • class_name (853-857)
src/datamodel_code_generator/reference.py (1)
  • resolve_default_value (1199-1236)
src/datamodel_code_generator/parser/openapi.py (2)
src/datamodel_code_generator/reference.py (2)
  • resolve_default_value (1199-1236)
  • reference (77-79)
src/datamodel_code_generator/parser/jsonschema.py (2)
  • has_default (422-424)
  • get_object_field (1031-1088)
tests/data/expected/main/jsonschema/jsonschema_default_values_override.py (3)
tests/data/expected/main/graphql/default_values_required_use_default.py (1)
  • User (29-33)
tests/data/expected/main/openapi/default_values_parameters_use_default.py (1)
  • User (19-21)
src/datamodel_code_generator/model/base.py (1)
  • name (827-829)
tests/data/expected/main/graphql/default_values_required_use_default.py (2)
src/datamodel_code_generator/model/type_alias.py (1)
  • TypeAlias (37-42)
tests/data/expected/main/jsonschema/jsonschema_default_values_override.py (1)
  • User (16-19)
src/datamodel_code_generator/__main__.py (2)
src/datamodel_code_generator/util.py (1)
  • field_validator (175-191)
src/datamodel_code_generator/watch.py (1)
  • watch_and_regenerate (23-67)
src/datamodel_code_generator/parser/graphql.py (1)
src/datamodel_code_generator/reference.py (3)
  • resolve_default_value (1199-1236)
  • get_valid_field_name_and_alias (304-354)
  • get_valid_field_name_and_alias (1171-1197)
🪛 Ruff (0.14.10)
src/datamodel_code_generator/reference.py

1203-1203: Unused noqa directive (non-enabled: FBT001)

Remove unused noqa directive

(RUF100)

src/datamodel_code_generator/parser/openapi.py

505-505: Unused noqa directive (non-enabled: PLR0912, PLR0914)

Remove unused noqa directive

(RUF100)

src/datamodel_code_generator/__main__.py

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

Remove unused noqa directive

(RUF100)


1204-1204: Unused noqa directive (non-enabled: T201)

Remove unused noqa directive

(RUF100)


1207-1207: Unused noqa directive (non-enabled: T201)

Remove unused noqa directive

(RUF100)


1210-1210: Unused noqa directive (non-enabled: T201)

Remove unused noqa directive

(RUF100)

⏰ 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). (17)
  • GitHub Check: 3.10 on macOS
  • GitHub Check: py312-isort5 on Ubuntu
  • GitHub Check: py312-black22 on Ubuntu
  • GitHub Check: py312-isort7 on Ubuntu
  • GitHub Check: py312-black24 on Ubuntu
  • GitHub Check: py312-isort6 on Ubuntu
  • GitHub Check: 3.10 on Ubuntu
  • GitHub Check: 3.11 on Ubuntu
  • GitHub Check: 3.12 on macOS
  • GitHub Check: 3.13 on macOS
  • GitHub Check: 3.10 on Windows
  • GitHub Check: 3.13 on Windows
  • GitHub Check: 3.14 on Windows
  • GitHub Check: 3.11 on macOS
  • GitHub Check: 3.12 on Windows
  • GitHub Check: benchmarks
  • GitHub Check: Analyze (python)
🔇 Additional comments (36)
tests/data/expected/main/input_model/config_class.py (1)

241-241: LGTM! Auto-generated field addition is correct.

The new default_value_overrides field follows the established pattern for optional configuration fields in the GenerateConfig TypedDict. The type NotRequired[Mapping[str, Any] | None] appropriately allows omission, None, or a mapping structure for field default value overrides.

Note: This file is auto-generated by datamodel-codegen itself (as indicated by the header), so the actual source for this change is in the generator's configuration schema.

src/datamodel_code_generator/prompt_data.py (1)

36-36: LGTM! Auto-generated documentation entry is clear and accurate.

The description "Override field default values from external JSON file" is concise and accurately describes the --default-values option's purpose.

Note: This file is auto-generated (header specifies: "DO NOT EDIT MANUALLY. Run: python scripts/build_prompt_data.py"), so the actual source is in the CLI documentation markers elsewhere in the codebase.

tests/data/expected/main/graphql/default_values_required_use_default.py (1)

1-33: LGTM! Test expectation file demonstrates the feature correctly.

This generated output properly demonstrates the default value override feature in action. Line 32 shows the status field with the overridden default value 'active', applied to an optional field (String | None = 'active'). The GraphQL scalar type aliases and model structure follow the expected patterns for GraphQL schema code generation.

tests/main/conftest.py (1)

71-71: LGTM! Test data path constant follows established conventions.

The new DEFAULT_VALUES_DATA_PATH constant follows the existing pattern for test data paths (consistent naming, type annotation, and placement alongside similar constants like ALIASES_DATA_PATH). This will enable test files to consistently reference the default-values test data directory.

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

65-65: LGTM! Documentation entry is correctly categorized and formatted.

The --default-values option is appropriately placed in the Field Customization section with a clear description and proper link to the detailed documentation.


230-230: LGTM! Alphabetical index entry is correctly positioned.

The entry is properly placed alphabetically (between --dataclass-arguments and --disable-appending-item-suffix) and uses consistent formatting with the other index entries.

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

166-166: LGTM!

The new default_value_overrides field is correctly typed and properly marked as optional using NotRequired.

src/datamodel_code_generator/cli_options.py (1)

142-142: LGTM!

The CLI option metadata is correctly registered under the Field Customization category, which is appropriate for an option that controls field default values.

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

13-13: LGTM!

Documentation correctly updated to reflect the new --default-values option with the field count incremented from 22 to 23 and the option properly indexed.

Also applies to: 63-63

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

150-150: LGTM!

The default_value_overrides field is correctly added to ParserConfigDict with consistent typing.

src/datamodel_code_generator/config.py (1)

203-203: All verification points are satisfied.

The default_value_overrides field is properly integrated:

  1. CLI argument --default-values is defined in arguments.py (lines 829-841) with comprehensive help text describing JSON format and hierarchical (scoped and flat) key support
  2. JSON file loading and parsing logic exists in main.py (lines 1196-1211) with proper error handling for JSONDecodeError, type validation, and string key validation
  3. File path validation is handled by the validate_file() field validator in the Config class, which opens the file in read-text mode and validates the path exists
  4. The field is properly wired through the codebase: passed to the generator, used in reference.py for field-level default overrides, and integrated with watch mode
src/datamodel_code_generator/arguments.py (1)

828-839: --default-values option wiring matches existing CLI patterns

The new --default-values argument cleanly mirrors --aliases (same group, type=Path, rich help text) and exposes the documented flat vs scoped override behavior without impacting other options. Looks good.

docs/cli-reference/field-customization.md (1)

9-9: Index entry for --default-values is correctly wired

The new table row correctly links to #default-values and succinctly summarizes the option. No issues here.

src/datamodel_code_generator/parser/base.py (1)

879-901: ModelResolver now correctly receives default_value_overrides

Threading config.default_value_overrides into the main ModelResolver instance is consistent with how other config knobs are passed and confines overrides to the central resolver used for field/class resolution. The change is minimal and looks safe.

tests/main/graphql/test_main_graphql.py (1)

788-801: LGTM! Well-structured test for default values override feature.

The test appropriately exercises the interaction between --use-default and --default-values flags in the GraphQL code generation path, following the established test patterns in this file.

tests/main/test_public_api_signature_baseline.py (2)

77-77: LGTM! Backward-compatible API extension.

The addition of the optional default_value_overrides parameter maintains backward compatibility while extending the API to support the new feature.


212-212: LGTM! Consistent parameter addition.

The default_value_overrides parameter is added consistently to the Parser baseline, matching the pattern used in the generate function baseline.

src/datamodel_code_generator/watch.py (2)

28-28: LGTM! Consistent parameter threading for watch mode.

The addition of default_value_overrides parameter ensures that watch mode respects default value overrides, maintaining feature parity with one-shot generation.


59-59: LGTM! Parameter correctly forwarded.

The default_value_overrides parameter is properly passed through to run_generate_from_config, ensuring the overrides are applied during regeneration on file changes.

tests/main/test_main_general.py (1)

2186-2209: LGTM! Comprehensive error handling tests.

These tests appropriately verify error handling for the --default-values option, covering both invalid JSON syntax and incorrect data type scenarios. The error messages are clear and actionable for users.

tests/data/expected/main/jsonschema/jsonschema_default_values_override.py (1)

12-13: No changes required. The __root__: Any usage is correct.

This file represents the expected output with default Pydantic version (v1 compatibility). The codebase uses a consistent naming convention: expected output files without a version suffix generate Pydantic v1 syntax (using __root__), while files with an explicit _pydantic_v2 suffix generate Pydantic v2 syntax (using RootModel[T]). Since there is no corresponding jsonschema_default_values_override_pydantic_v2.py variant for this test, the v1 output shown is the intended behavior.

src/datamodel_code_generator/parser/jsonschema.py (3)

2421-2427: Good implementation of default value resolution.

The integration correctly:

  • Uses original_field_name (not sanitized field_name) for override lookup, ensuring schema-level matching
  • Passes class_name to enable scoped overrides (ClassName.field_name)
  • Resolves overrides before field construction

2429-2434: Correct integration of override defaults with required field logic.

The code correctly uses effective_has_default (line 2430) instead of field.has_default when determining if a field should be required. This ensures that fields with overridden defaults are properly marked as optional when apply_default_values_for_required_fields is enabled.


1064-1082: Proper propagation of resolved default values to field construction.

The resolved default_value and has_default are correctly used in:

  • Field default assignment (line 1064)
  • Nullable determination with strict_nullable mode (line 1072)
  • Field metadata (line 1082)

This ensures the override values are consistently applied throughout field creation.

tests/data/expected/main/openapi/default_values_parameters_use_default.py (1)

16-16: No changes required. This test data file is specifically for Pydantic v1 code generation. The generator correctly uses parse_obj() for v1 output and has a separate Pydantic v2 implementation that uses model_validate() instead. The use of parse_obj() in this file is appropriate for the v1 target.

Likely an incorrect or invalid review comment.

tests/main/jsonschema/test_main_jsonschema.py (1)

28-41: DEFAULT_VALUES_DATA_PATH import is consistent with existing test data wiring

The added DEFAULT_VALUES_DATA_PATH import matches the existing pattern (e.g., ALIASES_DATA_PATH) and keeps all test data paths centralized in tests.main.conftest. No issues here.

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

34-45: Importing DEFAULT_VALUES_DATA_PATH fits existing test helpers pattern

Bringing in DEFAULT_VALUES_DATA_PATH next to DATA_PATH keeps the test data wiring consistent and avoids hard-coded paths; no issues here.


4693-4709: New OpenAPI test for --use-default + --default-values looks well‑wired

The test name, docstring, input/output fixtures, and CLI arguments are consistent with the rest of the OpenAPI suite and correctly scope generation to paths and parameters while feeding the JSON defaults file. Nothing to change from a logic perspective.

src/datamodel_code_generator/parser/graphql.py (1)

334-422: GraphQL default override flow in parse_field is coherent with the new resolver API

Using original_field_name and class_name to feed resolve_default_value, then basing required on effective_has_default when apply_default_values_for_required_fields is enabled, lines up with how JsonSchema/OpenAPI now behave. Propagating effective_default/effective_has_default into the constructed field ensures Pydantic/msgspec generation sees the resolved defaults correctly. No functional issues spotted here.

Please just confirm your GraphQL default override documentation (or examples) assumes keys of the form TypeName.fieldName (matching obj.name), since that’s what this implementation uses for the class_name scope.

src/datamodel_code_generator/reference.py (2)

550-550: LGTM: Parameter addition follows existing patterns.

The default_value_overrides parameter is appropriately typed and positioned to maintain backward compatibility.


611-614: LGTM: Instance variable storage follows best practices.

The defensive copy pattern {**default_value_overrides} prevents external mutation, and the empty dict default is consistent with how aliases are handled.

src/datamodel_code_generator/__main__.py (5)

179-179: LGTM: Field validator correctly includes the new field.

Adding "default_values" to the validator ensures the file is validated and opened consistently with other JSON file inputs like aliases.


866-866: LGTM: Function parameter addition maintains backward compatibility.

The default_value_overrides parameter is correctly positioned at the end with an appropriate default value.


993-993: LGTM: Correctly propagates overrides to generation.

The parameter is appropriately passed to the generate() function call.


1278-1278: LGTM: Correctly passes overrides to generation.

The default_value_overrides is properly threaded through the main generation path.


1327-1329: LGTM: Watch mode correctly receives overrides.

The default_value_overrides parameter is properly passed to watch_and_regenerate, ensuring consistent behavior between one-shot generation and watch mode.

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 (2)
src/datamodel_code_generator/reference.py (2)

611-614: Consider clarifying the comment to be more general.

The comment on line 611 specifically mentions "external JSON file", but the default_value_overrides parameter accepts mappings from any source (CLI, config files, programmatic, etc.). Consider rewording to be more generic, e.g., "Default value overrides for field defaults".

🔎 Suggested improvement
-        # Default value overrides from external JSON file
+        # Default value overrides for field defaults
         self.default_value_overrides: Mapping[str, Any] = (
             {} if default_value_overrides is None else {**default_value_overrides}
         )

1203-1203: Remove unused noqa directive.

The # noqa: FBT001 comment on line 1203 is unnecessary because the FBT001 rule (boolean positional argument) is not enabled in this project's configuration.

🔎 Proposed fix
-        has_default: bool,  # noqa: FBT001
+        has_default: bool,

Based on static analysis.

📜 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 2793acb and f59ca58.

⛔ Files ignored due to path filters (1)
  • tests/data/default_values/graphql_user_defaults.json is excluded by !tests/data/**/*.json and included by none
📒 Files selected for processing (2)
  • src/datamodel_code_generator/reference.py
  • tests/data/expected/main/graphql/default_values_required_use_default.py
🧰 Additional context used
🧬 Code graph analysis (2)
tests/data/expected/main/graphql/default_values_required_use_default.py (1)
src/datamodel_code_generator/model/type_alias.py (1)
  • TypeAlias (37-42)
src/datamodel_code_generator/reference.py (2)
src/datamodel_code_generator/parser/jsonschema.py (1)
  • has_default (422-424)
src/datamodel_code_generator/model/base.py (2)
  • class_name (848-850)
  • class_name (853-857)
🪛 Ruff (0.14.10)
src/datamodel_code_generator/reference.py

1203-1203: Unused noqa directive (non-enabled: FBT001)

Remove unused noqa directive

(RUF100)

⏰ 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). (9)
  • GitHub Check: 3.11 on macOS
  • GitHub Check: py312-isort6 on Ubuntu
  • GitHub Check: 3.13 on Windows
  • GitHub Check: 3.14 on Windows
  • GitHub Check: benchmarks
  • GitHub Check: 3.10 on Windows
  • GitHub Check: 3.12 on Windows
  • GitHub Check: 3.11 on Windows
  • GitHub Check: Analyze (python)
🔇 Additional comments (4)
tests/data/expected/main/graphql/default_values_required_use_default.py (2)

1-26: LGTM! Clean GraphQL type mappings.

The file header, imports, and TypeAlias definitions follow standard conventions for generated GraphQL-to-Python mappings. The use of from __future__ import annotations ensures compatibility with union syntax across Python versions, and the comprehensive docstrings properly document each GraphQL scalar type.


29-33: LGTM! User model correctly demonstrates default value overrides.

The User model structure is appropriate for testing the new --default-values feature:

  • The required id field (no default) contrasts properly with optional fields
  • The name and status fields demonstrate the default value override behavior ('default_user', 'active')
  • The typename__ field with __typename alias follows standard GraphQL introspection conventions
src/datamodel_code_generator/reference.py (2)

550-550: LGTM! Parameter addition follows best practices.

The new default_value_overrides parameter is correctly placed at the end for backward compatibility, uses an immutable Mapping interface, and follows the standard optional parameter pattern.


1199-1234: Excellent implementation! Clear hierarchical resolution logic.

The resolve_default_value method is well-designed:

  • Clean priority system (scoped → flat → original) that mirrors the existing alias resolution pattern
  • Clear documentation explaining the override hierarchy
  • Efficient early return when no overrides configured
  • Consistent with the pattern used in get_valid_field_name_and_alias (lines 304-354)

The method signature and return type are appropriate for the use case described in the AI summary.

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 (8)
src/datamodel_code_generator/__main__.py (4)

42-42: Remove unused # noqa: TC003 (Ruff RUF100).

Ruff reports this TC003 ignore as unused. Since this line is already compliant, you can safely drop the # noqa: TC003 here (or enable TC003 globally if you really want to keep it).


506-506: Ruff: unused # noqa: UP045 on default_values.

Ruff flags this UP045 ignore as unused. Given the surrounding fields already use the same pattern, either enable UP045 in your config or drop the directive on this new line for a clean lint run.


889-899: Extend run_generate_from_config alias type hint to allow list-valued aliases.

run_generate_from_config still annotates aliases as dict[str, str] | None, but _validate_aliases and FieldNameResolver now support Mapping[str, str | list[str]]. At runtime this works fine, but the type hint is now misleading for callers and static type checkers.

Consider widening the annotation, e.g.:

Suggested type-hint adjustment
-def run_generate_from_config(  # noqa: PLR0913, PLR0917
-    config: Config,
-    input_: Path | str | ParseResult,
-    output: Path | None,
-    extra_template_data: dict[str, Any] | None,
-    aliases: dict[str, str] | None,
+def run_generate_from_config(  # noqa: PLR0913, PLR0917
+    config: Config,
+    input_: Path | str | ParseResult,
+    output: Path | None,
+    extra_template_data: dict[str, Any] | None,
+    aliases: dict[str, str | list[str]] | None,

Also applies to: 1025-1026


1231-1246: Ruff: unused # noqa: T201 on new print calls.

Ruff flags the # noqa: T201 ignores here as unused. Since these print-to-stderr diagnostics are intentional and other lines already carry the same ignore, you can either:

  • Enable T201 in Ruff so the directives have an effect, or
  • Drop the # noqa: T201 on these new lines for consistency with your current Ruff config.
src/datamodel_code_generator/reference.py (2)

1199-1233: Default override resolution logic is clear; consider skipping the scoped key when class_name is None.

The scoped‑then‑flat lookup order (ClassName.field then field) is straightforward and matches the documented behavior. Two minor nits:

  • When class_name is None, you still compute scoped_key = "None.<field>", which is harmless but a bit odd.
  • If you ever expose default_value_overrides externally, this can surprise users who accidentally use "None.foo" keys.

You could tighten this slightly:

Possible refinement
-        scoped_key = f"{class_name}.{field_name}"
-        if scoped_key in self.default_value_overrides:
-            return self.default_value_overrides[scoped_key], True
+        if class_name is not None:
+            scoped_key = f"{class_name}.{field_name}"
+            if scoped_key in self.default_value_overrides:
+                return self.default_value_overrides[scoped_key], True

Functionally it’s already correct; this would just avoid constructing meaningless scoped keys.


1203-1203: Ruff: unused # noqa: FBT001 on has_default parameter.

Ruff reports this FBT001 ignore as unused. Either enable FBT001 globally or drop the directive here to keep lint noise down.

src/datamodel_code_generator/parser/jsonschema.py (2)

2379-2445: parse_object_fields override integration looks sound; note coverage for synthesized allOf fields.

The flow here:

  • Resolves effective_default, effective_has_default via model_resolver.resolve_default_value using the original schema field name and class name.
  • Uses effective_has_default in the required‑flag calculation, so overrides participate in --use-default semantics.
  • Passes the effective pair into get_object_field, which in turn adjusts default, has_default, and nullable.

This aligns with the intended behavior of --default-values for JSON Schema object properties.

One nuance: synthesized fields created in _parse_all_of_item for required‑but‑missing properties are still built via data_model_field_type(...) directly and don’t go through resolve_default_value. If you expect overrides to apply to those synthetic required fields as well, you may want to route them through resolve_default_value (or get_object_field) in a follow‑up.


856-860: Ruff: unused # noqa: FBT001 on _create_synthetic_enum_obj’s nullable parameter.

Ruff reports the FBT001 ignore on nullable as unused. As with other similar hints, you can either enable FBT001 or drop the directive to keep lint output clean.

📜 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 f59ca58 and 9bf0998.

📒 Files selected for processing (5)
  • src/datamodel_code_generator/__main__.py
  • src/datamodel_code_generator/parser/graphql.py
  • src/datamodel_code_generator/parser/jsonschema.py
  • src/datamodel_code_generator/parser/openapi.py
  • src/datamodel_code_generator/reference.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/datamodel_code_generator/parser/graphql.py
🧰 Additional context used
🧬 Code graph analysis (3)
src/datamodel_code_generator/parser/jsonschema.py (2)
src/datamodel_code_generator/model/base.py (5)
  • DataModelFieldBase (151-469)
  • field (404-406)
  • nullable (903-905)
  • class_name (848-850)
  • class_name (853-857)
src/datamodel_code_generator/reference.py (1)
  • resolve_default_value (1199-1233)
src/datamodel_code_generator/__main__.py (2)
src/datamodel_code_generator/util.py (1)
  • field_validator (175-191)
src/datamodel_code_generator/watch.py (1)
  • watch_and_regenerate (23-67)
src/datamodel_code_generator/parser/openapi.py (2)
src/datamodel_code_generator/reference.py (2)
  • resolve_default_value (1199-1233)
  • reference (77-79)
src/datamodel_code_generator/parser/jsonschema.py (2)
  • has_default (422-424)
  • get_object_field (1031-1087)
🪛 Ruff (0.14.10)
src/datamodel_code_generator/__main__.py

42-42: Unused noqa directive (non-enabled: TC003)

Remove unused noqa directive

(RUF100)


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

Remove unused noqa directive

(RUF100)


1231-1231: Unused noqa directive (non-enabled: T201)

Remove unused noqa directive

(RUF100)


1238-1238: Unused noqa directive (non-enabled: T201)

Remove unused noqa directive

(RUF100)


1245-1245: Unused noqa directive (non-enabled: T201)

Remove unused noqa directive

(RUF100)

src/datamodel_code_generator/parser/openapi.py

505-505: Unused noqa directive (non-enabled: PLR0912, PLR0914)

Remove unused noqa directive

(RUF100)

src/datamodel_code_generator/reference.py

1203-1203: Unused noqa directive (non-enabled: FBT001)

Remove unused noqa directive

(RUF100)

⏰ 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). (13)
  • GitHub Check: 3.14 on macOS
  • GitHub Check: py312-isort6 on Ubuntu
  • GitHub Check: 3.11 on Ubuntu
  • GitHub Check: 3.10 on Ubuntu
  • GitHub Check: 3.10 on Windows
  • GitHub Check: 3.12 on Windows
  • GitHub Check: 3.13 on Ubuntu
  • GitHub Check: 3.12 on macOS
  • GitHub Check: 3.14 on Windows
  • GitHub Check: 3.11 on Windows
  • GitHub Check: 3.13 on Windows
  • GitHub Check: benchmarks
  • GitHub Check: Analyze (python)
🔇 Additional comments (9)
src/datamodel_code_generator/__main__.py (4)

179-191: Config default_values wiring and file validation look consistent.

Including "default_values" in validate_file and adding default_values: Optional[TextIOBase] on Config matches how aliases/extra_template_data/custom_formatters_kwargs are handled and keeps IO concerns in one place. No functional issues spotted.

Also applies to: 506-507


857-887: _load_json_config helper is well-factored; minor behavioral note.

The helper cleanly centralizes JSON loading/validation and ensures file handles are closed. Behavior and error messages are consistent with the existing extra_template_data handling. Only note is that validator is assumed to be total; if it ever raises instead of returning a string, that will bubble up — which is acceptable for a CLI but worth keeping in mind.


1208-1247: JSON config validation for aliases/defaults/formatters is robust and aligned.

The three validators (_validate_aliases, _validate_string_key_dict, _validate_string_mapping) paired with _load_json_config give clear, early CLI feedback and constrain the shapes as expected:

  • aliases: str -> str | list[str]
  • default_values: str -> Any
  • custom_formatters_kwargs: str -> str

The error messages are precise and consistent with the existing extra_template_data error style. No logic issues spotted.


1283-1293: default_value_overrides is correctly threaded through generate and watch paths.

Passing default_value_overrides into run_generate_from_config (both initial run and watch loop) ensures overrides apply consistently for:

  • direct CLI invocations
  • --check mode (through the temp output)
  • --watch regenerations

The pattern mirrors how aliases and custom_formatters_kwargs are propagated. No issues found.

Also applies to: 1337-1343

src/datamodel_code_generator/reference.py (1)

524-551: ModelResolver’s default_value_overrides storage is safe and side-effect free.

Taking default_value_overrides: Mapping[str, Any] | None and normalizing to a new dict ({**default_value_overrides}) avoids accidental mutation of caller-owned mappings while keeping lookup O(1). This is a good choice given how frequently resolution is called.

Also applies to: 611-615

src/datamodel_code_generator/parser/jsonschema.py (1)

1031-1046: get_object_field correctly incorporates effective defaults without breaking existing callers.

Using effective_default / effective_has_default only when effective_has_default is not None preserves existing behavior for all legacy call sites, while allowing override-aware callers to:

  • set both default and has_default on the DataModelFieldBase
  • influence strict‑nullable handling via the updated has_default value

The nullable expression reusing the new has_default is consistent with how root and array paths already behave.

Also applies to: 1061-1087

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

538-556: LGTM! Default value override resolution is correctly implemented.

The code properly resolves default values with hierarchical overrides (class.field → field) and correctly computes effective_required based on the apply_default_values_for_required_fields flag and presence of an effective default. The resolved values are appropriately passed to get_object_field.


585-633: LGTM! Consistent default value resolution for content-based parameters.

The non-schema parameter branch correctly mirrors the pattern from the schema branch:

  • Properly extracts original defaults from object_schema when available
  • Applies the same hierarchical override resolution
  • Computes effective_required with identical logic
  • Consistently uses effective values throughout field construction, including the nullable determination

505-505: Remove unused noqa codes.

The noqa directive includes PLR0912 and PLR0914, but these rules are not enabled in your linting configuration. Keep only the enabled rules in the noqa directive.

🔎 Proposed fix
-    def parse_all_parameters(  # noqa: PLR0912, PLR0914
+    def parse_all_parameters(  # noqa: PLR0914
         self,
         name: str,
         parameters: list[ReferenceObject | ParameterObject],

Likely an incorrect or invalid review comment.

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

🧹 Nitpick comments (3)
src/datamodel_code_generator/reference.py (1)

611-614: Revise comment to be implementation-agnostic.

The comment mentions "external JSON file", but this attribute accepts any Mapping regardless of source. The comment should be more generic.

🔎 Suggested revision
-        # Default value overrides from external JSON file
+        # Default value overrides for field defaults
         self.default_value_overrides: Mapping[str, Any] = (
             {} if default_value_overrides is None else {**default_value_overrides}
         )
tests/main/jsonschema/test_main_jsonschema.py (2)

7461-7483: CLI doc + default-values override test are consistent with existing patterns

The new @pytest.mark.cli_doc block and test_main_jsonschema_default_values_override are wired consistently with other CLI doc tests:

  • options, input_schema, cli_args, and golden_output align with the runtime test configuration.
  • The CLI docs use a relative path (default_values/scoped_defaults.json) while the test uses DEFAULT_VALUES_DATA_PATH / "scoped_defaults.json", matching how aliases are handled elsewhere.

If you want to make the docs a bit clearer, you could optionally mention how --default-values interacts with --use-default (e.g., that it can override schema defaults and/or supply defaults where none exist), but functionally this looks good.


7486-7499: allOf + --use-default coverage for default overrides looks solid

test_main_jsonschema_default_values_allof gives good coverage for the interaction between --use-default and --default-values in an allOf inheritance scenario, and the paths/expected filename are consistent with the rest of the suite.

If you later expand this area, you might consider an additional case without --use-default (pure override of existing schema defaults only), but that’s not strictly necessary for this PR.

📜 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 9bf0998 and 169bacf.

⛔ Files ignored due to path filters (3)
  • tests/data/default_values/allof_defaults.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/default_values/invalid_formatters_kwargs.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/jsonschema/default_values_allof.json is excluded by !tests/data/**/*.json and included by none
📒 Files selected for processing (4)
  • src/datamodel_code_generator/reference.py
  • tests/data/expected/main/jsonschema/jsonschema_default_values_allof.py
  • tests/main/jsonschema/test_main_jsonschema.py
  • tests/main/test_main_general.py
✅ Files skipped from review due to trivial changes (1)
  • tests/data/expected/main/jsonschema/jsonschema_default_values_allof.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/main/test_main_general.py
🧰 Additional context used
🧬 Code graph analysis (1)
src/datamodel_code_generator/reference.py (2)
src/datamodel_code_generator/parser/jsonschema.py (1)
  • has_default (422-424)
src/datamodel_code_generator/model/base.py (2)
  • class_name (848-850)
  • class_name (853-857)
🪛 Ruff (0.14.10)
src/datamodel_code_generator/reference.py

1203-1203: Unused noqa directive (non-enabled: FBT001)

Remove unused noqa directive

(RUF100)

⏰ 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). (13)
  • GitHub Check: 3.14 on macOS
  • GitHub Check: 3.11 on Windows
  • GitHub Check: py312-isort7 on Ubuntu
  • GitHub Check: 3.10 on Ubuntu
  • GitHub Check: py312-pydantic1 on Ubuntu
  • GitHub Check: 3.10 on Windows
  • GitHub Check: py312-isort6 on Ubuntu
  • GitHub Check: 3.12 on Windows
  • GitHub Check: py312-black23 on Ubuntu
  • GitHub Check: 3.13 on Windows
  • GitHub Check: 3.14 on Windows
  • GitHub Check: Analyze (python)
  • GitHub Check: benchmarks
🔇 Additional comments (2)
src/datamodel_code_generator/reference.py (1)

550-550: LGTM!

The new parameter is well-typed and properly positioned as an optional parameter with a None default, maintaining backward compatibility.

tests/main/jsonschema/test_main_jsonschema.py (1)

28-41: Shared DEFAULT_VALUES_DATA_PATH import looks correct

Importing DEFAULT_VALUES_DATA_PATH alongside the other shared test data paths keeps the JSON override file locations centralized and mirrors the existing ALIASES_DATA_PATH pattern; no changes needed here.

Comment thread src/datamodel_code_generator/reference.py
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/reference.py (1)

1199-1218: Previous issue resolved; remove unused noqa directive.

The logic correctly implements hierarchical override resolution (scoped → flat → original), and the critical issue from the previous review (flat overrides being skipped when class_name is None) has been properly addressed.

However, the noqa: FBT001 directive on line 1203 is unused and should be removed.

🔎 Proposed fix
     def resolve_default_value(
         self,
         field_name: str,
         original_default: Any,
-        has_default: bool,  # noqa: FBT001
+        has_default: bool,
         class_name: str | None,
     ) -> tuple[Any, bool]:
📜 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 169bacf and 2ec4de8.

📒 Files selected for processing (1)
  • src/datamodel_code_generator/reference.py
🧰 Additional context used
🧬 Code graph analysis (1)
src/datamodel_code_generator/reference.py (2)
src/datamodel_code_generator/parser/jsonschema.py (1)
  • has_default (422-424)
src/datamodel_code_generator/model/base.py (2)
  • class_name (848-850)
  • class_name (853-857)
🪛 Ruff (0.14.10)
src/datamodel_code_generator/reference.py

1203-1203: Unused noqa directive (non-enabled: FBT001)

Remove unused noqa directive

(RUF100)

⏰ 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). (14)
  • GitHub Check: py312-isort7 on Ubuntu
  • GitHub Check: py312-black23 on Ubuntu
  • GitHub Check: py312-black22 on Ubuntu
  • GitHub Check: 3.10 on macOS
  • GitHub Check: 3.10 on Windows
  • GitHub Check: 3.13 on Windows
  • GitHub Check: 3.11 on Windows
  • GitHub Check: 3.10 on Ubuntu
  • GitHub Check: 3.12 on Windows
  • GitHub Check: 3.13 on macOS
  • GitHub Check: 3.14 on Ubuntu
  • GitHub Check: 3.14 on Windows
  • GitHub Check: Analyze (python)
  • GitHub Check: benchmarks
🔇 Additional comments (2)
src/datamodel_code_generator/reference.py (2)

550-550: LGTM! Well-designed parameter addition.

The new default_value_overrides parameter is appropriately optional with a sensible default, uses the immutable Mapping interface, and is positioned at the end to maintain backward compatibility.


611-614: LGTM! Proper defensive initialization.

The attribute initialization correctly creates a defensive copy of the provided mapping to prevent external mutations, with a clean fallback to an empty dict.

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/reference.py (1)

1199-1217: LGTM: Correct override resolution logic.

The implementation now properly supports both scoped (ClassName.field_name) and flat (field_name) overrides. The previous issue where flat overrides were skipped when class_name is None has been successfully addressed by removing the class_name is None check from the early return.

The logic flow is clean: check for overrides → try scoped key → try flat key → fallback to original.

📝 Optional cleanup: Remove unused noqa directive

Line 1203 has an unused noqa directive since FBT001 is not enabled in the Ruff configuration:

-    has_default: bool,  # noqa: FBT001
+    has_default: bool,
📜 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 2ec4de8 and 0f88967.

📒 Files selected for processing (1)
  • src/datamodel_code_generator/reference.py
🧰 Additional context used
🧬 Code graph analysis (1)
src/datamodel_code_generator/reference.py (2)
src/datamodel_code_generator/parser/jsonschema.py (1)
  • has_default (422-424)
src/datamodel_code_generator/model/base.py (2)
  • class_name (848-850)
  • class_name (853-857)
🪛 Ruff (0.14.10)
src/datamodel_code_generator/reference.py

1203-1203: Unused noqa directive (non-enabled: FBT001)

Remove unused noqa directive

(RUF100)

🔇 Additional comments (2)
src/datamodel_code_generator/reference.py (2)

550-550: LGTM: Clean parameter addition.

The parameter type Mapping[str, Any] | None correctly uses the read-only Mapping interface, allowing callers flexibility while preventing unintended mutations.


611-614: LGTM: Proper defensive initialization.

Creating a defensive copy with {**default_value_overrides} prevents external mutation, and storing as Mapping[str, Any] clearly signals the immutable interface.

@koxudaxi koxudaxi merged commit 689cc79 into main Jan 2, 2026
40 checks passed
@koxudaxi koxudaxi deleted the feature/default-values-override branch January 2, 2026 16:17
@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 --default-values CLI option and corresponding default_value_overrides parameter to the Python API. All changes are purely additive: (1) New CLI option with no changes to existing options, (2) New optional parameter added to GenerateConfig, ParserConfig, and TypedDict types with default value of None, (3) The generate() function uses **options with Unpack[GenerateConfigDict] so existing calls are unaffected, (4) No changes to generated code output for users not using this feature, (5) No changes to default behavior or required template updates. The test baseline file was updated to include the new parameter, confirming backward compatibility. Existing users will experience no changes unless they explicitly opt-in to use the new feature.


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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant