Skip to content

Commit 9a963ab

Browse files
butvinmclaude
andcommitted
Add tests for allOf --use-default and --force-optional coverage
Cover the previously uncovered code paths: - allOf with $ref + sub-schema required fields + --use-default - allOf with outer required fields + --force-optional Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent dc47a97 commit 9a963ab

5 files changed

Lines changed: 114 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# generated by datamodel-codegen:
2+
# filename: allof_required_use_default.json
3+
# timestamp: 2019-07-26T00:00:00+00:00
4+
5+
from __future__ import annotations
6+
7+
from pydantic import BaseModel
8+
9+
10+
class Base(BaseModel):
11+
id: int
12+
13+
14+
class Container(Base):
15+
name: str = 'unnamed'
16+
tag: str
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# generated by datamodel-codegen:
2+
# filename: force_optional_required.json
3+
# timestamp: 2019-07-26T00:00:00+00:00
4+
5+
from __future__ import annotations
6+
7+
from pydantic import BaseModel
8+
9+
10+
class Base(BaseModel):
11+
id: int | None = None
12+
13+
14+
class Container(Base):
15+
name: str | None = None
16+
value: int | None = None
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema",
3+
"title": "Container",
4+
"definitions": {
5+
"Base": {
6+
"type": "object",
7+
"properties": {
8+
"id": {
9+
"type": "integer"
10+
}
11+
},
12+
"required": ["id"]
13+
}
14+
},
15+
"allOf": [
16+
{"$ref": "#/definitions/Base"},
17+
{
18+
"type": "object",
19+
"properties": {
20+
"name": {
21+
"type": "string",
22+
"default": "unnamed"
23+
},
24+
"tag": {
25+
"type": "string"
26+
}
27+
},
28+
"required": ["name", "tag"]
29+
}
30+
]
31+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema",
3+
"title": "Container",
4+
"definitions": {
5+
"Base": {
6+
"type": "object",
7+
"properties": {
8+
"id": {
9+
"type": "integer"
10+
}
11+
}
12+
}
13+
},
14+
"allOf": [
15+
{"$ref": "#/definitions/Base"},
16+
{
17+
"type": "object",
18+
"properties": {
19+
"name": {
20+
"type": "string"
21+
},
22+
"value": {
23+
"type": "integer"
24+
}
25+
}
26+
}
27+
],
28+
"required": ["name", "value"]
29+
}

tests/main/jsonschema/test_main_jsonschema.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4753,6 +4753,28 @@ def test_all_of_use_default(output_file: Path) -> None:
47534753
)
47544754

47554755

4756+
def test_allof_required_use_default(output_file: Path) -> None:
4757+
"""Test allOf with required fields and --use-default renders defaults without nullable types."""
4758+
run_main_and_assert(
4759+
input_path=JSON_SCHEMA_DATA_PATH / "allof_required_use_default.json",
4760+
output_path=output_file,
4761+
input_file_type="jsonschema",
4762+
assert_func=assert_file_content,
4763+
extra_args=["--use-default"],
4764+
)
4765+
4766+
4767+
def test_force_optional_required(output_file: Path) -> None:
4768+
"""Test --force-optional makes required fields optional."""
4769+
run_main_and_assert(
4770+
input_path=JSON_SCHEMA_DATA_PATH / "force_optional_required.json",
4771+
output_path=output_file,
4772+
input_file_type="jsonschema",
4773+
assert_func=assert_file_content,
4774+
extra_args=["--force-optional"],
4775+
)
4776+
4777+
47564778
def test_main_root_one_of(output_dir: Path) -> None:
47574779
"""Test root-level oneOf schemas."""
47584780
run_main_and_assert(

0 commit comments

Comments
 (0)