Skip to content

Commit

Permalink
Use const as default (#1767)
Browse files Browse the repository at this point in the history
* Reproduce bug with unit test

* Use const as default
  • Loading branch information
mmwinther authored Dec 9, 2023
1 parent 38bf2b9 commit ef15441
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,5 @@ fabric.properties
.idea/caches/build_file_checksums.ser

.idea

.vscode
9 changes: 4 additions & 5 deletions datamodel_code_generator/model/pydantic_v2/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,9 @@ def process_const(self) -> None:
self.const = True
self.nullable = False
const = self.extras['const']
if self.data_type.type == 'str' and isinstance(
const, str
): # pragma: no cover # Literal supports only str
self.data_type = self.data_type.__class__(literals=[const])
self.data_type = self.data_type.__class__(literals=[const])
if not self.default:
self.default = const

def _process_data_in_str(self, data: Dict[str, Any]) -> None:
if self.const:
Expand All @@ -103,7 +102,7 @@ def _process_data_in_str(self, data: Dict[str, Any]) -> None:
def _process_annotated_field_arguments(
self, field_arguments: List[str]
) -> List[str]:
if not self.required:
if not self.required or self.const:
if self.use_default_kwarg:
return [
f'default={repr(self.default)}',
Expand Down
12 changes: 12 additions & 0 deletions tests/data/expected/main/use_default_with_const/output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# generated by datamodel-codegen:
# filename: use_default_with_const.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from pydantic import BaseModel
from typing_extensions import Literal


class UseDefaultWithConst(BaseModel):
foo: Literal['foo'] = 'foo'
10 changes: 10 additions & 0 deletions tests/data/jsonschema/use_default_with_const.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"title": "Use default with const",
"properties": {
"foo": {
"const": "foo"
}
}
}
22 changes: 22 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,28 @@ def test_force_optional():
)


@freeze_time('2019-07-26')
def test_use_default_pydantic_v2_with_json_schema_const():
with TemporaryDirectory() as output_dir:
output_file: Path = Path(output_dir) / 'output.py'
return_code: Exit = main(
[
'--input',
str(JSON_SCHEMA_DATA_PATH / 'use_default_with_const.json'),
'--output',
str(output_file),
'--output-model-type',
'pydantic_v2.BaseModel',
'--use-default',
]
)
assert return_code == Exit.OK
assert (
output_file.read_text()
== (EXPECTED_MAIN_PATH / 'use_default_with_const' / 'output.py').read_text()
)


@freeze_time('2019-07-26')
def test_main_with_exclusive():
with TemporaryDirectory() as output_dir:
Expand Down

0 comments on commit ef15441

Please sign in to comment.