Commit df834dc
Use validate_default=True for structured union defaults (#3050)
* Use validate_default=True instead of model_validate/TypeAdapter for structured defaults
Replace the ValidatedDefault/TypeAdapter mechanism from #3040 and the
original model_validate() codegen with Pydantic's native validate_default=True.
Instead of generating complex default_factory lambdas like:
default_factory=lambda: A.model_validate({'type': 'b'})
default_factory=lambda: TypeAdapter(A | B).validate_python({'type': 'b'})
Now generates:
Field({'type': 'b'}, validate_default=True)
This lets Pydantic handle union branch selection, dict-to-model coercion,
and RootModel wrapping at runtime. Fixes #3034, fixes #3035.
Removed:
- ValidatedDefault class and SUPPORTS_VALIDATED_DEFAULT flag
- TypeAdapter import in generated code
- 70-line branch-matching loop in _get_default_as_pydantic_model
- Helper functions: _supports_validated_default_model,
_uses_existing_model_factory_path, _default_matches_plain_container_branch,
_get_validated_default_type_name
Added:
- _needs_validate_default: simple check (is default dict/list/scalar
and does the type contain a model reference?)
- __set_validate_default_on_fields: sets extras["validate_default"] = True
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Remove dead None guard from _needs_validate_default to fix coverage
The caller already filters None/UNDEFINED defaults before calling this
function, so the None check was unreachable. Removed the unused `default`
parameter entirely.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Remove WrappedDefault in favor of validate_default=True for RootModel defaults
WrappedDefault wrapped RootModel scalar defaults (e.g., CountType(10)) in
default_factory lambdas. Since validate_default=True handles this natively,
the entire mechanism is redundant.
Removed: WrappedDefault class, _types.py module, SUPPORTS_WRAPPED_DEFAULT
flag, __wrap_root_model_default_values method, _get_default_as_pydantic_model
method.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Make GraphQL scalar types extend TypeAliasBase
_DataTypeScalarBase generated the same type alias syntax as TypeAliasBase
but didn't inherit from it, causing _unwrap_type_alias to not see through
scalar references. This led to unnecessary validate_default=True on fields
like `String | None = 'default_user'`.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Fix required fields with validate_default losing their default value
When a required field had validate_default=True set (e.g., empty list
default on list[Container]), the rendering logic prepended ... instead
of the actual default value. Now skips the ... override when
validate_default is set, since the default is needed for Pydantic to
validate at runtime.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Fix inaccurate docstring in test_needs_validate_default test
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>1 parent 76439c3 commit df834dc
File tree
54 files changed
+144
-471
lines changed- src/datamodel_code_generator
- model
- pydantic_v2
- parser
- tests
- data/expected
- main
- graphql
- jsonschema
- openapi
- enum_models
- pydantic_v2_default_object
- parser/openapi/openapi_parser_parse_enum_models
- parser
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
54 files changed
+144
-471
lines changedThis file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
29 | 28 | | |
30 | 29 | | |
31 | 30 | | |
| |||
38 | 37 | | |
39 | 38 | | |
40 | 39 | | |
41 | | - | |
42 | | - | |
43 | 40 | | |
44 | 41 | | |
45 | 42 | | |
| |||
598 | 595 | | |
599 | 596 | | |
600 | 597 | | |
601 | | - | |
602 | | - | |
603 | 598 | | |
604 | 599 | | |
605 | 600 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
23 | 22 | | |
24 | | - | |
| 23 | + | |
25 | 24 | | |
26 | 25 | | |
27 | 26 | | |
28 | 27 | | |
29 | 28 | | |
30 | | - | |
31 | 29 | | |
32 | 30 | | |
33 | 31 | | |
| |||
126 | 124 | | |
127 | 125 | | |
128 | 126 | | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | 127 | | |
205 | 128 | | |
206 | 129 | | |
| |||
259 | 182 | | |
260 | 183 | | |
261 | 184 | | |
262 | | - | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
263 | 188 | | |
264 | | - | |
265 | | - | |
266 | 189 | | |
267 | 190 | | |
268 | 191 | | |
| |||
288 | 211 | | |
289 | 212 | | |
290 | 213 | | |
291 | | - | |
| 214 | + | |
292 | 215 | | |
293 | 216 | | |
294 | 217 | | |
| |||
323 | 246 | | |
324 | 247 | | |
325 | 248 | | |
326 | | - | |
327 | | - | |
328 | | - | |
329 | | - | |
330 | | - | |
331 | | - | |
| 249 | + | |
| 250 | + | |
332 | 251 | | |
333 | 252 | | |
334 | 253 | | |
| |||
Lines changed: 0 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
222 | 222 | | |
223 | 223 | | |
224 | 224 | | |
225 | | - | |
226 | | - | |
227 | 225 | | |
228 | 226 | | |
229 | 227 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
17 | 16 | | |
| 17 | + | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| |||
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
41 | | - | |
| 42 | + | |
42 | 43 | | |
43 | 44 | | |
44 | 45 | | |
| |||
0 commit comments