From f252b49935c41babe48f40d3416a937856b43fe7 Mon Sep 17 00:00:00 2001 From: SAY-5 Date: Sun, 24 May 2026 16:09:30 -0700 Subject: [PATCH] fix: preserve value schema for dict-of-dicts type keys in json_schema --- schema/__init__.py | 15 +++++++++++---- test_schema.py | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/schema/__init__.py b/schema/__init__.py index 3a918d0..fcd5ab6 100644 --- a/schema/__init__.py +++ b/schema/__init__.py @@ -809,13 +809,20 @@ def _get_key_name(key: Any) -> Any: return key - additional_properties = ( - additional_properties - or _key_allows_additional_properties(key) - ) + key_allows_additional = _key_allows_additional_properties(key) sub_schema = _to_schema(s[key], ignore_extra_keys=i) key_name = _get_key_name(key) + if key_allows_additional and isinstance(s[key], dict): + # Preserve the value schema of dict-of-dicts type keys + additional_properties = _json_schema( + sub_schema, is_main_schema=False + ) + else: + additional_properties = ( + additional_properties or key_allows_additional + ) + if isinstance(key_name, str): if not isinstance(key, Optional): required_keys.append(key_name) diff --git a/test_schema.py b/test_schema.py index 8936e53..3387294 100644 --- a/test_schema.py +++ b/test_schema.py @@ -1349,6 +1349,23 @@ def test_json_schema_additional_properties_multiple(): } +def test_json_schema_dict_of_dicts_additional_properties(): + s = Schema({str: {"property-1": str}}) + assert s.json_schema("my-id") == { + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "my-id", + "required": [], + "properties": {}, + "additionalProperties": { + "type": "object", + "properties": {"property-1": {"type": "string"}}, + "required": ["property-1"], + "additionalProperties": False, + }, + "type": "object", + } + + @mark.parametrize( "input_schema, expected_keyword, expected_value", [