| 1 | """Hacks needed to circumvent connexion validation
|
|---|
| 2 |
|
|---|
| 3 | There is a bug on the connexion library concerning content negotiation and
|
|---|
| 4 | response validation. See https://github.com/zalando/connexion/issues/860 and
|
|---|
| 5 | https://github.com/zalando/connexion/pull/760
|
|---|
| 6 |
|
|---|
| 7 | Until this issue is fixed, we need to find a way to avoid a false validation
|
|---|
| 8 | error when a requests sends an 'application/geo+json' accept header
|
|---|
| 9 | """
|
|---|
| 10 | import functools
|
|---|
| 11 | import logging
|
|---|
| 12 |
|
|---|
| 13 | from connexion.decorators.response import ResponseValidator
|
|---|
| 14 | from connexion import problem
|
|---|
| 15 | from connexion.exceptions import NonConformingResponseBody, NonConformingResponseHeaders
|
|---|
| 16 | from connexion.utils import has_coroutine
|
|---|
| 17 |
|
|---|
| 18 | logger = logging.getLogger(__name__)
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 | class ValidatorWorkaround(ResponseValidator):
|
|---|
| 22 |
|
|---|
| 23 | def validate_response_with_request(self, request, data, status_code, headers, url):
|
|---|
| 24 | if request.headers.get('accept', '') == 'application/geo+json':
|
|---|
| 25 | logging.debug('Circumventing validation for application/geo+json')
|
|---|
| 26 | return True
|
|---|
| 27 | return self.validate_response(data, status_code, headers, url)
|
|---|
| 28 |
|
|---|
| 29 | def __call__(self, function):
|
|---|
| 30 | """
|
|---|
| 31 | :type function: types.FunctionType
|
|---|
| 32 | :rtype: types.FunctionType
|
|---|
| 33 | """
|
|---|
| 34 | def _wrapper(request, response):
|
|---|
| 35 | connexion_response = \
|
|---|
| 36 | self.operation.api.get_connexion_response(response, self.mimetype)
|
|---|
| 37 | self.validate_response_with_request(
|
|---|
| 38 | request,
|
|---|
| 39 | connexion_response.body, connexion_response.status_code,
|
|---|
| 40 | connexion_response.headers, request.url)
|
|---|
| 41 |
|
|---|
| 42 | return response
|
|---|
| 43 |
|
|---|
| 44 | if has_coroutine(function):
|
|---|
| 45 | from .coroutine_wrappers import get_response_validator_wrapper
|
|---|
| 46 | wrapper = get_response_validator_wrapper(function, _wrapper)
|
|---|
| 47 |
|
|---|
| 48 | else: # pragma: 3 no cover
|
|---|
| 49 | @functools.wraps(function)
|
|---|
| 50 | def wrapper(request):
|
|---|
| 51 | response = function(request)
|
|---|
| 52 | return _wrapper(request, response)
|
|---|
| 53 |
|
|---|
| 54 | return wrapper
|
|---|