Skip to content

Commit 03004c9

Browse files
author
Daniele Esposti
committed
Fixed generating Marshmallow Date formats
1 parent 47f20ce commit 03004c9

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

src/apispec/ext/marshmallow/field_converter.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ def init_attribute_functions(self):
113113
self.list2properties,
114114
self.dict2properties,
115115
self.timedelta2properties,
116+
self.date2properties,
116117
self.datetime2properties,
117118
self.field2nullable,
118119
]
@@ -544,9 +545,7 @@ def datetime2properties(self, field, **kwargs: typing.Any) -> dict:
544545
:rtype: dict
545546
"""
546547
ret = {}
547-
if isinstance(field, marshmallow.fields.DateTime) and not isinstance(
548-
field, marshmallow.fields.Date
549-
):
548+
if isinstance(field, marshmallow.fields.DateTime):
550549
if field.format == "iso" or field.format is None:
551550
# Will return { "type": "string", "format": "date-time" }
552551
# as specified inside DEFAULT_FIELD_MAPPING
@@ -596,6 +595,40 @@ def datetime2properties(self, field, **kwargs: typing.Any) -> dict:
596595
}
597596
return ret
598597

598+
def date2properties(
599+
self, field: marshmallow.fields.Date, **kwargs: typing.Any
600+
) -> dict:
601+
"""Return a dictionary of OpenAPI properties for a Date field.
602+
603+
:param Field field: A marshmallow Date field.
604+
:rtype: dict
605+
"""
606+
ret = {}
607+
608+
if isinstance(field, marshmallow.fields.Date):
609+
if field.format == "iso" or field.format is None:
610+
ret = {
611+
"type": "string",
612+
"format": "date",
613+
}
614+
elif field.format:
615+
ret = {
616+
"type": "string",
617+
"format": None,
618+
}
619+
else:
620+
ret = {
621+
"type": "string",
622+
"format": None,
623+
"pattern": (
624+
field.metadata["pattern"]
625+
if field.metadata.get("pattern")
626+
else None
627+
),
628+
}
629+
630+
return ret
631+
599632

600633
def make_type_list(types):
601634
"""Return a list of types from a type attribute

0 commit comments

Comments
 (0)