Skip to content

Commit 504ab20

Browse files
authored
Merge pull request #21 from larsmaxfield/developer/lars
Fix #20: KeyError when filling missing object with nested defaults for non-"properties" keywords
2 parents 783e44f + 8f04831 commit 504ab20

File tree

4 files changed

+56
-11
lines changed

4 files changed

+56
-11
lines changed

jsonschema_fill_default/jsonschema_fill_default.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def _fill_properties(instance: dict, schema: dict):
132132
None
133133
"""
134134
for _property, subschema in schema["properties"].items():
135-
if "properties" in subschema: # Recursion
135+
if any(key in ["properties", "oneOf", "allOf", "anyOf", "if", "dependentSchemas"] for key in subschema): # Recursion
136136
if _property not in instance:
137137
instance[_property] = dict()
138138
fill_default(instance[_property], subschema)
@@ -149,8 +149,6 @@ def _fill_properties(instance: dict, schema: dict):
149149
if default_key not in instance[_property]:
150150
instance[_property][default_key] = \
151151
subschema["default"][default_key]
152-
if any(key in ["oneOf", "allOf", "anyOf", "if", "dependentSchemas"] for key in subschema):
153-
fill_default(instance[_property], subschema)
154152
if "prefixItems" in subschema or "items" in subschema:
155153
if _property in instance: # Instance must have array to fill
156154
fill_default(instance[_property], subschema)

poetry.lock

+19-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "jsonschema-fill-default"
3-
version = "0.1.0.20250109"
3+
version = "0.1.1.20250130"
44
description = "Fill a JSON instance with the missing defaults from its JSON Schema Draft 2020-12-valid schema"
55
authors = ["Lars Maxfield"]
66
readme = "README.md"

tests/unit/test_validate_and_fill.py

+35
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,41 @@
669669
}
670670
]
671671
},
672+
"nestedKeywords": {
673+
"schema": {
674+
"$schema": "https://json-schema.org/draft/2020-12/schema",
675+
"title": "JSON Schema of 'dependentSchemas' with nested keywords",
676+
"type": "object",
677+
"properties": {
678+
"subObject": {
679+
"allOf": [
680+
{
681+
"properties": {
682+
"subString": {"default": "nested"}
683+
}
684+
}
685+
]
686+
}
687+
}
688+
},
689+
"instances": [
690+
{ # Empty
691+
"original": {
692+
},
693+
"expected": {
694+
"subObject": {"subString": "nested"}
695+
}
696+
},
697+
{ # Full
698+
"original": {
699+
"subObject": {"subString": "already taken"}
700+
},
701+
"expected": {
702+
"subObject": {"subString": "already taken"}
703+
}
704+
}
705+
]
706+
},
672707
}
673708

674709

0 commit comments

Comments
 (0)