Skip to content

Commit

Permalink
Add base_class validation similar to _validate_base_class to bare…
Browse files Browse the repository at this point in the history
… `generate` calls (#1453)

* Add base_class validation similar to _validate_base_class to bare generate calls

* Remove set default base_class

---------

Co-authored-by: Kyle Bebak <kyle@elementaryml.com>
Co-authored-by: Koudai Aono <koxudaxi@gmail.com>
  • Loading branch information
3 people authored Aug 1, 2023
1 parent 1fe472e commit 667dc56
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion datamodel_code_generator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def generate(
output: Optional[Path] = None,
output_model_type: DataModelType = DataModelType.PydanticBaseModel,
target_python_version: PythonVersion = PythonVersion.PY_37,
base_class: str = DEFAULT_BASE_CLASS,
base_class: str = '',
additional_imports: Optional[List[str]] = None,
custom_template_dir: Optional[Path] = None,
extra_template_data: Optional[DefaultDict[str, Dict[str, Any]]] = None,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# generated by datamodel-codegen:
# filename: simple_string.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from dataclasses import dataclass


@dataclass
class Model:
s: str
6 changes: 6 additions & 0 deletions tests/data/jsonschema/simple_string.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"properties": {"s": {"type": ["string"]}},
"required": ["s"]
}
28 changes: 27 additions & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
from freezegun import freeze_time
from packaging import version

from datamodel_code_generator import InputFileType, chdir, generate, inferred_message
from datamodel_code_generator import (
DataModelType,
InputFileType,
chdir,
generate,
inferred_message,
)
from datamodel_code_generator.__main__ import Exit, main

try:
Expand Down Expand Up @@ -2694,6 +2700,26 @@ def test_main_generate():
)


@freeze_time('2019-07-26')
def test_main_generate_non_pydantic_output():
"""
See https://github.com/koxudaxi/datamodel-code-generator/issues/1452.
"""
with TemporaryDirectory() as output_dir:
output_file: Path = Path(output_dir) / 'output.py'
input_ = (JSON_SCHEMA_DATA_PATH / 'simple_string.json').relative_to(Path.cwd())
assert not input_.is_absolute()
generate(
input_=input_,
input_file_type=InputFileType.JsonSchema,
output=output_file,
output_model_type=DataModelType.DataclassesDataclass,
)

file = EXPECTED_MAIN_PATH / 'main_generate_non_pydantic_output' / 'output.py'
assert output_file.read_text() == file.read_text()


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

0 comments on commit 667dc56

Please sign in to comment.