Skip to content

Commit 6dd1dbe

Browse files
authored
Fix const in anyOf/oneOf to generate Literal type (#2864)
1 parent 602c45a commit 6dd1dbe

13 files changed

Lines changed: 61 additions & 16 deletions

File tree

src/datamodel_code_generator/parser/jsonschema.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,9 +1262,10 @@ def get_data_type(self, obj: JsonSchemaObject) -> DataType:
12621262
if python_type_override:
12631263
return python_type_override
12641264

1265+
if "const" in obj.extras:
1266+
return self.data_type(literals=[obj.extras["const"]])
1267+
12651268
if obj.type is None:
1266-
if "const" in obj.extras:
1267-
return self.data_type_manager.get_data_type_from_value(obj.extras["const"])
12681269
return self.data_type_manager.get_data_type(
12691270
Types.any,
12701271
)

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
from __future__ import annotations
66

7+
from typing import Literal
8+
79
from pydantic import BaseModel, Field, confloat
810

911

1012
class MapState1(BaseModel):
11-
map_view_mode: str = Field("MODE_2D", alias="mapViewMode", const=True)
13+
map_view_mode: Literal["MODE_2D"] = Field(
14+
"MODE_2D", alias="mapViewMode", const=True
15+
)
1216

1317

1418
class MapState2(BaseModel):
@@ -18,8 +22,10 @@ class MapState2(BaseModel):
1822
bearing: Bearing | None = None
1923
pitch: Pitch
2024
drag_rotate: DragRotate | None = Field(None, alias="dragRotate")
21-
map_split_mode: str = Field("SWIPE_COMPARE", alias="mapSplitMode", const=True)
22-
is_split: bool = Field(True, alias="isSplit", const=True)
25+
map_split_mode: Literal["SWIPE_COMPARE"] = Field(
26+
"SWIPE_COMPARE", alias="mapSplitMode", const=True
27+
)
28+
is_split: Literal[True] = Field(True, alias="isSplit", const=True)
2329

2430

2531
class MapState3(BaseModel):
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# generated by datamodel-codegen:
2+
# filename: anyof_const_with_constraints.json
3+
4+
from __future__ import annotations
5+
6+
from typing import Literal
7+
8+
from pydantic import BaseModel, conint
9+
10+
11+
class Model(BaseModel):
12+
SomeValue: Literal[500000] | conint(ge=0, le=65534) | None = None

tests/data/expected/main/jsonschema/discriminator_with_external_references_folder/inner_folder/artificial_folder/type_1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010

1111

1212
class Type1(BaseModel):
13-
type_: Literal['a'] = Field(..., const=True, title='Type ')
13+
type_: Literal['a'] = Field('a', const=True, title='Type ')

tests/data/expected/main/jsonschema/discriminator_with_external_references_folder/inner_folder/schema.py

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

1616

1717
class Type3(BaseModel):
18-
type_: Literal['c'] = Field(..., const=True, title='Type ')
18+
type_: Literal['c'] = Field('c', const=True, title='Type ')
1919

2020

2121
class Response(BaseModel):

tests/data/expected/main/jsonschema/discriminator_with_external_references_folder/inner_folder/type_2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212

1313

1414
class Type2(BaseModel):
15-
type_: Literal['b'] = Field(..., const=True, title='Type ')
15+
type_: Literal['b'] = Field('b', const=True, title='Type ')
1616
ref_type: type_1.Type1 | None = Field(None, description='A referenced type.')

tests/data/expected/main/jsonschema/discriminator_with_external_references_folder/subfolder/type_5.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010

1111

1212
class Type5(BaseModel):
13-
type_: Literal['e'] = Field(..., const=True, title='Type ')
13+
type_: Literal['e'] = Field('e', const=True, title='Type ')

tests/data/expected/main/jsonschema/discriminator_with_external_references_folder/type_4.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010

1111

1212
class Type4(BaseModel):
13-
type_: Literal['d'] = Field(..., const=True, title='Type ')
13+
type_: Literal['d'] = Field('d', const=True, title='Type ')

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from __future__ import annotations
66

7+
from typing import Literal
8+
79
from pydantic import BaseModel, Field
810

911

@@ -12,4 +14,4 @@ class SomeType(BaseModel):
1214

1315

1416
class MixedUnion(BaseModel):
15-
__root__: str | SomeType = Field(..., title='MixedUnion')
17+
__root__: Literal['value1'] | SomeType = Field(..., title='MixedUnion')

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from __future__ import annotations
66

7-
from typing import Any
7+
from typing import Any, Literal
88

99
from pydantic import BaseModel, Field
1010

@@ -14,4 +14,4 @@ class ConstWithProps1(BaseModel):
1414

1515

1616
class ConstWithProps(BaseModel):
17-
__root__: ConstWithProps1 | str = Field(..., title='ConstWithProps')
17+
__root__: ConstWithProps1 | Literal['value2'] = Field(..., title='ConstWithProps')

0 commit comments

Comments
 (0)