Skip to content

Commit 3e06d37

Browse files
Merge pull request #332 from coveord/fix/json-schema-title-in-sub-schemas
fix: JSON Schema missing title in subschemas
2 parents c3aea90 + 481f2ee commit 3e06d37

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

schema/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,9 @@ def _to_schema(s: Any, ignore_extra_keys: bool) -> Schema:
671671

672672
return_schema["$ref"] = "#/definitions/" + cast(str, schema.name)
673673
else:
674+
if schema.name and not title:
675+
return_schema["title"] = schema.name
676+
674677
if flavor == TYPE:
675678
# Handle type
676679
return_schema["type"] = _get_type_name(s)

test_schema.py

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,6 +1443,46 @@ def test_json_schema_title_and_description():
14431443
}
14441444

14451445

1446+
def test_json_schema_title_in_or():
1447+
s = Schema(
1448+
{
1449+
"test": Or(
1450+
Schema(
1451+
"option1", name="Option 1", description="This is the first option"
1452+
),
1453+
Schema(
1454+
"option2",
1455+
name="Option 2",
1456+
description="This is the second option",
1457+
),
1458+
)
1459+
}
1460+
)
1461+
assert s.json_schema("my-id") == {
1462+
"$schema": "http://json-schema.org/draft-07/schema#",
1463+
"$id": "my-id",
1464+
"properties": {
1465+
"test": {
1466+
"anyOf": [
1467+
{
1468+
"const": "option1",
1469+
"title": "Option 1",
1470+
"description": "This is the first option",
1471+
},
1472+
{
1473+
"const": "option2",
1474+
"title": "Option 2",
1475+
"description": "This is the second option",
1476+
},
1477+
]
1478+
}
1479+
},
1480+
"required": ["test"],
1481+
"additionalProperties": False,
1482+
"type": "object",
1483+
}
1484+
1485+
14461486
def test_json_schema_description_nested():
14471487
s = Schema(
14481488
{
@@ -1588,8 +1628,16 @@ def test_json_schema_ref_in_list():
15881628

15891629
assert generated_json_schema == {
15901630
"definitions": {
1591-
"Inner test": {"items": {"type": "string"}, "type": "array"},
1592-
"Inner test2": {"items": {"type": "string"}, "type": "array"},
1631+
"Inner test": {
1632+
"items": {"type": "string"},
1633+
"type": "array",
1634+
"title": "Inner test",
1635+
},
1636+
"Inner test2": {
1637+
"items": {"type": "string"},
1638+
"type": "array",
1639+
"title": "Inner test2",
1640+
},
15931641
},
15941642
"anyOf": [
15951643
{"$ref": "#/definitions/Inner test"},
@@ -1775,6 +1823,7 @@ def test_json_schema_definitions():
17751823
"definitions": {
17761824
"sub_schema": {
17771825
"type": "object",
1826+
"title": "sub_schema",
17781827
"properties": {"sub_key1": {"type": "integer"}},
17791828
"required": ["sub_key1"],
17801829
"additionalProperties": False,
@@ -1824,6 +1873,7 @@ def test_json_schema_definitions_and_literals():
18241873
"sub_key1": {"description": "Sub key 1", "type": "integer"}
18251874
},
18261875
"required": ["sub_key1"],
1876+
"title": "sub_schema",
18271877
"additionalProperties": False,
18281878
}
18291879
},
@@ -1855,6 +1905,7 @@ def test_json_schema_definitions_nested():
18551905
"definitions": {
18561906
"sub_schema": {
18571907
"type": "object",
1908+
"title": "sub_schema",
18581909
"properties": {
18591910
"sub_key1": {"type": "integer"},
18601911
"sub_key2": {"$ref": "#/definitions/sub_sub_schema"},
@@ -1864,6 +1915,7 @@ def test_json_schema_definitions_nested():
18641915
},
18651916
"sub_sub_schema": {
18661917
"type": "object",
1918+
"title": "sub_sub_schema",
18671919
"properties": {"sub_sub_key1": {"type": "integer"}},
18681920
"required": ["sub_sub_key1"],
18691921
"additionalProperties": False,
@@ -1894,6 +1946,7 @@ def test_json_schema_definitions_recursive():
18941946
"definitions": {
18951947
"person": {
18961948
"type": "object",
1949+
"title": "person",
18971950
"properties": {
18981951
"name": {"type": "string"},
18991952
"children": {

0 commit comments

Comments
 (0)