Skip to content

Commit 53b0ed1

Browse files
committed
Fix RootModel default value not being applied
1 parent aa088d6 commit 53b0ed1

File tree

12 files changed

+14
-14
lines changed

12 files changed

+14
-14
lines changed

docs/cli-reference/model-customization.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5029,7 +5029,7 @@ where optional fields have defaults but cannot accept `None` values.
50295029

50305030

50315031
class Id(BaseModel):
5032-
__root__: int
5032+
__root__: int = 1
50335033

50345034

50355035
class Description(BaseModel):

docs/cli-reference/template-customization.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2625,7 +2625,7 @@ helps maintain consistency with codebases that prefer double-quote formatting.
26252625

26262626

26272627
class Zoom(BaseModel):
2628-
__root__: confloat(ge=0.0, le=25.0)
2628+
__root__: confloat(ge=0.0, le=25.0) = 0
26292629
```
26302630

26312631
---

src/datamodel_code_generator/model/template/pydantic/BaseModel_root.jinja2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class {{ class_name }}({{ base_class }}):{% if comment is defined %} # {{ comme
2424
{%- else %}
2525
__root__: {{ field.type_hint }}
2626
{%- endif %}
27-
{%- if not field.has_default_factory_in_field and not (field.required or (field.represented_default == 'None' and field.strip_default_none))
27+
{%- if not field.has_default_factory_in_field and not ((field.required and not field.has_default) or (field.represented_default == 'None' and field.strip_default_none))
2828
%} = {{ field.represented_default }}
2929
{%- endif -%}
3030
{%- endif %}

src/datamodel_code_generator/model/template/pydantic_v2/RootModel.jinja2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class {{ class_name }}({{ base_class }}{%- if fields -%}[{{get_type_hint(fields,
4242
{%- else %}
4343
root: {{ field.type_hint }}
4444
{%- endif %}
45-
{%- if not field.has_default_factory_in_field and not (field.required or (field.represented_default == 'None' and field.strip_default_none))
45+
{%- if not field.has_default_factory_in_field and not ((field.required and not field.has_default) or (field.represented_default == 'None' and field.strip_default_none))
4646
%} = {{ field.represented_default }}
4747
{%- endif -%}
4848
{%- endif %}

tests/data/expected/main/jsonschema/all_of_any_of_base_class_ref.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,4 @@ class Pitch(BaseModel):
7575

7676

7777
class Zoom(BaseModel):
78-
__root__: confloat(ge=0.0, le=25.0)
78+
__root__: confloat(ge=0.0, le=25.0) = 0

tests/data/expected/main/jsonschema/has_default_value.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ class TeamType(Enum):
1717

1818

1919
class ID(BaseModel):
20-
__root__: str
20+
__root__: str = 'abc'
2121

2222

2323
class Pet(BaseModel):
2424
name: str | None = None
2525

2626

2727
class Family(BaseModel):
28-
__root__: list[ID]
28+
__root__: list[ID] = ['abc', 'efg']
2929

3030

3131
class FamilyPets(BaseModel):
32-
__root__: list[Pet]
32+
__root__: list[Pet] = ['taro', 'shiro']
3333

3434

3535
class Person(BaseModel):

tests/data/expected/main/openapi/nullable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Email(BaseModel):
5757

5858

5959
class Id(BaseModel):
60-
__root__: int
60+
__root__: int = 1
6161

6262

6363
class Description(BaseModel):

tests/data/expected/main/openapi/nullable_strict_nullable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Email(BaseModel):
5757

5858

5959
class Id(BaseModel):
60-
__root__: int
60+
__root__: int = 1
6161

6262

6363
class Description(BaseModel):

tests/data/expected/main/openapi/nullable_strict_nullable_use_union_operator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Email(BaseModel):
5757

5858

5959
class Id(BaseModel):
60-
__root__: int
60+
__root__: int = 1
6161

6262

6363
class Description(BaseModel):

tests/data/expected/main/openapi/referenced_default.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
class ModelSettingB(RootModel[confloat(ge=0.0, le=10.0)]):
11-
root: confloat(ge=0.0, le=10.0)
11+
root: confloat(ge=0.0, le=10.0) = 5
1212

1313

1414
class Model(BaseModel):

0 commit comments

Comments
 (0)