From 5f5d7b32f0097e352b4ec41ca1fe44b2bb95ab45 Mon Sep 17 00:00:00 2001 From: Lars Maxfield Date: Thu, 30 Jan 2025 09:49:12 +0100 Subject: [PATCH 1/2] Fix #20: Add dict with nested defaults if missing Now for all applicable keywords, not just properties --- .../jsonschema_fill_default.py | 4 +-- tests/unit/test_validate_and_fill.py | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/jsonschema_fill_default/jsonschema_fill_default.py b/jsonschema_fill_default/jsonschema_fill_default.py index cf51cfd..c6534ba 100644 --- a/jsonschema_fill_default/jsonschema_fill_default.py +++ b/jsonschema_fill_default/jsonschema_fill_default.py @@ -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) @@ -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) diff --git a/tests/unit/test_validate_and_fill.py b/tests/unit/test_validate_and_fill.py index 3b72a48..03ac64f 100644 --- a/tests/unit/test_validate_and_fill.py +++ b/tests/unit/test_validate_and_fill.py @@ -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"} + } + } + ] + }, } From 8f048315b3b73f85d08eb5e35154334a438abc23 Mon Sep 17 00:00:00 2001 From: Lars Maxfield Date: Thu, 30 Jan 2025 09:50:47 +0100 Subject: [PATCH 2/2] Fix #20: Bump version --- poetry.lock | 26 +++++++++++++++++++------- pyproject.toml | 2 +- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/poetry.lock b/poetry.lock index be957d8..4f25894 100644 --- a/poetry.lock +++ b/poetry.lock @@ -2,13 +2,13 @@ [[package]] name = "attrs" -version = "24.3.0" +version = "25.1.0" description = "Classes Without Boilerplate" optional = false python-versions = ">=3.8" files = [ - {file = "attrs-24.3.0-py3-none-any.whl", hash = "sha256:ac96cd038792094f438ad1f6ff80837353805ac950cd2aa0e0625ef19850c308"}, - {file = "attrs-24.3.0.tar.gz", hash = "sha256:8f5c07333d543103541ba7be0e2ce16eeee8130cb0b3f9238ab904ce1e85baff"}, + {file = "attrs-25.1.0-py3-none-any.whl", hash = "sha256:c75a69e28a550a7e93789579c22aa26b0f5b83b75dc4e08fe092980051e1090a"}, + {file = "attrs-25.1.0.tar.gz", hash = "sha256:1c97078a80c814273a76b2a298a932eb681c87415c11dee0a6921de7f1b02c3e"}, ] [package.extras] @@ -151,18 +151,19 @@ dev = ["argcomplete", "attrs (>=19.2)", "hypothesis (>=3.56)", "mock", "pygments [[package]] name = "referencing" -version = "0.35.1" +version = "0.36.2" description = "JSON Referencing + Python" optional = false -python-versions = ">=3.8" +python-versions = ">=3.9" files = [ - {file = "referencing-0.35.1-py3-none-any.whl", hash = "sha256:eda6d3234d62814d1c64e305c1331c9a3a6132da475ab6382eaa997b21ee75de"}, - {file = "referencing-0.35.1.tar.gz", hash = "sha256:25b42124a6c8b632a425174f24087783efb348a6f1e0008e63cd4466fedf703c"}, + {file = "referencing-0.36.2-py3-none-any.whl", hash = "sha256:e8699adbbf8b5c7de96d8ffa0eb5c158b3beafce084968e2ea8bb08c6794dcd0"}, + {file = "referencing-0.36.2.tar.gz", hash = "sha256:df2e89862cd09deabbdba16944cc3f10feb6b3e6f18e902f7cc25609a34775aa"}, ] [package.dependencies] attrs = ">=22.2.0" rpds-py = ">=0.7.0" +typing-extensions = {version = ">=4.4.0", markers = "python_version < \"3.13\""} [[package]] name = "rpds-py" @@ -317,6 +318,17 @@ files = [ {file = "tomli-2.2.1.tar.gz", hash = "sha256:cd45e1dc79c835ce60f7404ec8119f2eb06d38b1deba146f07ced3bbc44505ff"}, ] +[[package]] +name = "typing-extensions" +version = "4.12.2" +description = "Backported and Experimental Type Hints for Python 3.8+" +optional = false +python-versions = ">=3.8" +files = [ + {file = "typing_extensions-4.12.2-py3-none-any.whl", hash = "sha256:04e5ca0351e0f3f85c6853954072df659d0d13fac324d0072316b67d7794700d"}, + {file = "typing_extensions-4.12.2.tar.gz", hash = "sha256:1a7ead55c7e559dd4dee8856e3a88b41225abfe1ce8df57b7c13915fe121ffb8"}, +] + [metadata] lock-version = "2.0" python-versions = "^3.9" diff --git a/pyproject.toml b/pyproject.toml index c9ef04d..56e6b78 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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"