Skip to content

Commit 833613d

Browse files
authored
Merge pull request #36 from larsmaxfield/developer/lars
Check default type on iteration, update
2 parents 9345f55 + 0dab268 commit 833613d

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

jsonschema_fill_default/jsonschema_fill_default.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ def fill_default(
6464
_fill_dependentschemas(instance, schema, config)
6565
if keyword == "default":
6666
if not instance:
67-
instance.update(schema["default"])
67+
if isinstance(schema["default"], dict):
68+
instance.update(schema["default"])
69+
else:
70+
instance = schema["default"]
6871
if isinstance(instance, list): # Handle "(prefix)Items" for lists (arrays)
6972
_fill_prefixitems_and_items(instance, schema, config)
7073
return None
@@ -183,8 +186,9 @@ def _fill_properties(instance: dict, schema: dict, config: FillConfig):
183186
instance[_property] = subschema["default"]
184187
# Fill missing keys if instance already exists as object
185188
elif _property in instance \
189+
and isinstance(instance[_property], dict) \
186190
and "default" in subschema \
187-
and isinstance(instance[_property], dict):
191+
and isinstance(subschema["default"], dict):
188192
for default_key in subschema["default"]:
189193
if default_key not in instance[_property]:
190194
instance[_property][default_key] = \

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
[tool.poetry]
22
name = "jsonschema-fill-default"
3-
version = "0.2.0.20250602"
3+
version = "0.2.1.20250710"
44
description = "Fill a JSON instance with the missing defaults from its JSON Schema Draft 2020-12-valid schema"
55
license = "MIT"
66
authors = ["Lars Maxfield"]
77
maintainers = ["Lars Maxfield"]
88
readme = "README.md"
99
repository = "https://github.com/larsmaxfield/jsonschema-fill-default"
10-
keywords = ["jsonschema", "default"]
10+
keywords = ["jsonschema", "default", "nested"]
1111

1212
[tool.poetry.dependencies]
1313
python = "^3.9"

tests/unit/test_validate_and_fill.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,39 @@
963963
}
964964
}
965965
]
966+
},
967+
"defaultNotDict": {
968+
"schema": {
969+
"$schema": "https://json-schema.org/draft/2020-12/schema",
970+
"title": "JSON Schema where default is not a dict, but instance is",
971+
"type": "object",
972+
"properties": {
973+
"usuallyBool": {
974+
"default": True,
975+
"oneOf": [
976+
{"type": "boolean"},
977+
{"type": "object"}
978+
]
979+
}
980+
}
981+
},
982+
"instances": [
983+
{ # Empty
984+
"original": {
985+
},
986+
"expected": {
987+
"usuallyBool": True
988+
}
989+
},
990+
{ # Partial
991+
"original": {
992+
"usuallyBool": {"is not": "a bool"}
993+
},
994+
"expected": {
995+
"usuallyBool": {"is not": "a bool"}
996+
}
997+
},
998+
]
966999
}
9671000
}
9681001

0 commit comments

Comments
 (0)