Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/datamodel_code_generator/parser/jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1262,9 +1262,10 @@ def get_data_type(self, obj: JsonSchemaObject) -> DataType:
if python_type_override:
return python_type_override

if "const" in obj.extras:
return self.data_type(literals=[obj.extras["const"]])

if obj.type is None:
if "const" in obj.extras:
return self.data_type_manager.get_data_type_from_value(obj.extras["const"])
return self.data_type_manager.get_data_type(
Types.any,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

from __future__ import annotations

from typing import Literal

from pydantic import BaseModel, Field, confloat


class MapState1(BaseModel):
map_view_mode: str = Field("MODE_2D", alias="mapViewMode", const=True)
map_view_mode: Literal["MODE_2D"] = Field(
"MODE_2D", alias="mapViewMode", const=True
)


class MapState2(BaseModel):
Expand All @@ -18,8 +22,10 @@ class MapState2(BaseModel):
bearing: Bearing | None = None
pitch: Pitch
drag_rotate: DragRotate | None = Field(None, alias="dragRotate")
map_split_mode: str = Field("SWIPE_COMPARE", alias="mapSplitMode", const=True)
is_split: bool = Field(True, alias="isSplit", const=True)
map_split_mode: Literal["SWIPE_COMPARE"] = Field(
"SWIPE_COMPARE", alias="mapSplitMode", const=True
)
is_split: Literal[True] = Field(True, alias="isSplit", const=True)


class MapState3(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# generated by datamodel-codegen:
# filename: anyof_const_with_constraints.json

from __future__ import annotations

from typing import Literal

from pydantic import BaseModel, conint


class Model(BaseModel):
SomeValue: Literal[500000] | conint(ge=0, le=65534) | None = None
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@


class Type1(BaseModel):
type_: Literal['a'] = Field(..., const=True, title='Type ')
type_: Literal['a'] = Field('a', const=True, title='Type ')
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


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


class Response(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@


class Type2(BaseModel):
type_: Literal['b'] = Field(..., const=True, title='Type ')
type_: Literal['b'] = Field('b', const=True, title='Type ')
ref_type: type_1.Type1 | None = Field(None, description='A referenced type.')
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@


class Type5(BaseModel):
type_: Literal['e'] = Field(..., const=True, title='Type ')
type_: Literal['e'] = Field('e', const=True, title='Type ')
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@


class Type4(BaseModel):
type_: Literal['d'] = Field(..., const=True, title='Type ')
type_: Literal['d'] = Field('d', const=True, title='Type ')
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from __future__ import annotations

from typing import Literal

from pydantic import BaseModel, Field


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


class MixedUnion(BaseModel):
__root__: str | SomeType = Field(..., title='MixedUnion')
__root__: Literal['value1'] | SomeType = Field(..., title='MixedUnion')
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from __future__ import annotations

from typing import Any
from typing import Any, Literal

from pydantic import BaseModel, Field

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


class ConstWithProps(BaseModel):
__root__: ConstWithProps1 | str = Field(..., title='ConstWithProps')
__root__: ConstWithProps1 | Literal['value2'] = Field(..., title='ConstWithProps')
6 changes: 4 additions & 2 deletions tests/data/expected/main/openapi/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

from __future__ import annotations

from typing import Literal

from pydantic import BaseModel, Field


class Namespace(BaseModel):
apiVersion: str = Field('v1', const=True)
kind: str = Field('Namespace', const=True)
apiVersion: Literal['v1'] = Field('v1', const=True)
kind: Literal['Namespace'] = Field('Namespace', const=True)
6 changes: 5 additions & 1 deletion tests/data/expected/main/openapi/const_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

from __future__ import annotations

from typing import Literal

from pydantic import BaseModel, Field


class Api(BaseModel):
version: str = Field('v1', const=True, description='The version of this API')
version: Literal['v1'] = Field(
'v1', const=True, description='The version of this API'
)
18 changes: 18 additions & 0 deletions tests/data/jsonschema/anyof_const_with_constraints.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"SomeValue": {
"type": "integer",
"anyOf": [
{
"const": 500000
},
{
"minimum": 0,
"maximum": 65534
}
]
}
}
}
Loading