Skip to content

Commit a4262f0

Browse files
authored
Merge pull request #27 from larsmaxfield/developer/lars
Fix #26: Don't delete object if already empty
2 parents 8276c1c + c9a51df commit a4262f0

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

jsonschema_fill_default/jsonschema_fill_default.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ def _fill_empty_property(schema: dict):
114114
return None
115115

116116

117+
def _is_empty_object(x):
118+
"""True if an empty object, False if not empty and/or not an object"""
119+
if not isinstance(x, (list, tuple, dict, set, range)):
120+
return False
121+
return not x
122+
123+
117124
def _fill_properties(instance: dict, schema: dict):
118125
"""Recursively fill a JSON instance with schema "properties" defaults
119126
@@ -135,10 +142,12 @@ def _fill_properties(instance: dict, schema: dict):
135142
if any(key in ["properties", "oneOf", "allOf", "anyOf", "if", "dependentSchemas"] for key in subschema): # Recursion
136143
if _property not in instance:
137144
instance[_property] = dict()
145+
_was_empty = False
146+
else:
147+
_was_empty = _is_empty_object(instance[_property])
138148
fill_default(instance[_property], subschema)
139-
if isinstance(instance[_property], (list, tuple, dict, set)):
140-
if len(instance[_property]) == 0: # No default found inside
141-
del instance[_property]
149+
if not _was_empty and _is_empty_object(instance[_property]):
150+
del instance[_property]
142151
if _property not in instance \
143152
and "default" in subschema:
144153
instance[_property] = subschema["default"]

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.2.20250130"
3+
version = "0.1.3.20250131"
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

+47
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,53 @@
703703
}
704704
}
705705
]
706+
},
707+
"emptyExisting": {
708+
"schema": {
709+
"$schema": "https://json-schema.org/draft/2020-12/schema",
710+
"title": "JSON Schema of 'dependentSchemas' with nested keywords",
711+
"type": "object",
712+
"properties": {
713+
"subObject": {
714+
"allOf": [
715+
{
716+
"if": {"required": ["exists"]},
717+
"then": {
718+
"properties": {
719+
"string": {"default": "nested"}
720+
}
721+
}
722+
}
723+
]
724+
}
725+
}
726+
},
727+
"instances": [
728+
{ # Empty
729+
"original": {
730+
"subObject": {}
731+
},
732+
"expected": {
733+
"subObject": {}
734+
}
735+
},
736+
{ # Partial
737+
"original": {
738+
"subObject": {"exists": True}
739+
},
740+
"expected": {
741+
"subObject": {"exists": True, "string": "nested"}
742+
}
743+
},
744+
{ # Partial
745+
"original": {
746+
"subObject": {"string": "hello"}
747+
},
748+
"expected": {
749+
"subObject": {"string": "hello"}
750+
}
751+
}
752+
]
706753
}
707754
}
708755

0 commit comments

Comments
 (0)