|
| 1 | +from dataclasses import is_dataclass |
| 2 | +import json |
| 3 | +import uuid |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from openapi_core.testing import MockRequest |
| 8 | +from openapi_core.testing import MockResponse |
| 9 | +from openapi_core.validation.response import openapi_v30_response_validator |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture(scope="class") |
| 13 | +def spec(factory): |
| 14 | + return factory.spec_from_file("data/v3.0/nullable_ref.yaml") |
| 15 | + |
| 16 | + |
| 17 | +class TestNullableRefs: |
| 18 | + @pytest.mark.xfail(message="The nullable attribute should be respected") |
| 19 | + def test_with_null_value(self, spec): |
| 20 | + person = { |
| 21 | + "id": str(uuid.uuid4()), |
| 22 | + "name": "Joe Bloggs", |
| 23 | + "user": None, |
| 24 | + } |
| 25 | + request = MockRequest("", "get", f"/people/{person['id']}") |
| 26 | + response = MockResponse(json.dumps(person)) |
| 27 | + |
| 28 | + result = openapi_v30_response_validator.validate( |
| 29 | + spec, request, response |
| 30 | + ) |
| 31 | + |
| 32 | + assert not result.errors |
| 33 | + assert is_dataclass(result.data) |
| 34 | + assert result.data.__class__.__name__ == "Person" |
| 35 | + assert result.data.id == uuid.UUID(person["id"]) |
| 36 | + assert result.data.name == person["name"] |
| 37 | + assert result.data.user is None |
| 38 | + |
| 39 | + def test_with_non_null_value(self, spec): |
| 40 | + person = { |
| 41 | + "id": str(uuid.uuid4()), |
| 42 | + "name": "Joe Bloggs", |
| 43 | + "user": { |
| 44 | + "id": str(uuid.uuid4()), |
| 45 | + "username": "joebloggs", |
| 46 | + }, |
| 47 | + } |
| 48 | + request = MockRequest("", "get", f"/people/{person['id']}") |
| 49 | + response = MockResponse(json.dumps(person)) |
| 50 | + |
| 51 | + result = openapi_v30_response_validator.validate( |
| 52 | + spec, request, response |
| 53 | + ) |
| 54 | + |
| 55 | + assert not result.errors |
| 56 | + assert is_dataclass(result.data) |
| 57 | + assert result.data.__class__.__name__ == "Person" |
| 58 | + assert result.data.id == uuid.UUID(person["id"]) |
| 59 | + assert result.data.name == person["name"] |
| 60 | + assert result.data.user is not None |
| 61 | + assert result.data.user.id == uuid.UUID(person["user"]["id"]) |
| 62 | + assert result.data.user.username == person["user"]["username"] |
0 commit comments