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
5 changes: 4 additions & 1 deletion src/datamodel_code_generator/parser/jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,10 @@ def _get_data_type(type_: str, format__: str) -> DataType:
data_types=[_get_data_type(t, obj.format or "default") for t in obj.type if t != "null"],
is_optional="null" in obj.type,
)
return _get_data_type(obj.type, obj.format or "default")
data_type = _get_data_type(obj.type, obj.format or "default")
if self.strict_nullable and obj.nullable:
return self.data_type(data_types=[data_type], is_optional=True)
return data_type

def get_ref_data_type(self, ref: str) -> DataType:
"""Get a data type from a reference string."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# generated by datamodel-codegen:
# filename: nullable_array_items.yaml
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from pydantic import BaseModel


class Model(BaseModel):
list1: list[str | None]
list2: list[int | None]
list3: list[str]
nested_list: list[list[float | None]]
27 changes: 27 additions & 0 deletions tests/data/jsonschema/nullable_array_items.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
properties:
list1:
type: array
items:
type: string
nullable: true
list2:
type: array
items:
type: integer
nullable: true
list3:
type: array
items:
type: string
nested_list:
type: array
items:
type: array
items:
type: number
nullable: true
required:
- list1
- list2
- list3
- nested_list
17 changes: 17 additions & 0 deletions tests/main/jsonschema/test_main_jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -4518,3 +4518,20 @@ def test_main_allof_root_model_constraints_none(output_file: Path) -> None:
expected_file="allof_root_model_constraints.py",
extra_args=["--allof-merge-mode", "none"],
)


@pytest.mark.benchmark
def test_main_nullable_array_items_strict_nullable(output_file: Path) -> None:
"""Test nullable array items with strict-nullable flag (issue #1815)."""
run_main_and_assert(
input_path=JSON_SCHEMA_DATA_PATH / "nullable_array_items.yaml",
output_path=output_file,
input_file_type="jsonschema",
assert_func=assert_file_content,
expected_file="nullable_array_items_strict_nullable.py",
extra_args=[
"--strict-nullable",
"--output-model-type",
"pydantic_v2.BaseModel",
],
)
Loading