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
14 changes: 13 additions & 1 deletion src/datamodel_code_generator/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,26 @@ def _external_ref_mapping(value: str) -> str:
def _json_value_or_file(value: str) -> dict[str, object]:
"""Parse a JSON value or load it from a JSON file path."""
path = Path(value).expanduser()
if path.is_file():

# In Python<=3.13, long json values would cause `path.is_file()` to raise an OSError exception
# because the string is too long to be interpreted as a filename.
# This changed in Python>=3.14 since now `path.is_file()` catches OSError and returns `false` instead.
# Therefore we are going to catch OSError exception raised in Python<=3.13 to replicate Python>=3.14 behavior.
is_file: bool
try:
is_file = path.is_file()
except OSError:
is_file = False

if is_file:
try:
json_input = path.read_text(encoding=DEFAULT_ENCODING)
except (OSError, UnicodeDecodeError) as e:
msg = f"Unable to read JSON file {value!r}: {e}"
raise ArgumentTypeError(msg) from e
else:
json_input = value

try:
result = json.loads(json_input)
except json.JSONDecodeError as e:
Expand Down
32 changes: 32 additions & 0 deletions tests/main/jsonschema/test_main_jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import itertools
import json
import os
import tempfile
Expand Down Expand Up @@ -3143,6 +3144,21 @@ def test_main_jsonschema_base_class_map_empty_list(output_file: Path) -> None:
)


def test_main_jsonschema_base_class_map_long_json(output_file: Path) -> None:
"""Test base_class_map with very long json string."""
run_main_and_assert(
input_path=JSON_SCHEMA_DATA_PATH / "base_class_map_empty_list.json",
output_path=output_file,
input_file_type="jsonschema",
assert_func=assert_file_content,
expected_file="base_class_map_empty_list.py",
extra_args=[
"--base-class-map",
'{"User": [' + ",".join(itertools.repeat('""', 100)) + "]}",
],
)


def test_long_description(output_file: Path) -> None:
"""Test long description handling."""
run_main_and_assert(
Expand Down Expand Up @@ -4659,6 +4675,22 @@ def test_main_enum_field_as_literal_map_from_file(output_file: Path, tmp_path: P
)


def test_main_enum_field_as_literal_map_long_json(output_file: Path) -> None:
"""Test enum_field_as_literal_map with very long json string."""
long_inline_mapping = '{"status": "literal",' + ",".join(f'"unused_field_{i}": "enum"' for i in range(100)) + "}"
run_main_and_assert(
input_path=JSON_SCHEMA_DATA_PATH / "enum_field_as_literal_map.json",
output_path=output_file,
input_file_type=None,
assert_func=assert_file_content,
expected_file="enum_field_as_literal_map.py",
extra_args=[
"--enum-field-as-literal-map",
long_inline_mapping,
],
)
Comment thread
coderabbitai[bot] marked this conversation as resolved.


def test_main_enum_field_as_literal_map_override_global(output_file: Path) -> None:
"""Test --enum-field-as-literal-map overrides global --enum-field-as-literal."""
run_main_and_assert(
Expand Down
Loading