Skip to content

Commit

Permalink
Merge pull request #825 from marshmallow-code/serialize_range
Browse files Browse the repository at this point in the history
field2range: serialize min/max values
  • Loading branch information
lafrech committed Mar 3, 2023
2 parents 90b38c2 + d420316 commit 8112917
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/apispec/ext/marshmallow/field_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,12 @@ def field2range(self, field: marshmallow.fields.Field, ret) -> dict:
if set(make_type_list(ret.get("type"))) & {"number", "integer"}
else ("x-minimum", "x-maximum")
)
return make_min_max_attributes(validators, min_attr, max_attr)

# Serialize min/max values with the field to which the validator is applied
return {
k: field._serialize(v, None, None)
for k, v in make_min_max_attributes(validators, min_attr, max_attr).items()
}

def field2length(
self, field: marshmallow.fields.Field, **kwargs: typing.Any
Expand Down
13 changes: 13 additions & 0 deletions tests/test_ext_marshmallow_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,19 @@ class NullableInteger(fields.Field):
assert res["type"] == ["string", "null"]


def test_field_with_range_datetime_type(spec_fixture):
field = fields.DateTime(
validate=validate.Range(
min=dt.datetime(1900, 1, 1),
max=dt.datetime(2000, 1, 1),
)
)
res = spec_fixture.openapi.field2property(field)
assert res["x-minimum"] == "1900-01-01T00:00:00"
assert res["x-maximum"] == "2000-01-01T00:00:00"
assert isinstance(res["type"], str)


def test_field_with_str_regex(spec_fixture):
regex_str = "^[a-zA-Z0-9]$"
field = fields.Str(validate=validate.Regexp(regex_str))
Expand Down
2 changes: 1 addition & 1 deletion tests/test_ext_marshmallow_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ class ValidationSchema(Schema):
("custom_field_length", {"minLength": 1, "maxLength": 10}),
("multiple_lengths", {"minLength": 3, "maxLength": 7}),
("equal_length", {"minLength": 5, "maxLength": 5}),
("date_range", {"x-minimum": datetime(1900, 1, 1)}),
("date_range", {"x-minimum": "1900-01-01T00:00:00"}),
],
)
def test_properties(self, field, properties, spec):
Expand Down

0 comments on commit 8112917

Please sign in to comment.