Skip to content

Commit

Permalink
tests: Add test case for nullable references
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
stephenfin committed Oct 14, 2022
1 parent 3c79042 commit 60b2c12
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
58 changes: 58 additions & 0 deletions tests/integration/data/v3.0/nullable_ref.yaml
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
62 changes: 62 additions & 0 deletions tests/integration/validation/test_nullable_ref.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import json
import uuid
from dataclasses import is_dataclass

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"]

0 comments on commit 60b2c12

Please sign in to comment.