Skip to content

Fix #20: KeyError when filling missing object with nested defaults for non-"properties" keywords #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 30, 2025
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
4 changes: 1 addition & 3 deletions jsonschema_fill_default/jsonschema_fill_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def _fill_properties(instance: dict, schema: dict):
None
"""
for _property, subschema in schema["properties"].items():
if "properties" in subschema: # Recursion
if any(key in ["properties", "oneOf", "allOf", "anyOf", "if", "dependentSchemas"] for key in subschema): # Recursion
if _property not in instance:
instance[_property] = dict()
fill_default(instance[_property], subschema)
Expand All @@ -149,8 +149,6 @@ def _fill_properties(instance: dict, schema: dict):
if default_key not in instance[_property]:
instance[_property][default_key] = \
subschema["default"][default_key]
if any(key in ["oneOf", "allOf", "anyOf", "if", "dependentSchemas"] for key in subschema):
fill_default(instance[_property], subschema)
if "prefixItems" in subschema or "items" in subschema:
if _property in instance: # Instance must have array to fill
fill_default(instance[_property], subschema)
Expand Down
26 changes: 19 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "jsonschema-fill-default"
version = "0.1.0.20250109"
version = "0.1.1.20250130"
description = "Fill a JSON instance with the missing defaults from its JSON Schema Draft 2020-12-valid schema"
authors = ["Lars Maxfield"]
readme = "README.md"
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/test_validate_and_fill.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,41 @@
}
]
},
"nestedKeywords": {
"schema": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "JSON Schema of 'dependentSchemas' with nested keywords",
"type": "object",
"properties": {
"subObject": {
"allOf": [
{
"properties": {
"subString": {"default": "nested"}
}
}
]
}
}
},
"instances": [
{ # Empty
"original": {
},
"expected": {
"subObject": {"subString": "nested"}
}
},
{ # Full
"original": {
"subObject": {"subString": "already taken"}
},
"expected": {
"subObject": {"subString": "already taken"}
}
}
]
},
}


Expand Down