From fe07500955712129f61ed89a52e9022e299ec2de Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Braun Date: Fri, 22 May 2026 12:08:28 +0200 Subject: [PATCH] Fix: stop emitting null format/pattern for DateTime fields (#938) DateTime fields declared with the "rfc"/"rfc822" format or a custom strftime format string were rendered with `"format": null` and, for custom formats without a metadata pattern, `"pattern": null`. The OpenAPI 3 schema requires those keywords to be strings when present, so the resulting spec failed openapi-spec-validator. Pop the inherited `date-time` format from the accumulator dict for the non-iso branches and only emit `pattern` when the user supplied one via metadata. A regression test exercises the three affected combinations and runs the full spec through `validate_spec` so any future null leakage is caught immediately. Fixes #938. --- AUTHORS.rst | 1 + CHANGELOG.rst | 8 ++++ .../ext/marshmallow/field_converter.py | 43 ++++++++++------- tests/test_ext_marshmallow_field.py | 47 +++++++++++++++++-- 4 files changed, 78 insertions(+), 21 deletions(-) diff --git a/AUTHORS.rst b/AUTHORS.rst index 9d854662..bc54f7d4 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -91,3 +91,4 @@ Contributors (chronological) - Felix Claessen `@Flix6x `_ - Karthik Ramadugu `@karthiksai109 `_ - Amir Kahriman `@kingdomOfIT `_ +- Jean-Baptiste Braun `@jbbqqf `_ diff --git a/CHANGELOG.rst b/CHANGELOG.rst index c47aded9..a2e8c86b 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,6 +4,14 @@ Changelog unreleased ********** +Bug fixes: + +- ``MarshmallowPlugin``: stop emitting ``"format": null`` and ``"pattern": null`` + for ``DateTime`` fields declared with the ``"rfc"``/``"rfc822"`` format or a + custom ``strftime`` format string. The OpenAPI 3 schema rejects null values + for these keywords, so the resulting document failed + ``openapi-spec-validator`` (:issue:`938`). + Other changes: - Drop support for marshmallow 3, which is EOL. diff --git a/src/apispec/ext/marshmallow/field_converter.py b/src/apispec/ext/marshmallow/field_converter.py index 527fa9da..1806beda 100644 --- a/src/apispec/ext/marshmallow/field_converter.py +++ b/src/apispec/ext/marshmallow/field_converter.py @@ -566,52 +566,63 @@ def enum2properties(self, field, **kwargs: typing.Any) -> dict: ret["enum"].append(None) return ret - def datetime2properties(self, field, **kwargs: typing.Any) -> dict: + def datetime2properties(self, field, ret=None, **kwargs: typing.Any) -> dict: """Return a dictionary of properties from :class:`DateTime