diff --git a/src/apispec/ext/marshmallow/field_converter.py b/src/apispec/ext/marshmallow/field_converter.py index 3bd16ed7..44ed1ad2 100644 --- a/src/apispec/ext/marshmallow/field_converter.py +++ b/src/apispec/ext/marshmallow/field_converter.py @@ -113,6 +113,7 @@ def init_attribute_functions(self): self.list2properties, self.dict2properties, self.timedelta2properties, + self.date2properties, self.datetime2properties, self.field2nullable, ] @@ -544,9 +545,7 @@ def datetime2properties(self, field, **kwargs: typing.Any) -> dict: :rtype: dict """ ret = {} - if isinstance(field, marshmallow.fields.DateTime) and not isinstance( - field, marshmallow.fields.Date - ): + if isinstance(field, marshmallow.fields.DateTime): if field.format == "iso" or field.format is None: # Will return { "type": "string", "format": "date-time" } # as specified inside DEFAULT_FIELD_MAPPING @@ -596,6 +595,40 @@ def datetime2properties(self, field, **kwargs: typing.Any) -> dict: } return ret + def date2properties( + self, field: marshmallow.fields.Date, **kwargs: typing.Any + ) -> dict: + """Return a dictionary of OpenAPI properties for a Date field. + + :param Field field: A marshmallow Date field. + :rtype: dict + """ + ret = {} + + if isinstance(field, marshmallow.fields.Date): + if field.format == "iso" or field.format is None: + ret = { + "type": "string", + "format": "date", + } + elif field.format: + ret = { + "type": "string", + "format": None, + } + else: + ret = { + "type": "string", + "format": None, + "pattern": ( + field.metadata["pattern"] + if field.metadata.get("pattern") + else None + ), + } + + return ret + def make_type_list(types): """Return a list of types from a type attribute