| 1 |
import datetime as dt |
| 2 |
import importlib.metadata |
| 3 |
import re |
| 4 |
from enum import Enum |
| 5 |
|
| 6 |
import pytest |
| 7 |
from marshmallow import Schema, fields, validate |
| 8 |
from packaging.version import Version |
| 9 |
|
| 10 |
from .schemas import CategorySchema, CustomIntegerField, CustomList, CustomStringField |
| 11 |
from .utils import build_ref, get_schemas |
| 12 |
|
| 13 |
MA_VERSION = Version(importlib.metadata.version("marshmallow")) |
| 14 |
|
| 15 |
|
| 16 |
def test_field2choices_preserving_order(openapi): |
| 17 |
choices = ["a", "b", "c", "aa", "0", "cc"] |
| 18 |
field = fields.String(validate=validate.OneOf(choices)) |
| 19 |
assert openapi.field2choices(field) == {"enum": choices} |
| 20 |
|
| 21 |
|
| 22 |
@pytest.mark.parametrize( |
| 23 |
("FieldClass", "jsontype"), |
| 24 |
[ |
| 25 |
(fields.Integer, "integer"), |
| 26 |
(fields.Float, "number"), |
| 27 |
(fields.String, "string"), |
| 28 |
(fields.Str, "string"), |
| 29 |
(fields.Boolean, "boolean"), |
| 30 |
(fields.Bool, "boolean"), |
| 31 |
(fields.UUID, "string"), |
| 32 |
(fields.DateTime, "string"), |
| 33 |
(fields.Date, "string"), |
| 34 |
(fields.Time, "string"), |
| 35 |
(fields.TimeDelta, "number" if MA_VERSION.major >= 4 else "integer"), |
| 36 |
(fields.Email, "string"), |
| 37 |
(fields.URL, "string"), |
| 38 |
(fields.IP, "string"), |
| 39 |
(fields.IPv4, "string"), |
| 40 |
(fields.IPv6, "string"), |
| 41 |
# Custom fields inherit types from their parents |
| 42 |
(CustomStringField, "string"), |
| 43 |
(CustomIntegerField, "integer"), |
| 44 |
], |
| 45 |
) |
| 46 |
def test_field2property_type(FieldClass, jsontype, spec_fixture): |
| 47 |
field = FieldClass() |
| 48 |
res = spec_fixture.openapi.field2property(field) |
| 49 |
assert res["type"] == jsontype |
| 50 |
|
| 51 |
|
| 52 |
@pytest.mark.skipif( |
| 53 |
MA_VERSION.major >= 4, reason="Marshmallow 4 removed serialization_type attribute" |
| 54 |
) |
| 55 |
@pytest.mark.parametrize( |
| 56 |
("serialization_type", "expected_type"), |
| 57 |
[ |
| 58 |
(int, "integer"), |
| 59 |
(float, "number"), |
| 60 |
], |
| 61 |
) |
| 62 |
def test_field2property_type_for_timedelta_marshmallow_3( |
| 63 |
spec_fixture, |
| 64 |
serialization_type, |
| 65 |
expected_type, |
| 66 |
): |
| 67 |
field = fields.TimeDelta(serialization_type=serialization_type) |
| 68 |
res = spec_fixture.openapi.field2property(field) |
| 69 |
assert res["type"] == expected_type |
| 70 |
|
| 71 |
|
| 72 |
def test_field2property_no_type(spec_fixture): |
| 73 |
field = fields.Raw() |
| 74 |
res = spec_fixture.openapi.field2property(field) |
| 75 |
assert "type" not in res |
| 76 |
|
| 77 |
|
| 78 |
@pytest.mark.parametrize("ListClass", [fields.List, CustomList]) |
| 79 |
def test_formatted_field_translates_to_array(ListClass, spec_fixture): |
| 80 |
field = ListClass(fields.String) |
| 81 |
res = spec_fixture.openapi.field2property(field) |
| 82 |
assert res["type"] == "array" |
| 83 |
assert res["items"] == spec_fixture.openapi.field2property(fields.String()) |
| 84 |
|
| 85 |
|
| 86 |
@pytest.mark.parametrize( |
| 87 |
("FieldClass", "expected_format"), |
| 88 |
[ |
| 89 |
(fields.UUID, "uuid"), |
| 90 |
(fields.DateTime, "date-time"), |
| 91 |
(fields.Date, "date"), |
| 92 |
(fields.Email, "email"), |
| 93 |
(fields.URL, "url"), |
| 94 |
(fields.IP, "ip"), |
| 95 |
(fields.IPv4, "ipv4"), |
| 96 |
(fields.IPv6, "ipv6"), |
| 97 |
], |
| 98 |
) |
| 99 |
def test_field2property_formats(FieldClass, expected_format, spec_fixture): |
| 100 |
field = FieldClass() |
| 101 |
res = spec_fixture.openapi.field2property(field) |
| 102 |
assert res["format"] == expected_format |
| 103 |
|
| 104 |
|
| 105 |
def test_field_with_description(spec_fixture): |
| 106 |
field = fields.Str(metadata={"description": "a username"}) |
| 107 |
res = spec_fixture.openapi.field2property(field) |
| 108 |
assert res["description"] == "a username" |
| 109 |
|
| 110 |
|
| 111 |
def test_field_with_load_default(spec_fixture): |
| 112 |
field = fields.Str(dump_default="foo", load_default="bar") |
| 113 |
res = spec_fixture.openapi.field2property(field) |
| 114 |
assert res["default"] == "bar" |
| 115 |
|
| 116 |
|
| 117 |
def test_boolean_field_with_false_load_default(spec_fixture): |
| 118 |
field = fields.Boolean(dump_default=None, load_default=False) |
| 119 |
res = spec_fixture.openapi.field2property(field) |
| 120 |
assert res["default"] is False |
| 121 |
|
| 122 |
|
| 123 |
def test_datetime_field_with_load_default(spec_fixture): |
| 124 |
field = fields.Date(load_default=dt.date(2014, 7, 18)) |
| 125 |
res = spec_fixture.openapi.field2property(field) |
| 126 |
assert res["default"] == dt.date(2014, 7, 18).isoformat() |
| 127 |
|
| 128 |
|
| 129 |
def test_field_with_load_default_callable(spec_fixture): |
| 130 |
field = fields.Str(load_default=lambda: "dummy") |
| 131 |
res = spec_fixture.openapi.field2property(field) |
| 132 |
assert "default" not in res |
| 133 |
|
| 134 |
|
| 135 |
def test_field_with_default(spec_fixture): |
| 136 |
field = fields.Str(metadata={"default": "Manual default"}) |
| 137 |
res = spec_fixture.openapi.field2property(field) |
| 138 |
assert res["default"] == "Manual default" |
| 139 |
|
| 140 |
|
| 141 |
def test_field_with_default_and_load_default(spec_fixture): |
| 142 |
field = fields.Int(load_default=12, metadata={"default": 42}) |
| 143 |
res = spec_fixture.openapi.field2property(field) |
| 144 |
assert res["default"] == 42 |
| 145 |
|
| 146 |
|
| 147 |
def test_field_with_choices(spec_fixture): |
| 148 |
field = fields.Str(validate=validate.OneOf(["freddie", "brian", "john"])) |
| 149 |
res = spec_fixture.openapi.field2property(field) |
| 150 |
assert set(res["enum"]) == {"freddie", "brian", "john"} |
| 151 |
|
| 152 |
|
| 153 |
def test_field_with_nullable_choices(spec_fixture): |
| 154 |
field = fields.Str( |
| 155 |
validate=validate.OneOf(["freddie", "brian", "john"]), allow_none=True |
| 156 |
) |
| 157 |
res = spec_fixture.openapi.field2property(field) |
| 158 |
assert set(res["enum"]) == {"freddie", "brian", "john", None} |
| 159 |
|
| 160 |
|
| 161 |
def test_field_with_nullable_choices_returns_only_one_none(spec_fixture): |
| 162 |
field = fields.Str( |
| 163 |
validate=validate.OneOf(["freddie", "brian", "john", None]), allow_none=True |
| 164 |
) |
| 165 |
res = spec_fixture.openapi.field2property(field) |
| 166 |
assert res["enum"] == ["freddie", "brian", "john", None] |
| 167 |
|
| 168 |
|
| 169 |
def test_field_with_equal(spec_fixture): |
| 170 |
field = fields.Str(validate=validate.Equal("only choice")) |
| 171 |
res = spec_fixture.openapi.field2property(field) |
| 172 |
assert res["enum"] == ["only choice"] |
| 173 |
|
| 174 |
|
| 175 |
def test_only_allows_valid_properties_in_metadata(spec_fixture): |
| 176 |
field = fields.Str( |
| 177 |
load_default="foo", |
| 178 |
metadata={ |
| 179 |
"description": "foo", |
| 180 |
"not_valid": "lol", |
| 181 |
"allOf": ["bar"], |
| 182 |
"enum": ["red", "blue"], |
| 183 |
}, |
| 184 |
) |
| 185 |
res = spec_fixture.openapi.field2property(field) |
| 186 |
assert res["default"] == field.load_default |
| 187 |
assert "description" in res |
| 188 |
assert "enum" in res |
| 189 |
assert "allOf" in res |
| 190 |
assert "not_valid" not in res |
| 191 |
|
| 192 |
|
| 193 |
def test_field_with_choices_multiple(spec_fixture): |
| 194 |
field = fields.Str( |
| 195 |
validate=[ |
| 196 |
validate.OneOf(["freddie", "brian", "john"]), |
| 197 |
validate.OneOf(["brian", "john", "roger"]), |
| 198 |
] |
| 199 |
) |
| 200 |
res = spec_fixture.openapi.field2property(field) |
| 201 |
assert set(res["enum"]) == {"brian", "john"} |
| 202 |
|
| 203 |
|
| 204 |
def test_field_with_additional_metadata(spec_fixture): |
| 205 |
field = fields.Str(metadata={"minLength": 6, "maxLength": 100}) |
| 206 |
res = spec_fixture.openapi.field2property(field) |
| 207 |
assert res["maxLength"] == 100 |
| 208 |
assert res["minLength"] == 6 |
| 209 |
|
| 210 |
|
| 211 |
@pytest.mark.parametrize("spec_fixture", ("2.0", "3.0.0", "3.1.0"), indirect=True) |
| 212 |
def test_field_with_allow_none(spec_fixture): |
| 213 |
field = fields.Str(allow_none=True) |
| 214 |
res = spec_fixture.openapi.field2property(field) |
| 215 |
if spec_fixture.openapi.openapi_version.major < 3: |
| 216 |
assert res["x-nullable"] is True |
| 217 |
elif spec_fixture.openapi.openapi_version.minor < 1: |
| 218 |
assert res["nullable"] is True |
| 219 |
else: |
| 220 |
assert "nullable" not in res |
| 221 |
assert res["type"] == ["string", "null"] |
| 222 |
|
| 223 |
|
| 224 |
@pytest.mark.parametrize("spec_fixture", ("2.0", "3.0.0", "3.1.0"), indirect=True) |
| 225 |
def test_nullable_raw_field(spec_fixture): |
| 226 |
field = fields.Raw(allow_none=True) |
| 227 |
res = spec_fixture.openapi.field2property(field) |
| 228 |
if spec_fixture.openapi.openapi_version.major < 3: |
| 229 |
assert res["x-nullable"] is True |
| 230 |
elif spec_fixture.openapi.openapi_version.minor < 1: |
| 231 |
assert res["nullable"] is True |
| 232 |
else: |
| 233 |
assert "nullable" not in res |
| 234 |
assert "type" not in res |
| 235 |
|
| 236 |
|
| 237 |
@pytest.mark.parametrize("spec_fixture", ("2.0", "3.0.0", "3.1.0"), indirect=True) |
| 238 |
def test_nested_nullable(spec_fixture): |
| 239 |
class Child(Schema): |
| 240 |
name = fields.Str() |
| 241 |
|
| 242 |
field = fields.Nested(Child, allow_none=True) |
| 243 |
res = spec_fixture.openapi.field2property(field) |
| 244 |
version = spec_fixture.openapi.openapi_version |
| 245 |
if version.major < 3: |
| 246 |
assert res == {"$ref": "#/definitions/Child", "x-nullable": True} |
| 247 |
elif version.major == 3 and version.minor < 1: |
| 248 |
assert res == { |
| 249 |
"anyOf": [ |
| 250 |
{"type": "object", "nullable": True}, |
| 251 |
{"$ref": "#/components/schemas/Child"}, |
| 252 |
] |
| 253 |
} |
| 254 |
else: |
| 255 |
assert res == { |
| 256 |
"anyOf": [{"$ref": "#/components/schemas/Child"}, {"type": "null"}] |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
@pytest.mark.parametrize("spec_fixture", ("2.0", "3.0.0", "3.1.0"), indirect=True) |
| 261 |
def test_nested_nullable_with_metadata(spec_fixture): |
| 262 |
# Regression test for https://github.com/marshmallow-code/apispec/issues/955 |
| 263 |
class Child(Schema): |
| 264 |
name = fields.Str() |
| 265 |
|
| 266 |
field = fields.Nested( |
| 267 |
Child, |
| 268 |
allow_none=True, |
| 269 |
metadata={"description": "foo"}, |
| 270 |
) |
| 271 |
res = spec_fixture.openapi.field2property(field) |
| 272 |
version = spec_fixture.openapi.openapi_version |
| 273 |
if version.major < 3: |
| 274 |
assert res == { |
| 275 |
"allOf": [ |
| 276 |
{"$ref": "#/definitions/Child"}, |
| 277 |
], |
| 278 |
"x-nullable": True, |
| 279 |
"description": "foo", |
| 280 |
} |
| 281 |
elif version.major == 3 and version.minor < 1: |
| 282 |
assert res == { |
| 283 |
"anyOf": [ |
| 284 |
{"$ref": "#/components/schemas/Child"}, |
| 285 |
{"type": "object", "nullable": True}, |
| 286 |
], |
| 287 |
"description": "foo", |
| 288 |
} |
| 289 |
else: |
| 290 |
assert res == { |
| 291 |
"anyOf": [{"$ref": "#/components/schemas/Child"}, {"type": "null"}], |
| 292 |
"description": "foo", |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
@pytest.mark.parametrize("spec_fixture", ("2.0", "3.0.0", "3.1.0"), indirect=True) |
| 297 |
def test_nullable_pluck(spec_fixture): |
| 298 |
class Example(Schema): |
| 299 |
name = fields.Str() |
| 300 |
|
| 301 |
field = fields.Pluck(Example, "name", allow_none=True) |
| 302 |
res = spec_fixture.openapi.field2property(field) |
| 303 |
version = spec_fixture.openapi.openapi_version |
| 304 |
if version.major < 3: |
| 305 |
assert res == {"type": "string", "x-nullable": True} |
| 306 |
elif version.major == 3 and version.minor < 1: |
| 307 |
assert res == {"type": "string", "nullable": True} |
| 308 |
else: |
| 309 |
assert res == {"type": ["string", "null"]} |
| 310 |
|
| 311 |
|
| 312 |
def test_field_with_dump_only(spec_fixture): |
| 313 |
field = fields.Str(dump_only=True) |
| 314 |
res = spec_fixture.openapi.field2property(field) |
| 315 |
assert res["readOnly"] is True |
| 316 |
|
| 317 |
|
| 318 |
@pytest.mark.parametrize("spec_fixture", ("2.0", "3.0.0", "3.1.0"), indirect=True) |
| 319 |
def test_field_with_load_only(spec_fixture): |
| 320 |
field = fields.Str(load_only=True) |
| 321 |
res = spec_fixture.openapi.field2property(field) |
| 322 |
if spec_fixture.openapi.openapi_version.major < 3: |
| 323 |
assert "writeOnly" not in res |
| 324 |
else: |
| 325 |
assert res["writeOnly"] is True |
| 326 |
|
| 327 |
|
| 328 |
def test_field_with_range_no_type(spec_fixture): |
| 329 |
field = fields.Raw(validate=validate.Range(min=1, max=10)) |
| 330 |
res = spec_fixture.openapi.field2property(field) |
| 331 |
assert res["x-minimum"] == 1 |
| 332 |
assert res["x-maximum"] == 10 |
| 333 |
assert "type" not in res |
| 334 |
|
| 335 |
|
| 336 |
@pytest.mark.parametrize("field", (fields.Float, fields.Integer)) |
| 337 |
def test_field_with_range_string_type(spec_fixture, field): |
| 338 |
field = field(validate=validate.Range(min=1, max=10)) |
| 339 |
res = spec_fixture.openapi.field2property(field) |
| 340 |
assert res["minimum"] == 1 |
| 341 |
assert res["maximum"] == 10 |
| 342 |
assert isinstance(res["type"], str) |
| 343 |
|
| 344 |
|
| 345 |
@pytest.mark.parametrize("spec_fixture", ("3.1.0",), indirect=True) |
| 346 |
def test_field_with_range_type_list_with_number(spec_fixture): |
| 347 |
class NullableInteger(fields.Field): |
| 348 |
"""Nullable integer""" |
| 349 |
|
| 350 |
spec_fixture.openapi.map_to_openapi_type(NullableInteger, ["integer", "null"], None) |
| 351 |
|
| 352 |
field = NullableInteger(validate=validate.Range(min=1, max=10)) |
| 353 |
res = spec_fixture.openapi.field2property(field) |
| 354 |
assert res["minimum"] == 1 |
| 355 |
assert res["maximum"] == 10 |
| 356 |
assert res["type"] == ["integer", "null"] |
| 357 |
|
| 358 |
|
| 359 |
@pytest.mark.parametrize("spec_fixture", ("3.1.0",), indirect=True) |
| 360 |
def test_field_with_range_type_list_without_number(spec_fixture): |
| 361 |
class NullableInteger(fields.Field): |
| 362 |
"""Nullable integer""" |
| 363 |
|
| 364 |
spec_fixture.openapi.map_to_openapi_type(NullableInteger, ["string", "null"], None) |
| 365 |
|
| 366 |
field = NullableInteger(validate=validate.Range(min=1, max=10)) |
| 367 |
res = spec_fixture.openapi.field2property(field) |
| 368 |
assert res["x-minimum"] == 1 |
| 369 |
assert res["x-maximum"] == 10 |
| 370 |
assert res["type"] == ["string", "null"] |
| 371 |
|
| 372 |
|
| 373 |
def test_field_with_range_datetime_type(spec_fixture): |
| 374 |
field = fields.DateTime( |
| 375 |
validate=validate.Range( |
| 376 |
min=dt.datetime(1900, 1, 1), |
| 377 |
max=dt.datetime(2000, 1, 1), |
| 378 |
) |
| 379 |
) |
| 380 |
res = spec_fixture.openapi.field2property(field) |
| 381 |
assert res["x-minimum"] == "1900-01-01T00:00:00" |
| 382 |
assert res["x-maximum"] == "2000-01-01T00:00:00" |
| 383 |
assert isinstance(res["type"], str) |
| 384 |
|
| 385 |
|
| 386 |
def test_field_with_str_regex(spec_fixture): |
| 387 |
regex_str = "^[a-zA-Z0-9]$" |
| 388 |
field = fields.Str(validate=validate.Regexp(regex_str)) |
| 389 |
ret = spec_fixture.openapi.field2property(field) |
| 390 |
assert ret["pattern"] == regex_str |
| 391 |
|
| 392 |
|
| 393 |
def test_field_with_pattern_obj_regex(spec_fixture): |
| 394 |
regex_str = "^[a-zA-Z0-9]$" |
| 395 |
field = fields.Str(validate=validate.Regexp(re.compile(regex_str))) |
| 396 |
ret = spec_fixture.openapi.field2property(field) |
| 397 |
assert ret["pattern"] == regex_str |
| 398 |
|
| 399 |
|
| 400 |
def test_field_with_no_pattern(spec_fixture): |
| 401 |
field = fields.Str() |
| 402 |
ret = spec_fixture.openapi.field2property(field) |
| 403 |
assert "pattern" not in ret |
| 404 |
|
| 405 |
|
| 406 |
def test_field_with_multiple_patterns(recwarn, spec_fixture): |
| 407 |
regex_validators = [validate.Regexp("winner"), validate.Regexp("loser")] |
| 408 |
field = fields.Str(validate=regex_validators) |
| 409 |
with pytest.warns(UserWarning, match="More than one regex validator"): |
| 410 |
ret = spec_fixture.openapi.field2property(field) |
| 411 |
assert ret["pattern"] == "winner" |
| 412 |
|
| 413 |
|
| 414 |
def test_enum_symbol_field(spec_fixture): |
| 415 |
class MyEnum(Enum): |
| 416 |
one = 1 |
| 417 |
two = 2 |
| 418 |
|
| 419 |
field = fields.Enum(MyEnum) |
| 420 |
ret = spec_fixture.openapi.field2property(field) |
| 421 |
assert ret["type"] == "string" |
| 422 |
assert ret["enum"] == ["one", "two"] |
| 423 |
|
| 424 |
|
| 425 |
@pytest.mark.parametrize("by_value", [fields.Integer, True]) |
| 426 |
def test_enum_value_field(spec_fixture, by_value): |
| 427 |
class MyEnum(Enum): |
| 428 |
one = 1 |
| 429 |
two = 2 |
| 430 |
|
| 431 |
field = fields.Enum(MyEnum, by_value=by_value) |
| 432 |
ret = spec_fixture.openapi.field2property(field) |
| 433 |
if by_value is True: |
| 434 |
assert "type" not in ret |
| 435 |
else: |
| 436 |
assert ret["type"] == "integer" |
| 437 |
assert ret["enum"] == [1, 2] |
| 438 |
|
| 439 |
|
| 440 |
def test_nullable_enum(spec_fixture): |
| 441 |
class MyEnum(Enum): |
| 442 |
one = 1 |
| 443 |
two = 2 |
| 444 |
|
| 445 |
field = fields.Enum(MyEnum, allow_none=True, by_value=True) |
| 446 |
ret = spec_fixture.openapi.field2property(field) |
| 447 |
assert ret["enum"] == [1, 2, None] |
| 448 |
|
| 449 |
|
| 450 |
def test_nullable_enum_returns_only_one_none(spec_fixture): |
| 451 |
class MyEnum(Enum): |
| 452 |
one = 1 |
| 453 |
two = 2 |
| 454 |
three = None |
| 455 |
|
| 456 |
field = fields.Enum(MyEnum, allow_none=True, by_value=True) |
| 457 |
ret = spec_fixture.openapi.field2property(field) |
| 458 |
assert ret["enum"] == [1, 2, None] |
| 459 |
|
| 460 |
|
| 461 |
@pytest.mark.parametrize("spec_fixture", ("2.0", "3.0.0", "3.1.0"), indirect=True) |
| 462 |
def test_field2property_nested_spec_metadatas(spec_fixture): |
| 463 |
class Child(Schema): |
| 464 |
name = fields.Str() |
| 465 |
|
| 466 |
category = fields.Nested( |
| 467 |
Child, |
| 468 |
metadata={ |
| 469 |
"description": "A category", |
| 470 |
"invalid_property": "not in the result", |
| 471 |
"x_extension": "A great extension", |
| 472 |
}, |
| 473 |
) |
| 474 |
result = spec_fixture.openapi.field2property(category) |
| 475 |
version = spec_fixture.openapi.openapi_version |
| 476 |
if version.major < 3 or version.minor < 1: |
| 477 |
assert result == { |
| 478 |
"allOf": [build_ref(spec_fixture.spec, "schema", "Child")], |
| 479 |
"description": "A category", |
| 480 |
"x-extension": "A great extension", |
| 481 |
} |
| 482 |
else: |
| 483 |
assert result == { |
| 484 |
**build_ref(spec_fixture.spec, "schema", "Child"), |
| 485 |
"description": "A category", |
| 486 |
"x-extension": "A great extension", |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
def test_field2property_nested_spec(spec_fixture): |
| 491 |
spec_fixture.spec.components.schema("Category", schema=CategorySchema) |
| 492 |
category = fields.Nested(CategorySchema) |
| 493 |
assert spec_fixture.openapi.field2property(category) == build_ref( |
| 494 |
spec_fixture.spec, "schema", "Category" |
| 495 |
) |
| 496 |
|
| 497 |
|
| 498 |
def test_field2property_nested_many_spec(spec_fixture): |
| 499 |
spec_fixture.spec.components.schema("Category", schema=CategorySchema) |
| 500 |
category = fields.Nested(CategorySchema, many=True) |
| 501 |
ret = spec_fixture.openapi.field2property(category) |
| 502 |
assert ret["type"] == "array" |
| 503 |
assert ret["items"] == build_ref(spec_fixture.spec, "schema", "Category") |
| 504 |
|
| 505 |
|
| 506 |
def test_field2property_nested_ref(spec_fixture): |
| 507 |
category = fields.Nested(CategorySchema) |
| 508 |
ref = spec_fixture.openapi.field2property(category) |
| 509 |
assert ref == build_ref(spec_fixture.spec, "schema", "Category") |
| 510 |
|
| 511 |
|
| 512 |
def test_field2property_nested_many(spec_fixture): |
| 513 |
categories = fields.Nested(CategorySchema, many=True) |
| 514 |
res = spec_fixture.openapi.field2property(categories) |
| 515 |
assert res["type"] == "array" |
| 516 |
assert res["items"] == build_ref(spec_fixture.spec, "schema", "Category") |
| 517 |
|
| 518 |
|
| 519 |
def test_nested_field_with_property(spec_fixture): |
| 520 |
category_1 = fields.Nested(CategorySchema) |
| 521 |
category_2 = fields.Nested(CategorySchema, dump_only=True) |
| 522 |
category_3 = fields.Nested(CategorySchema, many=True) |
| 523 |
category_4 = fields.Nested(CategorySchema, many=True, dump_only=True) |
| 524 |
spec_fixture.spec.components.schema("Category", schema=CategorySchema) |
| 525 |
|
| 526 |
assert spec_fixture.openapi.field2property(category_1) == build_ref( |
| 527 |
spec_fixture.spec, "schema", "Category" |
| 528 |
) |
| 529 |
assert spec_fixture.openapi.field2property(category_2) == { |
| 530 |
"allOf": [build_ref(spec_fixture.spec, "schema", "Category")], |
| 531 |
"readOnly": True, |
| 532 |
} |
| 533 |
assert spec_fixture.openapi.field2property(category_3) == { |
| 534 |
"items": build_ref(spec_fixture.spec, "schema", "Category"), |
| 535 |
"type": "array", |
| 536 |
} |
| 537 |
assert spec_fixture.openapi.field2property(category_4) == { |
| 538 |
"items": build_ref(spec_fixture.spec, "schema", "Category"), |
| 539 |
"readOnly": True, |
| 540 |
"type": "array", |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
def test_datetime2property_iso(spec_fixture): |
| 545 |
field = fields.DateTime(format="iso") |
| 546 |
res = spec_fixture.openapi.field2property(field) |
| 547 |
assert res == { |
| 548 |
"type": "string", |
| 549 |
"format": "date-time", |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
def test_datetime2property_iso8601(spec_fixture): |
| 554 |
field = fields.DateTime(format="iso8601") |
| 555 |
res = spec_fixture.openapi.field2property(field) |
| 556 |
assert res == { |
| 557 |
"type": "string", |
| 558 |
"format": "date-time", |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
def test_datetime2property_rfc(spec_fixture): |
| 563 |
field = fields.DateTime(format="rfc") |
| 564 |
res = spec_fixture.openapi.field2property(field) |
| 565 |
assert res == { |
| 566 |
"type": "string", |
| 567 |
"format": None, |
| 568 |
"example": "Wed, 02 Oct 2002 13:00:00 GMT", |
| 569 |
"pattern": r"((Mon|Tue|Wed|Thu|Fri|Sat|Sun), ){0,1}\d{2} " |
| 570 |
+ r"(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{4} \d{2}:\d{2}:\d{2} " |
| 571 |
+ r"(UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|(Z|A|M|N)|(\+|-)\d{4})", |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
def test_datetime2property_rfc822(spec_fixture): |
| 576 |
field = fields.DateTime(format="rfc822") |
| 577 |
res = spec_fixture.openapi.field2property(field) |
| 578 |
assert res == { |
| 579 |
"type": "string", |
| 580 |
"format": None, |
| 581 |
"example": "Wed, 02 Oct 2002 13:00:00 GMT", |
| 582 |
"pattern": r"((Mon|Tue|Wed|Thu|Fri|Sat|Sun), ){0,1}\d{2} " |
| 583 |
+ r"(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{4} \d{2}:\d{2}:\d{2} " |
| 584 |
+ r"(UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|(Z|A|M|N)|(\+|-)\d{4})", |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
def test_datetime2property_timestamp(spec_fixture): |
| 589 |
field = fields.DateTime(format="timestamp") |
| 590 |
res = spec_fixture.openapi.field2property(field) |
| 591 |
assert res == { |
| 592 |
"type": "number", |
| 593 |
"format": "float", |
| 594 |
"min": "0", |
| 595 |
"example": "1676451245.596", |
| 596 |
} |
| 597 |
|
| 598 |
|
| 599 |
def test_datetime2property_timestamp_ms(spec_fixture): |
| 600 |
field = fields.DateTime(format="timestamp_ms") |
| 601 |
res = spec_fixture.openapi.field2property(field) |
| 602 |
assert res == { |
| 603 |
"type": "number", |
| 604 |
"format": "float", |
| 605 |
"min": "0", |
| 606 |
"example": "1676451277514.654", |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
def test_datetime2property_custom_format(spec_fixture): |
| 611 |
field = fields.DateTime( |
| 612 |
format="%d-%m%Y %H:%M:%S", |
| 613 |
metadata={ |
| 614 |
"pattern": r"^((?:(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2}(?:\.\d+)?))(Z|[\+-]\d{2}:\d{2})?)$" |
| 615 |
}, |
| 616 |
) |
| 617 |
res = spec_fixture.openapi.field2property(field) |
| 618 |
assert res == { |
| 619 |
"type": "string", |
| 620 |
"format": None, |
| 621 |
"pattern": r"^((?:(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2}(?:\.\d+)?))(Z|[\+-]\d{2}:\d{2})?)$", |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
def test_datetime2property_custom_format_missing_regex(spec_fixture): |
| 626 |
field = fields.DateTime(format="%d-%m%Y %H:%M:%S") |
| 627 |
res = spec_fixture.openapi.field2property(field) |
| 628 |
assert res == { |
| 629 |
"type": "string", |
| 630 |
"format": None, |
| 631 |
"pattern": None, |
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
class TestField2PropertyPluck: |
| 636 |
@pytest.fixture(autouse=True) |
| 637 |
def _setup(self, spec_fixture): |
| 638 |
self.field2property = spec_fixture.openapi.field2property |
| 639 |
|
| 640 |
self.spec = spec_fixture.spec |
| 641 |
self.spec.components.schema("Category", schema=CategorySchema) |
| 642 |
self.unplucked = get_schemas(self.spec)["Category"]["properties"]["breed"] |
| 643 |
|
| 644 |
def test_spec(self, spec_fixture): |
| 645 |
breed = fields.Pluck(CategorySchema, "breed") |
| 646 |
assert self.field2property(breed) == self.unplucked |
| 647 |
|
| 648 |
def test_with_property(self): |
| 649 |
breed = fields.Pluck(CategorySchema, "breed", dump_only=True) |
| 650 |
assert self.field2property(breed) == {**self.unplucked, "readOnly": True} |
| 651 |
|
| 652 |
def test_metadata(self): |
| 653 |
breed = fields.Pluck( |
| 654 |
CategorySchema, |
| 655 |
"breed", |
| 656 |
metadata={ |
| 657 |
"description": "Category breed", |
| 658 |
"invalid_property": "not in the result", |
| 659 |
"x_extension": "A great extension", |
| 660 |
}, |
| 661 |
) |
| 662 |
assert self.field2property(breed) == { |
| 663 |
**self.unplucked, |
| 664 |
"description": "Category breed", |
| 665 |
"x-extension": "A great extension", |
| 666 |
} |
| 667 |
|
| 668 |
def test_many(self): |
| 669 |
breed = fields.Pluck(CategorySchema, "breed", many=True) |
| 670 |
assert self.field2property(breed) == {"type": "array", "items": self.unplucked} |
| 671 |
|
| 672 |
def test_many_with_property(self): |
| 673 |
breed = fields.Pluck(CategorySchema, "breed", many=True, dump_only=True) |
| 674 |
assert self.field2property(breed) == { |
| 675 |
"items": self.unplucked, |
| 676 |
"type": "array", |
| 677 |
"readOnly": True, |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
def test_custom_properties_for_custom_fields(spec_fixture): |
| 682 |
def custom_string2properties(self, field, **kwargs): |
| 683 |
ret = {} |
| 684 |
if isinstance(field, CustomStringField): |
| 685 |
if self.openapi_version.major == 2: |
| 686 |
ret["x-customString"] = True |
| 687 |
else: |
| 688 |
ret["x-customString"] = False |
| 689 |
return ret |
| 690 |
|
| 691 |
spec_fixture.marshmallow_plugin.converter.add_attribute_function( |
| 692 |
custom_string2properties |
| 693 |
) |
| 694 |
properties = spec_fixture.marshmallow_plugin.converter.field2property( |
| 695 |
CustomStringField() |
| 696 |
) |
| 697 |
assert properties["x-customString"] == ( |
| 698 |
spec_fixture.openapi.openapi_version.major == 2 |
| 699 |
) |
| 700 |
|
| 701 |
|
| 702 |
def test_field2property_with_non_string_metadata_keys(spec_fixture): |
| 703 |
class _DesertSentinel: |
| 704 |
pass |
| 705 |
|
| 706 |
field = fields.Boolean(metadata={"description": "A description"}) |
| 707 |
field.metadata[_DesertSentinel()] = "to be ignored" |
| 708 |
result = spec_fixture.openapi.field2property(field) |
| 709 |
assert result == {"description": "A description", "type": "boolean"} |
| 710 |
|
| 711 |
|
| 712 |
def test_field2property_examples(spec_fixture): |
| 713 |
field = fields.Raw(metadata={"examples": ["foo", "bar"]}) |
| 714 |
res = spec_fixture.openapi.field2property(field) |
| 715 |
assert res == { |
| 716 |
"examples": ["foo", "bar"], |
| 717 |
} |