Skip to content

Commit

Permalink
Add support for openapi-core 0.16.x
Browse files Browse the repository at this point in the history
Take two! This time with the necessary fixes to make things actually
work.

Signed-off-by: Stephen Finucane <[email protected]>
  • Loading branch information
stephenfin committed Oct 10, 2022
1 parent d03fc63 commit d5d668e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
34 changes: 21 additions & 13 deletions patchwork/tests/api/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@

from django.urls import resolve
import openapi_core
from openapi_core.contrib.django import DjangoOpenAPIRequestFactory
from openapi_core.contrib.django import DjangoOpenAPIResponseFactory
from openapi_core.exceptions import OpenAPIParameterError
from openapi_core.unmarshalling.schemas.factories import (
SchemaUnmarshallersFactory,
)
from openapi_core.contrib.django import DjangoOpenAPIRequest
from openapi_core.contrib.django import DjangoOpenAPIResponse
from openapi_core.exceptions import OpenAPIError
from openapi_core.templating import util
from openapi_core.unmarshalling.schemas.formatters import Formatter
from openapi_core.validation.request.validators import RequestValidator
from openapi_core.validation.response.validators import ResponseValidator
from openapi_schema_validator import OAS30Validator
from rest_framework import status
import yaml

Expand Down Expand Up @@ -98,7 +102,7 @@ def _load_spec(version):
with open(spec_path, 'r') as fh:
data = yaml.load(fh, Loader=yaml.SafeLoader)

_LOADED_SPECS[version] = openapi_core.create_spec(data)
_LOADED_SPECS[version] = openapi_core.Spec.create(data)

return _LOADED_SPECS[version]

Expand All @@ -110,24 +114,28 @@ def validate_data(
return

spec = _load_spec(resolve(path).kwargs.get('version'))
request = DjangoOpenAPIRequestFactory.create(request)
response = DjangoOpenAPIResponseFactory.create(response)
request = DjangoOpenAPIRequest(request)
response = DjangoOpenAPIResponse(response)

schema_unmarshallers_factory = SchemaUnmarshallersFactory(
OAS30Validator,
custom_formatters=CUSTOM_FORMATTERS,
# context=UnmarshalContext.RESPONSE,
)

# request
if validate_request:
validator = RequestValidator(spec, custom_formatters=CUSTOM_FORMATTERS)
result = validator.validate(request)
validator = RequestValidator(schema_unmarshallers_factory)
result = validator.validate(spec, request)
try:
result.raise_for_errors()
except OpenAPIParameterError:
except OpenAPIError:
# TODO(stephenfin): In API v2.0, this should be an error. As things
# stand, we silently ignore these issues.
assert response.status_code == status.HTTP_200_OK

# response
if validate_response:
validator = ResponseValidator(
spec, custom_formatters=CUSTOM_FORMATTERS
)
result = validator.validate(request, response)
validator = ResponseValidator(schema_unmarshallers_factory)
result = validator.validate(spec, request, response)
result.raise_for_errors()
9 changes: 1 addition & 8 deletions requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,4 @@ psycopg2-binary~=2.9.0
sqlparse~=0.4.0
python-dateutil~=2.8.0
tblib~=1.7.0
openapi-core~=0.14.2
# FIXME(stephenfin): We have to pin this to prevent a recurrence of [1]. It
# seems openapi-core needs to gain support for OpenAPI 3.1 before we can fix
# this properly [2]
#
# [1] https://github.com/OAI/OpenAPI-Specification/issues/1368
# [2] https://github.com/p1c2u/openapi-core/pull/373
jsonschema<4.0
openapi-core~=0.16.1

0 comments on commit d5d668e

Please sign in to comment.