mirror of
https://github.com/koxudaxi/datamodel-code-generator.git
synced 2024-03-18 14:54:37 +03:00
Use const as default (#1767)
* Reproduce bug with unit test * Use const as default
This commit is contained in:
committed by
GitHub
parent
38bf2b9e62
commit
ef15441112
2
.gitignore
vendored
2
.gitignore
vendored
@@ -175,3 +175,5 @@ fabric.properties
|
||||
.idea/caches/build_file_checksums.ser
|
||||
|
||||
.idea
|
||||
|
||||
.vscode
|
||||
|
||||
@@ -87,10 +87,9 @@ class DataModelField(DataModelFieldV1):
|
||||
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:
|
||||
@@ -103,7 +102,7 @@ class DataModelField(DataModelFieldV1):
|
||||
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)}',
|
||||
|
||||
12
tests/data/expected/main/use_default_with_const/output.py
Normal file
12
tests/data/expected/main/use_default_with_const/output.py
Normal file
@@ -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
tests/data/jsonschema/use_default_with_const.json
Normal file
10
tests/data/jsonschema/use_default_with_const.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"type": "object",
|
||||
"title": "Use default with const",
|
||||
"properties": {
|
||||
"foo": {
|
||||
"const": "foo"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user