| [4d83fdc] | 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):
|
|---|
| [7be94aa] | 22 | def validate_response_with_request(
|
|---|
| 23 | self, request, data, status_code, headers, url
|
|---|
| 24 | ):
|
|---|
| 25 | if request.headers.get('accept', '') == 'application/geo+json':
|
|---|
| 26 | logging.debug('Circumventing validation for application/geo+json')
|
|---|
| 27 | return True
|
|---|
| 28 | return self.validate_response(data, status_code, headers, url)
|
|---|
| 29 |
|
|---|
| 30 | def __call__(self, function):
|
|---|
| 31 | """
|
|---|
| [ec9e811] | 32 | :type function: types.FunctionType
|
|---|
| 33 | :rtype: types.FunctionType
|
|---|
| 34 | """
|
|---|
| [7be94aa] | 35 | def _wrapper(request, response):
|
|---|
| 36 | connexion_response = \
|
|---|
| 37 | self.operation.api.get_connexion_response(response, self.mimetype)
|
|---|
| 38 | self.validate_response_with_request(
|
|---|
| 39 | request, connexion_response.body,
|
|---|
| 40 | connexion_response.status_code, connexion_response.headers,
|
|---|
| 41 | request.url
|
|---|
| 42 | )
|
|---|
| 43 |
|
|---|
| 44 | return response
|
|---|
| 45 |
|
|---|
| 46 | if has_coroutine(function):
|
|---|
| 47 | from .coroutine_wrappers import get_response_validator_wrapper
|
|---|
| 48 | wrapper = get_response_validator_wrapper(function, _wrapper)
|
|---|
| 49 |
|
|---|
| 50 | else: # pragma: 3 no cover
|
|---|
| 51 |
|
|---|
| 52 | @functools.wraps(function)
|
|---|
| 53 | def wrapper(request):
|
|---|
| 54 | response = function(request)
|
|---|
| 55 | return _wrapper(request, response)
|
|---|
| 56 |
|
|---|
| 57 | return wrapper
|
|---|