From a05b37ba094869020c911aba8622cbc3c7303a8e Mon Sep 17 00:00:00 2001 From: Theron Luhn Date: Sun, 20 Oct 2024 14:24:22 -0700 Subject: [PATCH] Add `additionalProperties: {}` for Dict with the default `values` (#950) * Add `additionalProperties: {}` for Dict with the default `values` * Add self to AUTHORS.rst * Update changelog --------- Co-authored-by: Steven Loria --- AUTHORS.rst | 1 + CHANGELOG.rst | 5 +++++ src/apispec/ext/marshmallow/field_converter.py | 2 ++ tests/test_ext_marshmallow.py | 2 +- 4 files changed, 9 insertions(+), 1 deletion(-) diff --git a/AUTHORS.rst b/AUTHORS.rst index 16fb4991..2d5ed602 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -83,3 +83,4 @@ Contributors (chronological) - Tobias Kolditz `@kolditz-senec `_ - Christian Proud `@cjproud `_ - ``_ +- Theron Luhn `@luhn `_ diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 291c3d42..178eca3a 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,6 +4,11 @@ Changelog 6.7.0 (unreleased) ****************** +Bug fixes: + +- Fix handling of ``fields.Dict()`` with ``values`` unset (:issue:`949`). + Thanks :user:`luhn` for the catch and patch. + Other changes: - Officially support Python 3.13 (:pr:`948`). diff --git a/src/apispec/ext/marshmallow/field_converter.py b/src/apispec/ext/marshmallow/field_converter.py index 4e17aa2e..a284f9b1 100644 --- a/src/apispec/ext/marshmallow/field_converter.py +++ b/src/apispec/ext/marshmallow/field_converter.py @@ -504,6 +504,8 @@ def dict2properties(self, field, **kwargs: typing.Any) -> dict: value_field = field.value_field if value_field: ret["additionalProperties"] = self.field2property(value_field) + else: + ret["additionalProperties"] = {} return ret def timedelta2properties(self, field, **kwargs: typing.Any) -> dict: diff --git a/tests/test_ext_marshmallow.py b/tests/test_ext_marshmallow.py index fa1cfe78..0b863f41 100644 --- a/tests/test_ext_marshmallow.py +++ b/tests/test_ext_marshmallow.py @@ -1294,7 +1294,7 @@ class SchemaWithDict(Schema): spec.components.schema("SchemaWithDict", schema=SchemaWithDict) result = get_schemas(spec)["SchemaWithDict"]["properties"]["dict_field"] - assert result == {"type": "object"} + assert result == {"type": "object", "additionalProperties": {}} def test_dict_with_nested(self, spec): class SchemaWithDict(Schema):