| 1 |
import importlib.metadata |
| 2 |
import json |
| 3 |
|
| 4 |
import pytest |
| 5 |
from marshmallow import Schema |
| 6 |
from marshmallow.fields import ( |
| 7 |
DateTime, |
| 8 |
Dict, |
| 9 |
Field, |
| 10 |
Int, |
| 11 |
List, |
| 12 |
Nested, |
| 13 |
String, |
| 14 |
TimeDelta, |
| 15 |
) |
| 16 |
from packaging.version import Version |
| 17 |
|
| 18 |
from apispec import APISpec |
| 19 |
from apispec.exceptions import APISpecError |
| 20 |
from apispec.ext.marshmallow import MarshmallowPlugin, common |
| 21 |
|
| 22 |
from .schemas import ( |
| 23 |
AnalysisSchema, |
| 24 |
AnalysisWithListSchema, |
| 25 |
DefaultValuesSchema, |
| 26 |
PatternedObjectSchema, |
| 27 |
PetSchema, |
| 28 |
RunSchema, |
| 29 |
SampleSchema, |
| 30 |
SelfReferencingSchema, |
| 31 |
) |
| 32 |
from .utils import ( |
| 33 |
build_ref, |
| 34 |
get_headers, |
| 35 |
get_parameters, |
| 36 |
get_paths, |
| 37 |
get_responses, |
| 38 |
get_schemas, |
| 39 |
) |
| 40 |
|
| 41 |
MA_VERSION = Version(importlib.metadata.version("marshmallow")) |
| 42 |
|
| 43 |
|
| 44 |
class TestDefinitionHelper: |
| 45 |
@pytest.mark.parametrize("schema", [PetSchema, PetSchema()]) |
| 46 |
def test_can_use_schema_as_definition(self, spec, schema): |
| 47 |
spec.components.schema("Pet", schema=schema) |
| 48 |
definitions = get_schemas(spec) |
| 49 |
props = definitions["Pet"]["properties"] |
| 50 |
|
| 51 |
assert props["id"]["type"] == "integer" |
| 52 |
assert props["name"]["type"] == "string" |
| 53 |
|
| 54 |
def test_schema_helper_without_schema(self, spec): |
| 55 |
spec.components.schema("Pet", {"properties": {"key": {"type": "integer"}}}) |
| 56 |
definitions = get_schemas(spec) |
| 57 |
assert definitions["Pet"]["properties"] == {"key": {"type": "integer"}} |
| 58 |
|
| 59 |
@pytest.mark.parametrize("schema", [AnalysisSchema, AnalysisSchema()]) |
| 60 |
def test_resolve_schema_dict_auto_reference(self, schema): |
| 61 |
def resolver(schema): |
| 62 |
schema_cls = common.resolve_schema_cls(schema) |
| 63 |
return schema_cls.__name__ |
| 64 |
|
| 65 |
spec = APISpec( |
| 66 |
title="Test auto-reference", |
| 67 |
version="0.1", |
| 68 |
openapi_version="2.0", |
| 69 |
plugins=(MarshmallowPlugin(schema_name_resolver=resolver),), |
| 70 |
) |
| 71 |
assert get_schemas(spec) == {} |
| 72 |
|
| 73 |
spec.components.schema("analysis", schema=schema) |
| 74 |
spec.path( |
| 75 |
"/test", |
| 76 |
operations={ |
| 77 |
"get": { |
| 78 |
"responses": { |
| 79 |
"200": {"schema": build_ref(spec, "schema", "analysis")} |
| 80 |
} |
| 81 |
} |
| 82 |
}, |
| 83 |
) |
| 84 |
definitions = get_schemas(spec) |
| 85 |
assert 3 == len(definitions) |
| 86 |
|
| 87 |
assert "analysis" in definitions |
| 88 |
assert "SampleSchema" in definitions |
| 89 |
assert "RunSchema" in definitions |
| 90 |
|
| 91 |
@pytest.mark.parametrize( |
| 92 |
"schema", [AnalysisWithListSchema, AnalysisWithListSchema()] |
| 93 |
) |
| 94 |
def test_resolve_schema_dict_auto_reference_in_list(self, schema): |
| 95 |
def resolver(schema): |
| 96 |
schema_cls = common.resolve_schema_cls(schema) |
| 97 |
return schema_cls.__name__ |
| 98 |
|
| 99 |
spec = APISpec( |
| 100 |
title="Test auto-reference", |
| 101 |
version="0.1", |
| 102 |
openapi_version="2.0", |
| 103 |
plugins=(MarshmallowPlugin(schema_name_resolver=resolver),), |
| 104 |
) |
| 105 |
assert get_schemas(spec) == {} |
| 106 |
|
| 107 |
spec.components.schema("analysis", schema=schema) |
| 108 |
spec.path( |
| 109 |
"/test", |
| 110 |
operations={ |
| 111 |
"get": { |
| 112 |
"responses": { |
| 113 |
"200": {"schema": build_ref(spec, "schema", "analysis")} |
| 114 |
} |
| 115 |
} |
| 116 |
}, |
| 117 |
) |
| 118 |
definitions = get_schemas(spec) |
| 119 |
assert 3 == len(definitions) |
| 120 |
|
| 121 |
assert "analysis" in definitions |
| 122 |
assert "SampleSchema" in definitions |
| 123 |
assert "RunSchema" in definitions |
| 124 |
|
| 125 |
@pytest.mark.parametrize("schema", [AnalysisSchema, AnalysisSchema()]) |
| 126 |
def test_resolve_schema_dict_auto_reference_return_none(self, schema): |
| 127 |
def resolver(schema): |
| 128 |
return None |
| 129 |
|
| 130 |
spec = APISpec( |
| 131 |
title="Test auto-reference", |
| 132 |
version="0.1", |
| 133 |
openapi_version="2.0", |
| 134 |
plugins=(MarshmallowPlugin(schema_name_resolver=resolver),), |
| 135 |
) |
| 136 |
assert get_schemas(spec) == {} |
| 137 |
|
| 138 |
with pytest.raises( |
| 139 |
APISpecError, match="Name resolver returned None for schema" |
| 140 |
): |
| 141 |
spec.components.schema("analysis", schema=schema) |
| 142 |
|
| 143 |
@pytest.mark.parametrize("schema", [AnalysisSchema, AnalysisSchema()]) |
| 144 |
def test_warning_when_schema_added_twice(self, spec, schema): |
| 145 |
spec.components.schema("Analysis", schema=schema) |
| 146 |
with pytest.warns(UserWarning, match="has already been added to the spec"): |
| 147 |
spec.components.schema("DuplicateAnalysis", schema=schema) |
| 148 |
|
| 149 |
def test_schema_instances_with_different_modifiers_added(self, spec): |
| 150 |
class MultiModifierSchema(Schema): |
| 151 |
pet_unmodified = Nested(PetSchema) |
| 152 |
pet_exclude = Nested(PetSchema, exclude=("name",)) |
| 153 |
|
| 154 |
spec.components.schema("Pet", schema=PetSchema()) |
| 155 |
spec.components.schema("Pet_Exclude", schema=PetSchema(exclude=("name",))) |
| 156 |
|
| 157 |
spec.components.schema("MultiModifierSchema", schema=MultiModifierSchema) |
| 158 |
|
| 159 |
definitions = get_schemas(spec) |
| 160 |
pet_unmodified_ref = definitions["MultiModifierSchema"]["properties"][ |
| 161 |
"pet_unmodified" |
| 162 |
] |
| 163 |
assert pet_unmodified_ref == build_ref(spec, "schema", "Pet") |
| 164 |
|
| 165 |
pet_exclude = definitions["MultiModifierSchema"]["properties"]["pet_exclude"] |
| 166 |
assert pet_exclude == build_ref(spec, "schema", "Pet_Exclude") |
| 167 |
|
| 168 |
def test_schema_instance_with_different_modifers_custom_resolver(self, recwarn): |
| 169 |
class MultiModifierSchema(Schema): |
| 170 |
pet_unmodified = Nested(PetSchema) |
| 171 |
pet_exclude = Nested(PetSchema(partial=True)) |
| 172 |
|
| 173 |
def resolver(schema): |
| 174 |
schema_instance = common.resolve_schema_instance(schema) |
| 175 |
prefix = "Partial-" if schema_instance.partial else "" |
| 176 |
schema_cls = common.resolve_schema_cls(schema) |
| 177 |
name = prefix + schema_cls.__name__ |
| 178 |
if name.endswith("Schema"): |
| 179 |
return name[:-6] or name |
| 180 |
return name |
| 181 |
|
| 182 |
spec = APISpec( |
| 183 |
title="Test Custom Resolver for Partial", |
| 184 |
version="0.1", |
| 185 |
openapi_version="2.0", |
| 186 |
plugins=(MarshmallowPlugin(schema_name_resolver=resolver),), |
| 187 |
) |
| 188 |
|
| 189 |
spec.components.schema("NameClashSchema", schema=MultiModifierSchema) |
| 190 |
assert not recwarn |
| 191 |
|
| 192 |
def test_schema_with_clashing_names(self, spec): |
| 193 |
class Pet(PetSchema): |
| 194 |
another_field = String() |
| 195 |
|
| 196 |
class NameClashSchema(Schema): |
| 197 |
pet_1 = Nested(PetSchema) |
| 198 |
pet_2 = Nested(Pet) |
| 199 |
|
| 200 |
with pytest.warns( |
| 201 |
UserWarning, match="Multiple schemas resolved to the name Pet" |
| 202 |
): |
| 203 |
spec.components.schema("NameClashSchema", schema=NameClashSchema) |
| 204 |
|
| 205 |
definitions = get_schemas(spec) |
| 206 |
|
| 207 |
assert "Pet" in definitions |
| 208 |
assert "Pet1" in definitions |
| 209 |
|
| 210 |
def test_resolve_nested_schema_many_true_resolver_return_none(self): |
| 211 |
def resolver(schema): |
| 212 |
return None |
| 213 |
|
| 214 |
class PetFamilySchema(Schema): |
| 215 |
pets_1 = Nested(PetSchema, many=True) |
| 216 |
pets_2 = List(Nested(PetSchema)) |
| 217 |
|
| 218 |
spec = APISpec( |
| 219 |
title="Test auto-reference", |
| 220 |
version="0.1", |
| 221 |
openapi_version="2.0", |
| 222 |
plugins=(MarshmallowPlugin(schema_name_resolver=resolver),), |
| 223 |
) |
| 224 |
|
| 225 |
spec.components.schema("PetFamily", schema=PetFamilySchema) |
| 226 |
props = get_schemas(spec)["PetFamily"]["properties"] |
| 227 |
pets_1 = props["pets_1"] |
| 228 |
pets_2 = props["pets_2"] |
| 229 |
assert pets_1["type"] == pets_2["type"] == "array" |
| 230 |
|
| 231 |
|
| 232 |
class TestComponentParameterHelper: |
| 233 |
@pytest.mark.parametrize("schema", [PetSchema, PetSchema()]) |
| 234 |
def test_can_use_schema_in_parameter(self, spec, schema): |
| 235 |
param = {"schema": schema} |
| 236 |
spec.components.parameter("Pet", "body", param) |
| 237 |
parameter = get_parameters(spec)["Pet"] |
| 238 |
assert parameter["in"] == "body" |
| 239 |
reference = parameter["schema"] |
| 240 |
assert reference == build_ref(spec, "schema", "Pet") |
| 241 |
|
| 242 |
resolved_schema = spec.components.schemas["Pet"] |
| 243 |
assert resolved_schema["properties"]["name"]["type"] == "string" |
| 244 |
assert resolved_schema["properties"]["password"]["type"] == "string" |
| 245 |
assert resolved_schema["properties"]["id"]["type"] == "integer" |
| 246 |
|
| 247 |
@pytest.mark.parametrize("spec", ("3.0.0",), indirect=True) |
| 248 |
@pytest.mark.parametrize("schema", [PetSchema, PetSchema()]) |
| 249 |
def test_can_use_schema_in_parameter_with_content(self, spec, schema): |
| 250 |
param = {"content": {"application/json": {"schema": schema}}} |
| 251 |
spec.components.parameter("Pet", "body", param) |
| 252 |
parameter = get_parameters(spec)["Pet"] |
| 253 |
assert parameter["in"] == "body" |
| 254 |
reference = parameter["content"]["application/json"]["schema"] |
| 255 |
assert reference == build_ref(spec, "schema", "Pet") |
| 256 |
|
| 257 |
resolved_schema = spec.components.schemas["Pet"] |
| 258 |
assert resolved_schema["properties"]["name"]["type"] == "string" |
| 259 |
assert resolved_schema["properties"]["password"]["type"] == "string" |
| 260 |
assert resolved_schema["properties"]["id"]["type"] == "integer" |
| 261 |
|
| 262 |
|
| 263 |
class TestComponentResponseHelper: |
| 264 |
@pytest.mark.parametrize("schema", [PetSchema, PetSchema()]) |
| 265 |
def test_can_use_schema_in_response(self, spec, schema): |
| 266 |
if spec.openapi_version.major < 3: |
| 267 |
resp = {"schema": schema} |
| 268 |
else: |
| 269 |
resp = {"content": {"application/json": {"schema": schema}}} |
| 270 |
spec.components.response("GetPetOk", resp) |
| 271 |
response = get_responses(spec)["GetPetOk"] |
| 272 |
if spec.openapi_version.major < 3: |
| 273 |
reference = response["schema"] |
| 274 |
else: |
| 275 |
reference = response["content"]["application/json"]["schema"] |
| 276 |
assert reference == build_ref(spec, "schema", "Pet") |
| 277 |
|
| 278 |
resolved_schema = spec.components.schemas["Pet"] |
| 279 |
assert resolved_schema["properties"]["id"]["type"] == "integer" |
| 280 |
assert resolved_schema["properties"]["name"]["type"] == "string" |
| 281 |
assert resolved_schema["properties"]["password"]["type"] == "string" |
| 282 |
|
| 283 |
@pytest.mark.parametrize("schema", [PetSchema, PetSchema()]) |
| 284 |
def test_can_use_schema_in_response_header(self, spec, schema): |
| 285 |
resp = {"headers": {"PetHeader": {"schema": schema}}} |
| 286 |
spec.components.response("GetPetOk", resp) |
| 287 |
response = get_responses(spec)["GetPetOk"] |
| 288 |
reference = response["headers"]["PetHeader"]["schema"] |
| 289 |
assert reference == build_ref(spec, "schema", "Pet") |
| 290 |
|
| 291 |
resolved_schema = spec.components.schemas["Pet"] |
| 292 |
assert resolved_schema["properties"]["id"]["type"] == "integer" |
| 293 |
assert resolved_schema["properties"]["name"]["type"] == "string" |
| 294 |
assert resolved_schema["properties"]["password"]["type"] == "string" |
| 295 |
|
| 296 |
@pytest.mark.parametrize("spec", ("3.0.0",), indirect=True) |
| 297 |
def test_content_without_schema(self, spec): |
| 298 |
resp = {"content": {"application/json": {"example": {"name": "Example"}}}} |
| 299 |
spec.components.response("GetPetOk", resp) |
| 300 |
response = get_responses(spec)["GetPetOk"] |
| 301 |
assert response == resp |
| 302 |
|
| 303 |
|
| 304 |
class TestComponentHeaderHelper: |
| 305 |
@pytest.mark.parametrize("spec", ("3.0.0",), indirect=True) |
| 306 |
@pytest.mark.parametrize("schema", [PetSchema, PetSchema()]) |
| 307 |
def test_can_use_schema_in_header(self, spec, schema): |
| 308 |
param = {"schema": schema} |
| 309 |
spec.components.header("Pet", param) |
| 310 |
header = get_headers(spec)["Pet"] |
| 311 |
reference = header["schema"] |
| 312 |
assert reference == build_ref(spec, "schema", "Pet") |
| 313 |
|
| 314 |
resolved_schema = spec.components.schemas["Pet"] |
| 315 |
assert resolved_schema["properties"]["name"]["type"] == "string" |
| 316 |
assert resolved_schema["properties"]["password"]["type"] == "string" |
| 317 |
assert resolved_schema["properties"]["id"]["type"] == "integer" |
| 318 |
|
| 319 |
|
| 320 |
class TestCustomField: |
| 321 |
def test_can_use_custom_field_decorator(self, spec_fixture): |
| 322 |
class CustomNameA(Field): |
| 323 |
pass |
| 324 |
|
| 325 |
spec_fixture.marshmallow_plugin.map_to_openapi_type(CustomNameA, DateTime) |
| 326 |
|
| 327 |
class CustomNameB(Field): |
| 328 |
pass |
| 329 |
|
| 330 |
spec_fixture.marshmallow_plugin.map_to_openapi_type( |
| 331 |
CustomNameB, "integer", "int32" |
| 332 |
) |
| 333 |
|
| 334 |
class BadCustomField(Field): |
| 335 |
pass |
| 336 |
|
| 337 |
with pytest.raises(TypeError): |
| 338 |
spec_fixture.marshmallow_plugin.map_to_openapi_type( |
| 339 |
BadCustomField, "integer" |
| 340 |
) |
| 341 |
|
| 342 |
class CustomPetASchema(PetSchema): |
| 343 |
name = CustomNameA() |
| 344 |
|
| 345 |
class CustomPetBSchema(PetSchema): |
| 346 |
name = CustomNameB() |
| 347 |
|
| 348 |
spec_fixture.spec.components.schema("Pet", schema=PetSchema) |
| 349 |
spec_fixture.spec.components.schema("CustomPetA", schema=CustomPetASchema) |
| 350 |
spec_fixture.spec.components.schema("CustomPetB", schema=CustomPetBSchema) |
| 351 |
|
| 352 |
props_0 = get_schemas(spec_fixture.spec)["Pet"]["properties"] |
| 353 |
props_a = get_schemas(spec_fixture.spec)["CustomPetA"]["properties"] |
| 354 |
props_b = get_schemas(spec_fixture.spec)["CustomPetB"]["properties"] |
| 355 |
|
| 356 |
assert props_0["name"]["type"] == "string" |
| 357 |
assert "format" not in props_0["name"] |
| 358 |
|
| 359 |
assert props_a["name"]["type"] == "string" |
| 360 |
assert props_a["name"]["format"] == "date-time" |
| 361 |
|
| 362 |
assert props_b["name"]["type"] == "integer" |
| 363 |
assert props_b["name"]["format"] == "int32" |
| 364 |
|
| 365 |
|
| 366 |
def get_nested_schema(schema, field_name): |
| 367 |
try: |
| 368 |
return schema._declared_fields[field_name]._schema |
| 369 |
except AttributeError: |
| 370 |
return schema._declared_fields[field_name]._Nested__schema |
| 371 |
|
| 372 |
|
| 373 |
class TestOperationHelper: |
| 374 |
@pytest.fixture |
| 375 |
def make_pet_callback_spec(self, spec_fixture): |
| 376 |
def _make_pet_spec(operations): |
| 377 |
spec_fixture.spec.path( |
| 378 |
path="/pet", |
| 379 |
operations={ |
| 380 |
"post": {"callbacks": {"petEvent": {"petCallbackUrl": operations}}} |
| 381 |
}, |
| 382 |
) |
| 383 |
return spec_fixture |
| 384 |
|
| 385 |
return _make_pet_spec |
| 386 |
|
| 387 |
@pytest.mark.parametrize( |
| 388 |
"pet_schema", |
| 389 |
(PetSchema, PetSchema(), PetSchema(many=True), "tests.schemas.PetSchema"), |
| 390 |
) |
| 391 |
@pytest.mark.parametrize("spec_fixture", ("2.0",), indirect=True) |
| 392 |
def test_schema_v2(self, spec_fixture, pet_schema): |
| 393 |
spec_fixture.spec.path( |
| 394 |
path="/pet", |
| 395 |
operations={ |
| 396 |
"get": { |
| 397 |
"responses": { |
| 398 |
200: { |
| 399 |
"schema": pet_schema, |
| 400 |
"description": "successful operation", |
| 401 |
"headers": {"PetHeader": {"schema": pet_schema}}, |
| 402 |
} |
| 403 |
} |
| 404 |
} |
| 405 |
}, |
| 406 |
) |
| 407 |
get = get_paths(spec_fixture.spec)["/pet"]["get"] |
| 408 |
if isinstance(pet_schema, Schema) and pet_schema.many is True: |
| 409 |
assert get["responses"]["200"]["schema"]["type"] == "array" |
| 410 |
schema_reference = get["responses"]["200"]["schema"]["items"] |
| 411 |
assert ( |
| 412 |
get["responses"]["200"]["headers"]["PetHeader"]["schema"]["type"] |
| 413 |
== "array" |
| 414 |
) |
| 415 |
header_reference = get["responses"]["200"]["headers"]["PetHeader"][ |
| 416 |
"schema" |
| 417 |
]["items"] |
| 418 |
else: |
| 419 |
schema_reference = get["responses"]["200"]["schema"] |
| 420 |
header_reference = get["responses"]["200"]["headers"]["PetHeader"]["schema"] |
| 421 |
assert schema_reference == build_ref(spec_fixture.spec, "schema", "Pet") |
| 422 |
assert header_reference == build_ref(spec_fixture.spec, "schema", "Pet") |
| 423 |
assert len(spec_fixture.spec.components.schemas) == 1 |
| 424 |
resolved_schema = spec_fixture.spec.components.schemas["Pet"] |
| 425 |
assert resolved_schema == spec_fixture.openapi.schema2jsonschema(PetSchema) |
| 426 |
assert get["responses"]["200"]["description"] == "successful operation" |
| 427 |
|
| 428 |
@pytest.mark.parametrize( |
| 429 |
"pet_schema", |
| 430 |
(PetSchema, PetSchema(), PetSchema(many=True), "tests.schemas.PetSchema"), |
| 431 |
) |
| 432 |
@pytest.mark.parametrize("spec_fixture", ("3.0.0",), indirect=True) |
| 433 |
def test_schema_v3(self, spec_fixture, pet_schema): |
| 434 |
spec_fixture.spec.path( |
| 435 |
path="/pet", |
| 436 |
operations={ |
| 437 |
"get": { |
| 438 |
"responses": { |
| 439 |
200: { |
| 440 |
"content": {"application/json": {"schema": pet_schema}}, |
| 441 |
"description": "successful operation", |
| 442 |
"headers": {"PetHeader": {"schema": pet_schema}}, |
| 443 |
} |
| 444 |
} |
| 445 |
} |
| 446 |
}, |
| 447 |
) |
| 448 |
get = get_paths(spec_fixture.spec)["/pet"]["get"] |
| 449 |
if isinstance(pet_schema, Schema) and pet_schema.many is True: |
| 450 |
assert ( |
| 451 |
get["responses"]["200"]["content"]["application/json"]["schema"]["type"] |
| 452 |
== "array" |
| 453 |
) |
| 454 |
schema_reference = get["responses"]["200"]["content"]["application/json"][ |
| 455 |
"schema" |
| 456 |
]["items"] |
| 457 |
assert ( |
| 458 |
get["responses"]["200"]["headers"]["PetHeader"]["schema"]["type"] |
| 459 |
== "array" |
| 460 |
) |
| 461 |
header_reference = get["responses"]["200"]["headers"]["PetHeader"][ |
| 462 |
"schema" |
| 463 |
]["items"] |
| 464 |
else: |
| 465 |
schema_reference = get["responses"]["200"]["content"]["application/json"][ |
| 466 |
"schema" |
| 467 |
] |
| 468 |
header_reference = get["responses"]["200"]["headers"]["PetHeader"]["schema"] |
| 469 |
|
| 470 |
assert schema_reference == build_ref(spec_fixture.spec, "schema", "Pet") |
| 471 |
assert header_reference == build_ref(spec_fixture.spec, "schema", "Pet") |
| 472 |
assert len(spec_fixture.spec.components.schemas) == 1 |
| 473 |
resolved_schema = spec_fixture.spec.components.schemas["Pet"] |
| 474 |
assert resolved_schema == spec_fixture.openapi.schema2jsonschema(PetSchema) |
| 475 |
assert get["responses"]["200"]["description"] == "successful operation" |
| 476 |
|
| 477 |
@pytest.mark.parametrize( |
| 478 |
"pet_schema", |
| 479 |
(PetSchema, PetSchema(), PetSchema(many=True), "tests.schemas.PetSchema"), |
| 480 |
) |
| 481 |
@pytest.mark.parametrize("spec_fixture", ("3.0.0",), indirect=True) |
| 482 |
def test_callback_schema_v3(self, make_pet_callback_spec, pet_schema): |
| 483 |
spec_fixture = make_pet_callback_spec( |
| 484 |
{ |
| 485 |
"get": { |
| 486 |
"responses": { |
| 487 |
"200": { |
| 488 |
"content": {"application/json": {"schema": pet_schema}}, |
| 489 |
"description": "successful operation", |
| 490 |
"headers": {"PetHeader": {"schema": pet_schema}}, |
| 491 |
} |
| 492 |
} |
| 493 |
} |
| 494 |
} |
| 495 |
) |
| 496 |
p = get_paths(spec_fixture.spec)["/pet"] |
| 497 |
c = p["post"]["callbacks"]["petEvent"]["petCallbackUrl"] |
| 498 |
get = c["get"] |
| 499 |
if isinstance(pet_schema, Schema) and pet_schema.many is True: |
| 500 |
assert ( |
| 501 |
get["responses"]["200"]["content"]["application/json"]["schema"]["type"] |
| 502 |
== "array" |
| 503 |
) |
| 504 |
schema_reference = get["responses"]["200"]["content"]["application/json"][ |
| 505 |
"schema" |
| 506 |
]["items"] |
| 507 |
assert ( |
| 508 |
get["responses"]["200"]["headers"]["PetHeader"]["schema"]["type"] |
| 509 |
== "array" |
| 510 |
) |
| 511 |
header_reference = get["responses"]["200"]["headers"]["PetHeader"][ |
| 512 |
"schema" |
| 513 |
]["items"] |
| 514 |
else: |
| 515 |
schema_reference = get["responses"]["200"]["content"]["application/json"][ |
| 516 |
"schema" |
| 517 |
] |
| 518 |
header_reference = get["responses"]["200"]["headers"]["PetHeader"]["schema"] |
| 519 |
|
| 520 |
assert schema_reference == build_ref(spec_fixture.spec, "schema", "Pet") |
| 521 |
assert header_reference == build_ref(spec_fixture.spec, "schema", "Pet") |
| 522 |
assert len(spec_fixture.spec.components.schemas) == 1 |
| 523 |
resolved_schema = spec_fixture.spec.components.schemas["Pet"] |
| 524 |
assert resolved_schema == spec_fixture.openapi.schema2jsonschema(PetSchema) |
| 525 |
assert get["responses"]["200"]["description"] == "successful operation" |
| 526 |
|
| 527 |
@pytest.mark.parametrize("spec_fixture", ("2.0",), indirect=True) |
| 528 |
def test_schema_expand_parameters_v2(self, spec_fixture): |
| 529 |
spec_fixture.spec.path( |
| 530 |
path="/pet", |
| 531 |
operations={ |
| 532 |
"get": {"parameters": [{"in": "query", "schema": PetSchema}]}, |
| 533 |
"post": { |
| 534 |
"parameters": [ |
| 535 |
{ |
| 536 |
"in": "body", |
| 537 |
"description": "a pet schema", |
| 538 |
"required": True, |
| 539 |
"name": "pet", |
| 540 |
"schema": PetSchema, |
| 541 |
} |
| 542 |
] |
| 543 |
}, |
| 544 |
}, |
| 545 |
) |
| 546 |
p = get_paths(spec_fixture.spec)["/pet"] |
| 547 |
get = p["get"] |
| 548 |
assert get["parameters"] == spec_fixture.openapi.schema2parameters( |
| 549 |
PetSchema(), location="query" |
| 550 |
) |
| 551 |
post = p["post"] |
| 552 |
assert post["parameters"] == spec_fixture.openapi.schema2parameters( |
| 553 |
PetSchema, |
| 554 |
location="body", |
| 555 |
required=True, |
| 556 |
name="pet", |
| 557 |
description="a pet schema", |
| 558 |
) |
| 559 |
|
| 560 |
@pytest.mark.parametrize("spec_fixture", ("3.0.0",), indirect=True) |
| 561 |
def test_schema_expand_parameters_v3(self, spec_fixture): |
| 562 |
spec_fixture.spec.path( |
| 563 |
path="/pet", |
| 564 |
operations={ |
| 565 |
"get": {"parameters": [{"in": "query", "schema": PetSchema}]}, |
| 566 |
"post": { |
| 567 |
"requestBody": { |
| 568 |
"description": "a pet schema", |
| 569 |
"required": True, |
| 570 |
"content": {"application/json": {"schema": PetSchema}}, |
| 571 |
} |
| 572 |
}, |
| 573 |
}, |
| 574 |
) |
| 575 |
p = get_paths(spec_fixture.spec)["/pet"] |
| 576 |
get = p["get"] |
| 577 |
assert get["parameters"] == spec_fixture.openapi.schema2parameters( |
| 578 |
PetSchema(), location="query" |
| 579 |
) |
| 580 |
for parameter in get["parameters"]: |
| 581 |
description = parameter.get("description", False) |
| 582 |
assert description |
| 583 |
name = parameter["name"] |
| 584 |
assert description == PetSchema.description[name] |
| 585 |
post = p["post"] |
| 586 |
post_schema = spec_fixture.marshmallow_plugin.resolver.resolve_schema_dict( |
| 587 |
PetSchema |
| 588 |
) |
| 589 |
assert ( |
| 590 |
post["requestBody"]["content"]["application/json"]["schema"] == post_schema |
| 591 |
) |
| 592 |
assert post["requestBody"]["description"] == "a pet schema" |
| 593 |
assert post["requestBody"]["required"] |
| 594 |
|
| 595 |
@pytest.mark.parametrize("spec_fixture", ("3.0.0",), indirect=True) |
| 596 |
def test_callback_schema_expand_parameters_v3(self, make_pet_callback_spec): |
| 597 |
spec_fixture = make_pet_callback_spec( |
| 598 |
{ |
| 599 |
"get": {"parameters": [{"in": "query", "schema": PetSchema}]}, |
| 600 |
"post": { |
| 601 |
"requestBody": { |
| 602 |
"description": "a pet schema", |
| 603 |
"required": True, |
| 604 |
"content": {"application/json": {"schema": PetSchema}}, |
| 605 |
} |
| 606 |
}, |
| 607 |
} |
| 608 |
) |
| 609 |
p = get_paths(spec_fixture.spec)["/pet"] |
| 610 |
c = p["post"]["callbacks"]["petEvent"]["petCallbackUrl"] |
| 611 |
get = c["get"] |
| 612 |
assert get["parameters"] == spec_fixture.openapi.schema2parameters( |
| 613 |
PetSchema(), location="query" |
| 614 |
) |
| 615 |
for parameter in get["parameters"]: |
| 616 |
description = parameter.get("description", False) |
| 617 |
assert description |
| 618 |
name = parameter["name"] |
| 619 |
assert description == PetSchema.description[name] |
| 620 |
post = c["post"] |
| 621 |
post_schema = spec_fixture.marshmallow_plugin.resolver.resolve_schema_dict( |
| 622 |
PetSchema |
| 623 |
) |
| 624 |
assert ( |
| 625 |
post["requestBody"]["content"]["application/json"]["schema"] == post_schema |
| 626 |
) |
| 627 |
assert post["requestBody"]["description"] == "a pet schema" |
| 628 |
assert post["requestBody"]["required"] |
| 629 |
|
| 630 |
@pytest.mark.parametrize("spec_fixture", ("2.0",), indirect=True) |
| 631 |
def test_schema_uses_ref_if_available_v2(self, spec_fixture): |
| 632 |
spec_fixture.spec.components.schema("Pet", schema=PetSchema) |
| 633 |
spec_fixture.spec.path( |
| 634 |
path="/pet", operations={"get": {"responses": {200: {"schema": PetSchema}}}} |
| 635 |
) |
| 636 |
get = get_paths(spec_fixture.spec)["/pet"]["get"] |
| 637 |
assert get["responses"]["200"]["schema"] == build_ref( |
| 638 |
spec_fixture.spec, "schema", "Pet" |
| 639 |
) |
| 640 |
|
| 641 |
@pytest.mark.parametrize("spec_fixture", ("3.0.0",), indirect=True) |
| 642 |
def test_schema_uses_ref_if_available_v3(self, spec_fixture): |
| 643 |
spec_fixture.spec.components.schema("Pet", schema=PetSchema) |
| 644 |
spec_fixture.spec.path( |
| 645 |
path="/pet", |
| 646 |
operations={ |
| 647 |
"get": { |
| 648 |
"responses": { |
| 649 |
200: {"content": {"application/json": {"schema": PetSchema}}} |
| 650 |
} |
| 651 |
} |
| 652 |
}, |
| 653 |
) |
| 654 |
get = get_paths(spec_fixture.spec)["/pet"]["get"] |
| 655 |
assert get["responses"]["200"]["content"]["application/json"][ |
| 656 |
"schema" |
| 657 |
] == build_ref(spec_fixture.spec, "schema", "Pet") |
| 658 |
|
| 659 |
@pytest.mark.parametrize("spec_fixture", ("3.0.0",), indirect=True) |
| 660 |
def test_callback_schema_uses_ref_if_available_v3(self, make_pet_callback_spec): |
| 661 |
spec_fixture = make_pet_callback_spec( |
| 662 |
{ |
| 663 |
"get": { |
| 664 |
"responses": { |
| 665 |
"200": {"content": {"application/json": {"schema": PetSchema}}} |
| 666 |
} |
| 667 |
} |
| 668 |
} |
| 669 |
) |
| 670 |
p = get_paths(spec_fixture.spec)["/pet"] |
| 671 |
c = p["post"]["callbacks"]["petEvent"]["petCallbackUrl"] |
| 672 |
get = c["get"] |
| 673 |
assert get["responses"]["200"]["content"]["application/json"][ |
| 674 |
"schema" |
| 675 |
] == build_ref(spec_fixture.spec, "schema", "Pet") |
| 676 |
|
| 677 |
def test_schema_uses_ref_if_available_name_resolver_returns_none_v2(self): |
| 678 |
def resolver(schema): |
| 679 |
return None |
| 680 |
|
| 681 |
spec = APISpec( |
| 682 |
title="Test auto-reference", |
| 683 |
version="0.1", |
| 684 |
openapi_version="2.0", |
| 685 |
plugins=(MarshmallowPlugin(schema_name_resolver=resolver),), |
| 686 |
) |
| 687 |
spec.components.schema("Pet", schema=PetSchema) |
| 688 |
spec.path( |
| 689 |
path="/pet", operations={"get": {"responses": {200: {"schema": PetSchema}}}} |
| 690 |
) |
| 691 |
get = get_paths(spec)["/pet"]["get"] |
| 692 |
assert get["responses"]["200"]["schema"] == build_ref(spec, "schema", "Pet") |
| 693 |
|
| 694 |
def test_schema_uses_ref_if_available_name_resolver_returns_none_v3(self): |
| 695 |
def resolver(schema): |
| 696 |
return None |
| 697 |
|
| 698 |
spec = APISpec( |
| 699 |
title="Test auto-reference", |
| 700 |
version="0.1", |
| 701 |
openapi_version="3.0.0", |
| 702 |
plugins=(MarshmallowPlugin(schema_name_resolver=resolver),), |
| 703 |
) |
| 704 |
spec.components.schema("Pet", schema=PetSchema) |
| 705 |
spec.path( |
| 706 |
path="/pet", |
| 707 |
operations={ |
| 708 |
"get": { |
| 709 |
"responses": { |
| 710 |
200: {"content": {"application/json": {"schema": PetSchema}}} |
| 711 |
} |
| 712 |
} |
| 713 |
}, |
| 714 |
) |
| 715 |
get = get_paths(spec)["/pet"]["get"] |
| 716 |
assert get["responses"]["200"]["content"]["application/json"][ |
| 717 |
"schema" |
| 718 |
] == build_ref(spec, "schema", "Pet") |
| 719 |
|
| 720 |
@pytest.mark.parametrize("spec_fixture", ("2.0",), indirect=True) |
| 721 |
def test_schema_resolver_allof_v2(self, spec_fixture): |
| 722 |
spec_fixture.spec.components.schema("Pet", schema=PetSchema) |
| 723 |
spec_fixture.spec.components.schema("Sample", schema=SampleSchema) |
| 724 |
spec_fixture.spec.path( |
| 725 |
path="/pet", |
| 726 |
operations={ |
| 727 |
"get": { |
| 728 |
"responses": {200: {"schema": {"allOf": [PetSchema, SampleSchema]}}} |
| 729 |
} |
| 730 |
}, |
| 731 |
) |
| 732 |
get = get_paths(spec_fixture.spec)["/pet"]["get"] |
| 733 |
assert get["responses"]["200"]["schema"] == { |
| 734 |
"allOf": [ |
| 735 |
build_ref(spec_fixture.spec, "schema", "Pet"), |
| 736 |
build_ref(spec_fixture.spec, "schema", "Sample"), |
| 737 |
] |
| 738 |
} |
| 739 |
|
| 740 |
@pytest.mark.parametrize("spec_fixture", ("3.0.0",), indirect=True) |
| 741 |
@pytest.mark.parametrize("combinator", ["oneOf", "anyOf", "allOf"]) |
| 742 |
def test_schema_resolver_oneof_anyof_allof_v3(self, spec_fixture, combinator): |
| 743 |
spec_fixture.spec.components.schema("Pet", schema=PetSchema) |
| 744 |
spec_fixture.spec.path( |
| 745 |
path="/pet", |
| 746 |
operations={ |
| 747 |
"get": { |
| 748 |
"responses": { |
| 749 |
200: { |
| 750 |
"content": { |
| 751 |
"application/json": { |
| 752 |
"schema": {combinator: [PetSchema, SampleSchema]} |
| 753 |
} |
| 754 |
} |
| 755 |
} |
| 756 |
} |
| 757 |
} |
| 758 |
}, |
| 759 |
) |
| 760 |
get = get_paths(spec_fixture.spec)["/pet"]["get"] |
| 761 |
assert get["responses"]["200"]["content"]["application/json"]["schema"] == { |
| 762 |
combinator: [ |
| 763 |
build_ref(spec_fixture.spec, "schema", "Pet"), |
| 764 |
build_ref(spec_fixture.spec, "schema", "Sample"), |
| 765 |
] |
| 766 |
} |
| 767 |
|
| 768 |
@pytest.mark.parametrize("spec_fixture", ("2.0",), indirect=True) |
| 769 |
def test_schema_resolver_not_v2(self, spec_fixture): |
| 770 |
spec_fixture.spec.components.schema("Pet", schema=PetSchema) |
| 771 |
spec_fixture.spec.path( |
| 772 |
path="/pet", |
| 773 |
operations={"get": {"responses": {200: {"schema": {"not": PetSchema}}}}}, |
| 774 |
) |
| 775 |
get = get_paths(spec_fixture.spec)["/pet"]["get"] |
| 776 |
assert get["responses"]["200"]["schema"] == { |
| 777 |
"not": build_ref(spec_fixture.spec, "schema", "Pet"), |
| 778 |
} |
| 779 |
|
| 780 |
@pytest.mark.parametrize("spec_fixture", ("3.0.0",), indirect=True) |
| 781 |
def test_schema_resolver_not_v3(self, spec_fixture): |
| 782 |
spec_fixture.spec.components.schema("Pet", schema=PetSchema) |
| 783 |
spec_fixture.spec.path( |
| 784 |
path="/pet", |
| 785 |
operations={ |
| 786 |
"get": { |
| 787 |
"responses": { |
| 788 |
200: { |
| 789 |
"content": { |
| 790 |
"application/json": {"schema": {"not": PetSchema}} |
| 791 |
} |
| 792 |
} |
| 793 |
} |
| 794 |
} |
| 795 |
}, |
| 796 |
) |
| 797 |
get = get_paths(spec_fixture.spec)["/pet"]["get"] |
| 798 |
assert get["responses"]["200"]["content"]["application/json"]["schema"] == { |
| 799 |
"not": build_ref(spec_fixture.spec, "schema", "Pet"), |
| 800 |
} |
| 801 |
|
| 802 |
@pytest.mark.parametrize( |
| 803 |
"pet_schema", |
| 804 |
(PetSchema, PetSchema(), "tests.schemas.PetSchema"), |
| 805 |
) |
| 806 |
def test_schema_name_resolver_returns_none_v2(self, pet_schema): |
| 807 |
def resolver(schema): |
| 808 |
return None |
| 809 |
|
| 810 |
spec = APISpec( |
| 811 |
title="Test resolver returns None", |
| 812 |
version="0.1", |
| 813 |
openapi_version="2.0", |
| 814 |
plugins=(MarshmallowPlugin(schema_name_resolver=resolver),), |
| 815 |
) |
| 816 |
spec.path( |
| 817 |
path="/pet", |
| 818 |
operations={"get": {"responses": {200: {"schema": pet_schema}}}}, |
| 819 |
) |
| 820 |
get = get_paths(spec)["/pet"]["get"] |
| 821 |
assert "properties" in get["responses"]["200"]["schema"] |
| 822 |
|
| 823 |
@pytest.mark.parametrize( |
| 824 |
"pet_schema", |
| 825 |
(PetSchema, PetSchema(), "tests.schemas.PetSchema"), |
| 826 |
) |
| 827 |
def test_schema_name_resolver_returns_none_v3(self, pet_schema): |
| 828 |
def resolver(schema): |
| 829 |
return None |
| 830 |
|
| 831 |
spec = APISpec( |
| 832 |
title="Test resolver returns None", |
| 833 |
version="0.1", |
| 834 |
openapi_version="3.0.0", |
| 835 |
plugins=(MarshmallowPlugin(schema_name_resolver=resolver),), |
| 836 |
) |
| 837 |
spec.path( |
| 838 |
path="/pet", |
| 839 |
operations={ |
| 840 |
"get": { |
| 841 |
"responses": { |
| 842 |
200: {"content": {"application/json": {"schema": pet_schema}}} |
| 843 |
} |
| 844 |
} |
| 845 |
}, |
| 846 |
) |
| 847 |
get = get_paths(spec)["/pet"]["get"] |
| 848 |
assert ( |
| 849 |
"properties" |
| 850 |
in get["responses"]["200"]["content"]["application/json"]["schema"] |
| 851 |
) |
| 852 |
|
| 853 |
def test_callback_schema_uses_ref_if_available_name_resolver_returns_none_v3(self): |
| 854 |
def resolver(schema): |
| 855 |
return None |
| 856 |
|
| 857 |
spec = APISpec( |
| 858 |
title="Test auto-reference", |
| 859 |
version="0.1", |
| 860 |
openapi_version="3.0.0", |
| 861 |
plugins=(MarshmallowPlugin(schema_name_resolver=resolver),), |
| 862 |
) |
| 863 |
spec.components.schema("Pet", schema=PetSchema) |
| 864 |
spec.path( |
| 865 |
path="/pet", |
| 866 |
operations={ |
| 867 |
"post": { |
| 868 |
"callbacks": { |
| 869 |
"petEvent": { |
| 870 |
"petCallbackUrl": { |
| 871 |
"get": { |
| 872 |
"responses": { |
| 873 |
"200": { |
| 874 |
"content": { |
| 875 |
"application/json": { |
| 876 |
"schema": PetSchema |
| 877 |
} |
| 878 |
} |
| 879 |
} |
| 880 |
} |
| 881 |
} |
| 882 |
} |
| 883 |
} |
| 884 |
} |
| 885 |
} |
| 886 |
}, |
| 887 |
) |
| 888 |
p = get_paths(spec)["/pet"] |
| 889 |
c = p["post"]["callbacks"]["petEvent"]["petCallbackUrl"] |
| 890 |
get = c["get"] |
| 891 |
assert get["responses"]["200"]["content"]["application/json"][ |
| 892 |
"schema" |
| 893 |
] == build_ref(spec, "schema", "Pet") |
| 894 |
|
| 895 |
@pytest.mark.parametrize("spec_fixture", ("2.0",), indirect=True) |
| 896 |
def test_schema_uses_ref_in_parameters_and_request_body_if_available_v2( |
| 897 |
self, spec_fixture |
| 898 |
): |
| 899 |
spec_fixture.spec.components.schema("Pet", schema=PetSchema) |
| 900 |
spec_fixture.spec.path( |
| 901 |
path="/pet", |
| 902 |
operations={ |
| 903 |
"get": {"parameters": [{"in": "query", "schema": PetSchema}]}, |
| 904 |
"post": {"parameters": [{"in": "body", "schema": PetSchema}]}, |
| 905 |
}, |
| 906 |
) |
| 907 |
p = get_paths(spec_fixture.spec)["/pet"] |
| 908 |
assert "schema" not in p["get"]["parameters"][0] |
| 909 |
post = p["post"] |
| 910 |
assert len(post["parameters"]) == 1 |
| 911 |
assert post["parameters"][0]["schema"] == build_ref( |
| 912 |
spec_fixture.spec, "schema", "Pet" |
| 913 |
) |
| 914 |
|
| 915 |
@pytest.mark.parametrize("spec_fixture", ("3.0.0",), indirect=True) |
| 916 |
def test_schema_uses_ref_in_parameters_and_request_body_if_available_v3( |
| 917 |
self, spec_fixture |
| 918 |
): |
| 919 |
spec_fixture.spec.components.schema("Pet", schema=PetSchema) |
| 920 |
spec_fixture.spec.path( |
| 921 |
path="/pet", |
| 922 |
operations={ |
| 923 |
"get": {"parameters": [{"in": "query", "schema": PetSchema}]}, |
| 924 |
"post": { |
| 925 |
"requestBody": { |
| 926 |
"content": {"application/json": {"schema": PetSchema}} |
| 927 |
} |
| 928 |
}, |
| 929 |
}, |
| 930 |
) |
| 931 |
p = get_paths(spec_fixture.spec)["/pet"] |
| 932 |
assert "schema" in p["get"]["parameters"][0] |
| 933 |
post = p["post"] |
| 934 |
schema_ref = post["requestBody"]["content"]["application/json"]["schema"] |
| 935 |
assert schema_ref == build_ref(spec_fixture.spec, "schema", "Pet") |
| 936 |
|
| 937 |
@pytest.mark.parametrize("spec_fixture", ("3.0.0",), indirect=True) |
| 938 |
def test_callback_schema_uses_ref_in_parameters_and_request_body_if_available_v3( |
| 939 |
self, make_pet_callback_spec |
| 940 |
): |
| 941 |
spec_fixture = make_pet_callback_spec( |
| 942 |
{ |
| 943 |
"get": {"parameters": [{"in": "query", "schema": PetSchema}]}, |
| 944 |
"post": { |
| 945 |
"requestBody": { |
| 946 |
"content": {"application/json": {"schema": PetSchema}} |
| 947 |
} |
| 948 |
}, |
| 949 |
} |
| 950 |
) |
| 951 |
p = get_paths(spec_fixture.spec)["/pet"] |
| 952 |
c = p["post"]["callbacks"]["petEvent"]["petCallbackUrl"] |
| 953 |
assert "schema" in c["get"]["parameters"][0] |
| 954 |
post = c["post"] |
| 955 |
schema_ref = post["requestBody"]["content"]["application/json"]["schema"] |
| 956 |
assert schema_ref == build_ref(spec_fixture.spec, "schema", "Pet") |
| 957 |
|
| 958 |
@pytest.mark.parametrize("spec_fixture", ("2.0",), indirect=True) |
| 959 |
def test_schema_array_uses_ref_if_available_v2(self, spec_fixture): |
| 960 |
spec_fixture.spec.components.schema("Pet", schema=PetSchema) |
| 961 |
spec_fixture.spec.path( |
| 962 |
path="/pet", |
| 963 |
operations={ |
| 964 |
"get": { |
| 965 |
"parameters": [ |
| 966 |
{ |
| 967 |
"name": "petSchema", |
| 968 |
"in": "body", |
| 969 |
"schema": {"type": "array", "items": PetSchema}, |
| 970 |
} |
| 971 |
], |
| 972 |
"responses": { |
| 973 |
200: {"schema": {"type": "array", "items": PetSchema}} |
| 974 |
}, |
| 975 |
} |
| 976 |
}, |
| 977 |
) |
| 978 |
get = get_paths(spec_fixture.spec)["/pet"]["get"] |
| 979 |
assert len(get["parameters"]) == 1 |
| 980 |
resolved_schema = { |
| 981 |
"type": "array", |
| 982 |
"items": build_ref(spec_fixture.spec, "schema", "Pet"), |
| 983 |
} |
| 984 |
assert get["parameters"][0]["schema"] == resolved_schema |
| 985 |
assert get["responses"]["200"]["schema"] == resolved_schema |
| 986 |
|
| 987 |
@pytest.mark.parametrize("spec_fixture", ("3.0.0",), indirect=True) |
| 988 |
def test_schema_array_uses_ref_if_available_v3(self, spec_fixture): |
| 989 |
spec_fixture.spec.components.schema("Pet", schema=PetSchema) |
| 990 |
spec_fixture.spec.path( |
| 991 |
path="/pet", |
| 992 |
operations={ |
| 993 |
"get": { |
| 994 |
"parameters": [ |
| 995 |
{ |
| 996 |
"name": "Pet", |
| 997 |
"in": "query", |
| 998 |
"content": { |
| 999 |
"application/json": { |
| 1000 |
"schema": {"type": "array", "items": PetSchema} |
| 1001 |
} |
| 1002 |
}, |
| 1003 |
} |
| 1004 |
], |
| 1005 |
"responses": { |
| 1006 |
200: { |
| 1007 |
"content": { |
| 1008 |
"application/json": { |
| 1009 |
"schema": {"type": "array", "items": PetSchema} |
| 1010 |
} |
| 1011 |
} |
| 1012 |
} |
| 1013 |
}, |
| 1014 |
} |
| 1015 |
}, |
| 1016 |
) |
| 1017 |
get = get_paths(spec_fixture.spec)["/pet"]["get"] |
| 1018 |
assert len(get["parameters"]) == 1 |
| 1019 |
resolved_schema = { |
| 1020 |
"type": "array", |
| 1021 |
"items": build_ref(spec_fixture.spec, "schema", "Pet"), |
| 1022 |
} |
| 1023 |
request_schema = get["parameters"][0]["content"]["application/json"]["schema"] |
| 1024 |
assert request_schema == resolved_schema |
| 1025 |
response_schema = get["responses"]["200"]["content"]["application/json"][ |
| 1026 |
"schema" |
| 1027 |
] |
| 1028 |
assert response_schema == resolved_schema |
| 1029 |
|
| 1030 |
@pytest.mark.parametrize("spec_fixture", ("3.0.0",), indirect=True) |
| 1031 |
def test_callback_schema_array_uses_ref_if_available_v3( |
| 1032 |
self, make_pet_callback_spec |
| 1033 |
): |
| 1034 |
spec_fixture = make_pet_callback_spec( |
| 1035 |
{ |
| 1036 |
"get": { |
| 1037 |
"parameters": [ |
| 1038 |
{ |
| 1039 |
"name": "Pet", |
| 1040 |
"in": "query", |
| 1041 |
"content": { |
| 1042 |
"application/json": { |
| 1043 |
"schema": {"type": "array", "items": PetSchema} |
| 1044 |
} |
| 1045 |
}, |
| 1046 |
} |
| 1047 |
], |
| 1048 |
"responses": { |
| 1049 |
"200": { |
| 1050 |
"content": { |
| 1051 |
"application/json": { |
| 1052 |
"schema": {"type": "array", "items": PetSchema} |
| 1053 |
} |
| 1054 |
} |
| 1055 |
} |
| 1056 |
}, |
| 1057 |
} |
| 1058 |
} |
| 1059 |
) |
| 1060 |
p = get_paths(spec_fixture.spec)["/pet"] |
| 1061 |
c = p["post"]["callbacks"]["petEvent"]["petCallbackUrl"] |
| 1062 |
get = c["get"] |
| 1063 |
assert len(get["parameters"]) == 1 |
| 1064 |
resolved_schema = { |
| 1065 |
"type": "array", |
| 1066 |
"items": build_ref(spec_fixture.spec, "schema", "Pet"), |
| 1067 |
} |
| 1068 |
request_schema = get["parameters"][0]["content"]["application/json"]["schema"] |
| 1069 |
assert request_schema == resolved_schema |
| 1070 |
response_schema = get["responses"]["200"]["content"]["application/json"][ |
| 1071 |
"schema" |
| 1072 |
] |
| 1073 |
assert response_schema == resolved_schema |
| 1074 |
|
| 1075 |
@pytest.mark.parametrize("spec_fixture", ("2.0",), indirect=True) |
| 1076 |
def test_schema_partially_v2(self, spec_fixture): |
| 1077 |
spec_fixture.spec.components.schema("Pet", schema=PetSchema) |
| 1078 |
spec_fixture.spec.path( |
| 1079 |
path="/parents", |
| 1080 |
operations={ |
| 1081 |
"get": { |
| 1082 |
"responses": { |
| 1083 |
200: { |
| 1084 |
"schema": { |
| 1085 |
"type": "object", |
| 1086 |
"properties": { |
| 1087 |
"mother": PetSchema, |
| 1088 |
"father": PetSchema, |
| 1089 |
}, |
| 1090 |
} |
| 1091 |
} |
| 1092 |
} |
| 1093 |
} |
| 1094 |
}, |
| 1095 |
) |
| 1096 |
get = get_paths(spec_fixture.spec)["/parents"]["get"] |
| 1097 |
assert get["responses"]["200"]["schema"] == { |
| 1098 |
"type": "object", |
| 1099 |
"properties": { |
| 1100 |
"mother": build_ref(spec_fixture.spec, "schema", "Pet"), |
| 1101 |
"father": build_ref(spec_fixture.spec, "schema", "Pet"), |
| 1102 |
}, |
| 1103 |
} |
| 1104 |
|
| 1105 |
@pytest.mark.parametrize("spec_fixture", ("3.0.0",), indirect=True) |
| 1106 |
def test_schema_partially_v3(self, spec_fixture): |
| 1107 |
spec_fixture.spec.components.schema("Pet", schema=PetSchema) |
| 1108 |
spec_fixture.spec.path( |
| 1109 |
path="/parents", |
| 1110 |
operations={ |
| 1111 |
"get": { |
| 1112 |
"responses": { |
| 1113 |
200: { |
| 1114 |
"content": { |
| 1115 |
"application/json": { |
| 1116 |
"schema": { |
| 1117 |
"type": "object", |
| 1118 |
"properties": { |
| 1119 |
"mother": PetSchema, |
| 1120 |
"father": PetSchema, |
| 1121 |
}, |
| 1122 |
} |
| 1123 |
} |
| 1124 |
} |
| 1125 |
} |
| 1126 |
} |
| 1127 |
} |
| 1128 |
}, |
| 1129 |
) |
| 1130 |
get = get_paths(spec_fixture.spec)["/parents"]["get"] |
| 1131 |
assert get["responses"]["200"]["content"]["application/json"]["schema"] == { |
| 1132 |
"type": "object", |
| 1133 |
"properties": { |
| 1134 |
"mother": build_ref(spec_fixture.spec, "schema", "Pet"), |
| 1135 |
"father": build_ref(spec_fixture.spec, "schema", "Pet"), |
| 1136 |
}, |
| 1137 |
} |
| 1138 |
|
| 1139 |
@pytest.mark.parametrize("spec_fixture", ("3.0.0",), indirect=True) |
| 1140 |
def test_callback_schema_partially_v3(self, make_pet_callback_spec): |
| 1141 |
spec_fixture = make_pet_callback_spec( |
| 1142 |
{ |
| 1143 |
"get": { |
| 1144 |
"responses": { |
| 1145 |
"200": { |
| 1146 |
"content": { |
| 1147 |
"application/json": { |
| 1148 |
"schema": { |
| 1149 |
"type": "object", |
| 1150 |
"properties": { |
| 1151 |
"mother": PetSchema, |
| 1152 |
"father": PetSchema, |
| 1153 |
}, |
| 1154 |
} |
| 1155 |
} |
| 1156 |
} |
| 1157 |
} |
| 1158 |
} |
| 1159 |
} |
| 1160 |
} |
| 1161 |
) |
| 1162 |
p = get_paths(spec_fixture.spec)["/pet"] |
| 1163 |
c = p["post"]["callbacks"]["petEvent"]["petCallbackUrl"] |
| 1164 |
get = c["get"] |
| 1165 |
assert get["responses"]["200"]["content"]["application/json"]["schema"] == { |
| 1166 |
"type": "object", |
| 1167 |
"properties": { |
| 1168 |
"mother": build_ref(spec_fixture.spec, "schema", "Pet"), |
| 1169 |
"father": build_ref(spec_fixture.spec, "schema", "Pet"), |
| 1170 |
}, |
| 1171 |
} |
| 1172 |
|
| 1173 |
def test_parameter_reference(self, spec_fixture): |
| 1174 |
if spec_fixture.spec.openapi_version.major < 3: |
| 1175 |
param = {"schema": PetSchema} |
| 1176 |
else: |
| 1177 |
param = {"content": {"application/json": {"schema": PetSchema}}} |
| 1178 |
spec_fixture.spec.components.parameter("Pet", "body", param) |
| 1179 |
spec_fixture.spec.path( |
| 1180 |
path="/parents", operations={"get": {"parameters": ["Pet"]}} |
| 1181 |
) |
| 1182 |
get = get_paths(spec_fixture.spec)["/parents"]["get"] |
| 1183 |
assert get["parameters"] == [build_ref(spec_fixture.spec, "parameter", "Pet")] |
| 1184 |
|
| 1185 |
def test_response_reference(self, spec_fixture): |
| 1186 |
if spec_fixture.spec.openapi_version.major < 3: |
| 1187 |
resp = {"schema": PetSchema} |
| 1188 |
else: |
| 1189 |
resp = {"content": {"application/json": {"schema": PetSchema}}} |
| 1190 |
spec_fixture.spec.components.response("Pet", resp) |
| 1191 |
spec_fixture.spec.path( |
| 1192 |
path="/parents", operations={"get": {"responses": {"200": "Pet"}}} |
| 1193 |
) |
| 1194 |
get = get_paths(spec_fixture.spec)["/parents"]["get"] |
| 1195 |
assert get["responses"] == { |
| 1196 |
"200": build_ref(spec_fixture.spec, "response", "Pet") |
| 1197 |
} |
| 1198 |
|
| 1199 |
def test_schema_global_state_untouched_2json(self, spec_fixture): |
| 1200 |
assert get_nested_schema(RunSchema, "sample") is None |
| 1201 |
data = spec_fixture.openapi.schema2jsonschema(RunSchema) |
| 1202 |
json.dumps(data) |
| 1203 |
assert get_nested_schema(RunSchema, "sample") is None |
| 1204 |
|
| 1205 |
def test_schema_global_state_untouched_2parameters(self, spec_fixture): |
| 1206 |
assert get_nested_schema(RunSchema, "sample") is None |
| 1207 |
data = spec_fixture.openapi.schema2parameters(RunSchema, location="json") |
| 1208 |
json.dumps(data) |
| 1209 |
assert get_nested_schema(RunSchema, "sample") is None |
| 1210 |
|
| 1211 |
def test_resolve_schema_dict_ref_as_string(self, spec): |
| 1212 |
"""Test schema ref passed as string""" |
| 1213 |
# The case tested here is a reference passed as string, not a |
| 1214 |
# marshmallow Schema passed by name as string. We want to ensure the |
| 1215 |
# MarshmallowPlugin does not interfere with the feature interpreting |
| 1216 |
# strings as references. Therefore, we use a specific name to ensure |
| 1217 |
# there is no Schema with that name in the marshmallow registry from |
| 1218 |
# somewhere else in the tests. |
| 1219 |
# e.g. PetSchema is in the registry already so it wouldn't work. |
| 1220 |
schema = {"schema": "SomeSpecificPetSchema"} |
| 1221 |
if spec.openapi_version.major >= 3: |
| 1222 |
schema = {"content": {"application/json": schema}} |
| 1223 |
spec.path("/pet/{petId}", operations={"get": {"responses": {"200": schema}}}) |
| 1224 |
resp = get_paths(spec)["/pet/{petId}"]["get"]["responses"]["200"] |
| 1225 |
if spec.openapi_version.major < 3: |
| 1226 |
schema = resp["schema"] |
| 1227 |
else: |
| 1228 |
schema = resp["content"]["application/json"]["schema"] |
| 1229 |
assert schema == build_ref(spec, "schema", "SomeSpecificPetSchema") |
| 1230 |
|
| 1231 |
|
| 1232 |
class TestCircularReference: |
| 1233 |
def test_circular_referencing_schemas(self, spec): |
| 1234 |
spec.components.schema("Analysis", schema=AnalysisSchema) |
| 1235 |
definitions = get_schemas(spec) |
| 1236 |
ref = definitions["Analysis"]["properties"]["sample"] |
| 1237 |
assert ref == build_ref(spec, "schema", "Sample") |
| 1238 |
|
| 1239 |
|
| 1240 |
# Regression tests for issue #55 |
| 1241 |
class TestSelfReference: |
| 1242 |
def test_self_referencing_field_single(self, spec): |
| 1243 |
spec.components.schema("SelfReference", schema=SelfReferencingSchema) |
| 1244 |
definitions = get_schemas(spec) |
| 1245 |
ref = definitions["SelfReference"]["properties"]["single"] |
| 1246 |
assert ref == build_ref(spec, "schema", "SelfReference") |
| 1247 |
|
| 1248 |
def test_self_referencing_field_many(self, spec): |
| 1249 |
spec.components.schema("SelfReference", schema=SelfReferencingSchema) |
| 1250 |
definitions = get_schemas(spec) |
| 1251 |
result = definitions["SelfReference"]["properties"]["multiple"] |
| 1252 |
assert result == { |
| 1253 |
"type": "array", |
| 1254 |
"items": build_ref(spec, "schema", "SelfReference"), |
| 1255 |
} |
| 1256 |
|
| 1257 |
|
| 1258 |
class TestFieldOrdering: |
| 1259 |
def test_field_order_preserved(self, spec): |
| 1260 |
class OrderedSchema(Schema): |
| 1261 |
if (MA_VERSION.major == 3) and (MA_VERSION.minor < 26): |
| 1262 |
|
| 1263 |
class Meta: |
| 1264 |
ordered = True |
| 1265 |
|
| 1266 |
field1 = Int() |
| 1267 |
field2 = Int() |
| 1268 |
field3 = Int() |
| 1269 |
field4 = Int() |
| 1270 |
field5 = Int() |
| 1271 |
|
| 1272 |
spec.components.schema("Ordered", schema=OrderedSchema) |
| 1273 |
result = get_schemas(spec)["Ordered"]["properties"] |
| 1274 |
assert list(result.keys()) == ["field1", "field2", "field3", "field4", "field5"] |
| 1275 |
|
| 1276 |
|
| 1277 |
class TestFieldWithCustomProps: |
| 1278 |
def test_field_with_custom_props(self, spec): |
| 1279 |
spec.components.schema("PatternedObject", schema=PatternedObjectSchema) |
| 1280 |
result = get_schemas(spec)["PatternedObject"]["properties"]["count"] |
| 1281 |
assert "x-count" in result |
| 1282 |
assert result["x-count"] == 1 |
| 1283 |
|
| 1284 |
def test_field_with_custom_props_passed_as_snake_case(self, spec): |
| 1285 |
spec.components.schema("PatternedObject", schema=PatternedObjectSchema) |
| 1286 |
result = get_schemas(spec)["PatternedObject"]["properties"]["count2"] |
| 1287 |
assert "x-count2" in result |
| 1288 |
assert result["x-count2"] == 2 |
| 1289 |
|
| 1290 |
|
| 1291 |
class TestSchemaWithDefaultValues: |
| 1292 |
def test_schema_with_default_values(self, spec): |
| 1293 |
spec.components.schema("DefaultValuesSchema", schema=DefaultValuesSchema) |
| 1294 |
definitions = get_schemas(spec) |
| 1295 |
props = definitions["DefaultValuesSchema"]["properties"] |
| 1296 |
assert props["number_auto_default"]["default"] == 12 |
| 1297 |
assert props["number_manual_default"]["default"] == 42 |
| 1298 |
assert "default" not in props["string_callable_default"] |
| 1299 |
assert props["string_manual_default"]["default"] == "Manual" |
| 1300 |
assert "default" not in props["numbers"] |
| 1301 |
|
| 1302 |
|
| 1303 |
class TestDictValues: |
| 1304 |
def test_dict_values_resolve_to_additional_properties(self, spec): |
| 1305 |
class SchemaWithDict(Schema): |
| 1306 |
dict_field = Dict(values=String()) |
| 1307 |
|
| 1308 |
spec.components.schema("SchemaWithDict", schema=SchemaWithDict) |
| 1309 |
result = get_schemas(spec)["SchemaWithDict"]["properties"]["dict_field"] |
| 1310 |
assert result == {"type": "object", "additionalProperties": {"type": "string"}} |
| 1311 |
|
| 1312 |
def test_dict_with_empty_values_field(self, spec): |
| 1313 |
class SchemaWithDict(Schema): |
| 1314 |
dict_field = Dict() |
| 1315 |
|
| 1316 |
spec.components.schema("SchemaWithDict", schema=SchemaWithDict) |
| 1317 |
result = get_schemas(spec)["SchemaWithDict"]["properties"]["dict_field"] |
| 1318 |
assert result == {"type": "object", "additionalProperties": {}} |
| 1319 |
|
| 1320 |
def test_dict_with_empty_values_field_and_metadata(self, spec): |
| 1321 |
class SchemaWithDict(Schema): |
| 1322 |
dict_field = Dict(metadata={"additionalProperties": True}) |
| 1323 |
|
| 1324 |
spec.components.schema("SchemaWithDict", schema=SchemaWithDict) |
| 1325 |
result = get_schemas(spec)["SchemaWithDict"]["properties"]["dict_field"] |
| 1326 |
assert result == {"type": "object", "additionalProperties": True} |
| 1327 |
|
| 1328 |
def test_dict_with_nested(self, spec): |
| 1329 |
class SchemaWithDict(Schema): |
| 1330 |
dict_field = Dict(values=Nested(PetSchema)) |
| 1331 |
|
| 1332 |
spec.components.schema("SchemaWithDict", schema=SchemaWithDict) |
| 1333 |
|
| 1334 |
assert len(get_schemas(spec)) == 2 |
| 1335 |
|
| 1336 |
result = get_schemas(spec)["SchemaWithDict"]["properties"]["dict_field"] |
| 1337 |
assert result == { |
| 1338 |
"additionalProperties": build_ref(spec, "schema", "Pet"), |
| 1339 |
"type": "object", |
| 1340 |
} |
| 1341 |
|
| 1342 |
|
| 1343 |
class TestList: |
| 1344 |
def test_list_with_nested(self, spec): |
| 1345 |
class SchemaWithList(Schema): |
| 1346 |
list_field = List(Nested(PetSchema)) |
| 1347 |
|
| 1348 |
spec.components.schema("SchemaWithList", schema=SchemaWithList) |
| 1349 |
|
| 1350 |
assert len(get_schemas(spec)) == 2 |
| 1351 |
|
| 1352 |
result = get_schemas(spec)["SchemaWithList"]["properties"]["list_field"] |
| 1353 |
assert result == {"items": build_ref(spec, "schema", "Pet"), "type": "array"} |
| 1354 |
|
| 1355 |
|
| 1356 |
class TestTimeDelta: |
| 1357 |
def test_timedelta_x_unit(self, spec): |
| 1358 |
class SchemaWithTimeDelta(Schema): |
| 1359 |
sec = TimeDelta("seconds") |
| 1360 |
day = TimeDelta("days") |
| 1361 |
|
| 1362 |
spec.components.schema("SchemaWithTimeDelta", schema=SchemaWithTimeDelta) |
| 1363 |
|
| 1364 |
assert ( |
| 1365 |
get_schemas(spec)["SchemaWithTimeDelta"]["properties"]["sec"]["x-unit"] |
| 1366 |
== "seconds" |
| 1367 |
) |
| 1368 |
assert ( |
| 1369 |
get_schemas(spec)["SchemaWithTimeDelta"]["properties"]["day"]["x-unit"] |
| 1370 |
== "days" |
| 1371 |
) |