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
7 changes: 7 additions & 0 deletions src/datamodel_code_generator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,13 @@ def validate_class_name_affix_scope(cls, v: str | ClassNameAffixScope | None) ->
def merge_args(self, args: Namespace) -> None:
"""Merge command-line arguments into config."""
set_args = {f: getattr(args, f) for f in self.get_fields() if getattr(args, f) is not None}
explicit_input_sources = {
field_name for field_name in ("input", "url", "input_model") if field_name in set_args
}

if explicit_input_sources:
for field_name in {"input", "url", "input_model"} - explicit_input_sources:
setattr(self, field_name, None)

if set_args.get("output_model_type") == DataModelType.MsgspecStruct.value:
set_args["use_annotated"] = True
Expand Down
93 changes: 93 additions & 0 deletions tests/main/test_main_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,99 @@ def test_use_specialized_enum_pyproject_override_with_cli(output_file: Path, tmp
)


def test_cli_input_overrides_pyproject_url(output_file: Path, tmp_path: Path, mocker: MockerFixture) -> None:
"""Test --input takes precedence over pyproject.toml url setting."""
pyproject_toml = tmp_path / "pyproject.toml"
pyproject_toml.write_text(
'[tool.datamodel-codegen]\nurl = "https://example.com/schema.json"\ninput-file-type = "jsonschema"\n',
encoding="utf-8",
)

cli_input = tmp_path / "cli.json"
cli_input.write_text(
'{"type": "object", "properties": {"from_input": {"type": "string"}}}',
encoding="utf-8",
)

httpx_get_mock = mocker.patch("httpx.get")

with chdir(tmp_path):
run_main_and_assert(
input_path=cli_input.relative_to(tmp_path),
output_path=output_file,
extra_args=["--disable-timestamp"],
expected_output=snapshot(
"""\
# generated by datamodel-codegen:
# filename: cli.json

from __future__ import annotations

from pydantic import BaseModel


class Model(BaseModel):
from_input: str | None = None
"""
),
)

httpx_get_mock.assert_not_called()


def test_cli_url_overrides_pyproject_input(output_file: Path, tmp_path: Path, mocker: MockerFixture) -> None:
"""Test --url takes precedence over pyproject.toml input setting."""
pyproject_toml = tmp_path / "pyproject.toml"
pyproject_toml.write_text(
'[tool.datamodel-codegen]\ninput = "config.json"\ninput-file-type = "jsonschema"\n',
encoding="utf-8",
)

config_input = tmp_path / "config.json"
config_input.write_text(
'{"type": "object", "properties": {"from_config": {"type": "string"}}}',
encoding="utf-8",
)

mock_response = mocker.Mock()
mock_response.status_code = 200
mock_response.headers = {}
mock_response.text = '{"type": "object", "properties": {"from_url": {"type": "integer"}}}'
httpx_get_mock = mocker.patch("httpx.get", return_value=mock_response)

with chdir(tmp_path):
run_main_with_args([
"--url",
"http://127.0.0.1:8123/schema.json",
"--output",
str(output_file),
"--disable-timestamp",
])

assert output_file.read_text(encoding="utf-8") == snapshot(
"""\
# generated by datamodel-codegen:
# filename: http://127.0.0.1:8123/schema.json

from __future__ import annotations

from pydantic import BaseModel


class Model(BaseModel):
from_url: int | None = None
"""
)
httpx_get_mock.assert_called_once_with(
"http://127.0.0.1:8123/schema.json",
headers=None,
verify=True,
follow_redirects=True,
params=None,
timeout=30.0,
)


@pytest.mark.cli_doc(
options=["--module-split-mode"],
option_description="""Split generated models into separate files, one per model class.
Expand Down
Loading