Fix hostname format with field_constraints to use Field(pattern=...)#2724
Fix hostname format with field_constraints to use Field(pattern=...)#2724
Conversation
WalkthroughAdds a HOSTNAME_REGEX constant and exposes it on DataTypeManager across type modules; extends DataTypeManager.get_data_type with a keyword-only field_constraints flag; and threads field_constraints through the JSON Schema parser to inject hostname pattern constraints into generated types and tests. Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro 📒 Files selected for processing (8)
🚧 Files skipped from review as they are similar to previous changes (1)
🧰 Additional context used🧬 Code graph analysis (4)src/datamodel_code_generator/model/pydantic/types.py (2)
tests/data/expected/main/jsonschema/hostname_field_constraints_strict_pydantic_v1.py (2)
tests/data/expected/main/jsonschema/hostname_field_constraints_pydantic_v1.py (2)
tests/data/expected/main/jsonschema/hostname_root_type_pydantic_v2.py (1)
⏰ 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). (10)
🔇 Additional comments (8)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (9)
src/datamodel_code_generator/model/pydantic/types.py(3 hunks)src/datamodel_code_generator/model/pydantic_v2/types.py(2 hunks)src/datamodel_code_generator/model/types.py(4 hunks)src/datamodel_code_generator/parser/jsonschema.py(4 hunks)src/datamodel_code_generator/types.py(1 hunks)tests/data/expected/main/jsonschema/hostname_field_constraints_pydantic_v2.py(1 hunks)tests/data/expected/main/openapi/pattern/msgspec_pattern.py(1 hunks)tests/data/jsonschema/hostname_field_constraints.json(1 hunks)tests/main/jsonschema/test_main_jsonschema.py(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
tests/main/jsonschema/test_main_jsonschema.py (2)
tests/main/conftest.py (2)
output_file(98-100)run_main_and_assert(244-408)tests/test_main_kr.py (1)
output_file(44-46)
src/datamodel_code_generator/model/pydantic/types.py (6)
src/datamodel_code_generator/model/pydantic_v2/types.py (1)
DataTypeManager(66-149)src/datamodel_code_generator/model/types.py (1)
DataTypeManager(71-115)src/datamodel_code_generator/types.py (3)
DataTypeManager(788-863)Types(749-785)StrictTypes(101-108)src/datamodel_code_generator/model/dataclass.py (1)
DataTypeManager(205-251)src/datamodel_code_generator/model/msgspec.py (1)
DataTypeManager(440-486)src/datamodel_code_generator/parser/base.py (1)
data_type(980-982)
⏰ 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). (8)
- GitHub Check: benchmarks
- GitHub Check: 3.13 on macOS
- GitHub Check: 3.11 on Windows
- GitHub Check: 3.10 on Windows
- GitHub Check: 3.14 on Windows
- GitHub Check: 3.13 on Windows
- GitHub Check: 3.14 on Ubuntu
- GitHub Check: 3.12 on Windows
🔇 Additional comments (16)
tests/data/expected/main/openapi/pattern/msgspec_pattern.py (1)
13-21: LGTM!The hostname field now correctly uses
Annotated[str, Meta(pattern=...)]with the hostname regex pattern, consistent with the other pattern-constrained fields in this model.src/datamodel_code_generator/parser/jsonschema.py (4)
1021-1022: LGTM!The hostname pattern injection is correctly placed after the constraints dict is created and before field creation. The conditions properly guard the injection to only apply when field_constraints is enabled and the format is "hostname".
1067-1072: LGTM!The
field_constraintsparameter is correctly propagated to the data type manager, enabling format-aware type resolution when field constraints mode is active.
2584-2586: LGTM!The hostname pattern injection in
parse_root_typemirrors the logic inget_object_field, ensuring consistent behavior across both object field and root type parsing paths.
2649-2659: LGTM!The hostname constraint handling in
_parse_multiple_types_with_propertiesmaintains consistency with the other parsing paths.src/datamodel_code_generator/types.py (1)
794-798: LGTM!The
HOSTNAME_REGEXclass variable provides a centralized hostname validation pattern for the baseDataTypeManager. This enables consistent hostname validation across different model backends while allowing subclasses to override if needed.tests/data/jsonschema/hostname_field_constraints.json (1)
1-11: LGTM!A clean, minimal test schema that properly exercises the hostname format field constraint feature. The structure follows JSON Schema draft-07 conventions.
src/datamodel_code_generator/model/pydantic_v2/types.py (2)
32-36: LGTM!The module-level
HOSTNAME_REGEXconstant is correctly defined with the standard hostname validation pattern.
70-70: LGTM!The class attribute correctly exposes the hostname regex pattern, enabling the parser to access it through the data type manager.
tests/main/jsonschema/test_main_jsonschema.py (1)
1678-1688: LGTM!The test follows the established patterns in this test file and properly validates the fix for issue #1972. It correctly tests that hostname format fields use
Field(pattern=)instead ofconstrwhen--field-constraintsis enabled with Pydantic v2.tests/data/expected/main/jsonschema/hostname_field_constraints_pydantic_v2.py (1)
1-14: LGTM!The expected output correctly demonstrates the fix for issue #1972. The
myhostfield now usesField(pattern=...)instead ofconstr(...)when--field-constraintsis enabled with Pydantic v2, which is the intended behavior.src/datamodel_code_generator/model/pydantic/types.py (2)
161-164: LGTM - HOSTNAME_REGEX constant for Pydantic v1.The
\Zend anchor is appropriate for Pydantic v1's regex validation, which uses Python'sremodule directly. This is consistent with the existing pattern at line 106.
340-365: LGTM - Hostname handling with field_constraints.The logic correctly returns a plain
strtype (orStrictStrif strict mode is enabled) whenfield_constraints=Truefor hostname fields. This allows the JSON Schema parser to attach the hostname regex pattern viaField(pattern=...)instead of usingconstr(...).src/datamodel_code_generator/model/types.py (3)
8-8: LGTM - Import update.The
ClassVarimport is correctly added to support the new class attribute annotation.
74-75: LGTM - Class attribute exposed.The
HOSTNAME_REGEXis correctly exposed as a class variable, allowing access viaDataTypeManager.HOSTNAME_REGEXfor use in the JSON Schema parser.
107-115: LGTM - Interface updated with field_constraints parameter.The
field_constraintsparameter is correctly added as a keyword-only argument with a default value ofFalse. TheARG002noqa comment appropriately indicates the parameter is intentionally unused in this base implementation, establishing the interface for subclasses to override.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2724 +/- ##
=======================================
Coverage 99.35% 99.35%
=======================================
Files 83 83
Lines 11838 11868 +30
Branches 1428 1433 +5
=======================================
+ Hits 11762 11792 +30
Misses 45 45
Partials 31 31
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
CodSpeed Performance ReportMerging #2724 will not alter performanceComparing Summary
Footnotes
|
Fixes: #1972
Summary by CodeRabbit
New Features
Tests
✏️ Tip: You can customize this high-level summary in your review settings.