Commit 160e507
Fix required field default rendering and --use-default nullable types (#3054)
* Fix required fields inconsistently rendering defaults (#3048)
__set_validate_default_on_fields set validate_default=True on fields
with model references without checking field.required, causing required
model-ref fields to render defaults while required scalar fields didn't.
Skip required fields (unless use_default_with_required) so all required
fields consistently have no defaults rendered.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Fix --use-default making required fields nullable
--use-default previously skipped setting field.required=True, which made
fields non-required and therefore nullable (e.g. str | None = 'foo').
Now fields stay required=True with a new use_default_with_required flag
that allows defaults to render without changing the type. Produces
`status: str = 'foo'` instead of `status: str | None = 'foo'`.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Add tests for allOf --use-default and --force-optional coverage
Cover the previously uncovered code paths:
- allOf with $ref + sub-schema required fields + --use-default
- allOf with outer required fields + --force-optional
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Pass use_default_with_required via constructor instead of patching
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Propagate use_default_with_required to missed field-assignment sites
Three regressions where the new use_default_with_required flag wasn't
propagated to all the rendering sites that decide whether a field has
an assignment. Each produces broken output on this branch but the
correct output on baseline (92bdc27).
1. pydantic_base.py:205 — early-return for required+nullable fields
with no other Field args returned Field(...) without checking
use_default_with_required, dropping the user's default.
Repro: --use-default --strict-nullable, required nullable str with
default. Got: x: str | None = Field(...). Want: x: str | None = 'hello'.
2. dataclass.py:has_field_assignment — sort-key helper didn't account
for use_default_with_required, so a required field with default null
(use_default_with_required=True, field.field empty) was sorted as
no-assignment alongside truly required fields. Schema order was then
preserved, placing the defaulted field before the non-defaulted one.
Repro: --use-default --output-model-type dataclasses.dataclass, required
nullable a (default null) before required b. Got: a before b
(TypeError on import). Want: b before a. Same fix covers
pydantic_v2.dataclass since it shares the helper.
3. msgspec.py:_has_field_assignment — same class of bug. Helper checked
neither bool(field.field) nor use_default_with_required, breaking
ordering for both string-defaulted and null-defaulted required fields.
Repro: --use-default --output-model-type msgspec.Struct on the existing
allof_required_use_default.json fixture. Got: name='unnamed' before tag
(TypeError on import). Want: tag before name='unnamed'.
Adds three regression tests in tests/main/jsonschema/test_main_jsonschema.py
covering the three repros above.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* Fix required default handling
---------
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Koudai Aono <koxudaxi@gmail.com>1 parent ece1783 commit 160e507
29 files changed
Lines changed: 317 additions & 61 deletions
File tree
- src/datamodel_code_generator
- model
- template
- pydantic_v2
- parser
- tests
- data
- expected/main
- graphql
- jsonschema
- openapi
- jsonschema
- main/jsonschema
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
173 | 173 | | |
174 | 174 | | |
175 | 175 | | |
| 176 | + | |
176 | 177 | | |
177 | 178 | | |
178 | 179 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
| 42 | + | |
| 43 | + | |
43 | 44 | | |
44 | 45 | | |
45 | 46 | | |
| |||
172 | 173 | | |
173 | 174 | | |
174 | 175 | | |
175 | | - | |
| 176 | + | |
176 | 177 | | |
177 | 178 | | |
178 | 179 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
73 | | - | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
74 | 78 | | |
75 | 79 | | |
76 | 80 | | |
| |||
311 | 315 | | |
312 | 316 | | |
313 | 317 | | |
314 | | - | |
| 318 | + | |
315 | 319 | | |
316 | 320 | | |
317 | 321 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
202 | 202 | | |
203 | 203 | | |
204 | 204 | | |
205 | | - | |
| 205 | + | |
206 | 206 | | |
207 | 207 | | |
208 | 208 | | |
| |||
211 | 211 | | |
212 | 212 | | |
213 | 213 | | |
214 | | - | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
215 | 220 | | |
216 | 221 | | |
217 | 222 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
| 33 | + | |
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | | - | |
| 32 | + | |
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
| 30 | + | |
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
44 | | - | |
| 44 | + | |
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2180 | 2180 | | |
2181 | 2181 | | |
2182 | 2182 | | |
| 2183 | + | |
| 2184 | + | |
2183 | 2185 | | |
2184 | 2186 | | |
2185 | 2187 | | |
| |||
2227 | 2229 | | |
2228 | 2230 | | |
2229 | 2231 | | |
| 2232 | + | |
| 2233 | + | |
2230 | 2234 | | |
2231 | 2235 | | |
2232 | 2236 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
372 | 372 | | |
373 | 373 | | |
374 | 374 | | |
375 | | - | |
376 | | - | |
| 375 | + | |
377 | 376 | | |
378 | 377 | | |
379 | 378 | | |
| |||
405 | 404 | | |
406 | 405 | | |
407 | 406 | | |
| 407 | + | |
408 | 408 | | |
409 | 409 | | |
410 | 410 | | |
| |||
0 commit comments