forked from python-openapi/openapi-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: Add test case for nullable references
This is somewhat undefined behavior [1][2], however, it worked until openapi-core 0.15.x. Demonstrate this, pending a fix. [1] https://stackoverflow.com/a/48114924/613428 [2] OAI/OpenAPI-Specification#1368 Signed-off-by: Stephen Finucane <stephen@that.guru>
- Loading branch information
1 parent
56863fb
commit b67aa2a
Showing
2 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
openapi: "3.0.0" | ||
info: | ||
version: "0.1" | ||
title: OpenAPI specification with nullable refs | ||
paths: | ||
/people/{personID}: | ||
get: | ||
summary: Show a person | ||
parameters: | ||
- name: personID | ||
in: path | ||
required: true | ||
description: The ID of the person to retrieve | ||
schema: | ||
type: string | ||
format: uuid | ||
responses: | ||
default: | ||
description: Expected response to a valid request | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/Person' | ||
components: | ||
schemas: | ||
Person: | ||
x-model: Person | ||
type: object | ||
required: | ||
- id | ||
- name | ||
properties: | ||
id: | ||
description: The ID of the person. | ||
type: string | ||
format: uuid | ||
name: | ||
description: The full name of the person. | ||
type: string | ||
user: | ||
description: The associated user account, if any. | ||
nullable: true | ||
allOf: | ||
- $ref: '#/components/schemas/User' | ||
User: | ||
x-model: User | ||
type: object | ||
required: | ||
- id | ||
- username | ||
properties: | ||
id: | ||
description: The ID of the user. | ||
type: string | ||
format: uuid | ||
username: | ||
description: The username of the user. | ||
type: string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from dataclasses import is_dataclass | ||
import json | ||
import uuid | ||
|
||
import pytest | ||
|
||
from openapi_core.testing import MockRequest | ||
from openapi_core.testing import MockResponse | ||
from openapi_core.validation.response import openapi_v30_response_validator | ||
|
||
|
||
@pytest.fixture(scope="class") | ||
def spec(factory): | ||
return factory.spec_from_file("data/v3.0/nullable_ref.yaml") | ||
|
||
|
||
class TestNullableRefs: | ||
@pytest.mark.xfail(message="The nullable attribute should be respected") | ||
def test_with_null_value(self, spec): | ||
person = { | ||
"id": str(uuid.uuid4()), | ||
"name": "Joe Bloggs", | ||
"user": None, | ||
} | ||
request = MockRequest("", "get", f"/people/{person['id']}") | ||
response = MockResponse(json.dumps(person)) | ||
|
||
result = openapi_v30_response_validator.validate( | ||
spec, request, response | ||
) | ||
|
||
assert not result.errors | ||
assert is_dataclass(result.data) | ||
assert result.data.__class__.__name__ == "Person" | ||
assert result.data.id == uuid.UUID(person["id"]) | ||
assert result.data.name == person["name"] | ||
assert result.data.user is None | ||
|
||
def test_with_non_null_value(self, spec): | ||
person = { | ||
"id": str(uuid.uuid4()), | ||
"name": "Joe Bloggs", | ||
"user": { | ||
"id": str(uuid.uuid4()), | ||
"username": "joebloggs", | ||
}, | ||
} | ||
request = MockRequest("", "get", f"/people/{person['id']}") | ||
response = MockResponse(json.dumps(person)) | ||
|
||
result = openapi_v30_response_validator.validate( | ||
spec, request, response | ||
) | ||
|
||
assert not result.errors | ||
assert is_dataclass(result.data) | ||
assert result.data.__class__.__name__ == "Person" | ||
assert result.data.id == uuid.UUID(person["id"]) | ||
assert result.data.name == person["name"] | ||
assert result.data.user is not None | ||
assert result.data.user.id == uuid.UUID(person["user"]["id"]) | ||
assert result.data.user.username == person["user"]["username"] |