Skip to content

Commit

Permalink
Changes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
31z4 committed Sep 29, 2016
1 parent 1fb776e commit e01171a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 27 deletions.
4 changes: 2 additions & 2 deletions connexion/decorators/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import six
import werkzeug.exceptions as exceptions

from ..utils import boolean, is_null, is_nullable
from ..utils import boolean, is_null, is_nullable, all_json

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -89,7 +89,7 @@ def parameter_to_arg(parameters, consumes, function):
def wrapper(*args, **kwargs):
logger.debug('Function Arguments: %s', arguments)

if 'application/json' in consumes:
if all_json(consumes):
try:
request_body = flask.request.json
except exceptions.BadRequest:
Expand Down
4 changes: 2 additions & 2 deletions connexion/decorators/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from ..exceptions import (NonConformingResponseBody,
NonConformingResponseHeaders)
from ..problem import problem
from ..utils import produces_json
from ..utils import all_json
from .decorator import BaseDecorator
from .validation import ResponseBodyValidator

Expand Down Expand Up @@ -91,7 +91,7 @@ def is_json_schema_compatible(self, response_definition):
if not response_definition:
return False
return ('schema' in response_definition and
(produces_json([self.mimetype]) or self.mimetype == 'text/plain'))
(all_json([self.mimetype]) or self.mimetype == 'text/plain'))

def __call__(self, function):
"""
Expand Down
16 changes: 7 additions & 9 deletions connexion/decorators/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from werkzeug import FileStorage

from ..problem import problem
from ..utils import boolean, is_null, is_nullable
from ..utils import boolean, is_null, is_nullable, all_json

logger = logging.getLogger('connexion.decorators.validation')

Expand Down Expand Up @@ -116,15 +116,13 @@ def __call__(self, function):

@functools.wraps(function)
def wrapper(*args, **kwargs):
if 'application/json' not in self.consumes:
return function(*args, **kwargs)
if all_json(self.consumes):
data = flask.request.json

data = flask.request.json

logger.debug("%s validating schema...", flask.request.url)
error = self.validate_schema(data)
if error and not self.has_default:
return error
logger.debug("%s validating schema...", flask.request.url)
error = self.validate_schema(data)
if error and not self.has_default:
return error

response = function(*args, **kwargs)
return response
Expand Down
6 changes: 3 additions & 3 deletions connexion/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from .decorators.validation import (ParameterValidator, RequestBodyValidator,
TypeValidationError)
from .exceptions import InvalidSpecification
from .utils import flaskify_endpoint, is_nullable, produces_json
from .utils import flaskify_endpoint, is_nullable, all_json

logger = logging.getLogger('connexion.operation')

Expand Down Expand Up @@ -279,7 +279,7 @@ def get_mimetype(self):
:rtype str
"""
if produces_json(self.produces):
if all_json(self.produces):
try:
return self.produces[0]
except IndexError:
Expand Down Expand Up @@ -374,7 +374,7 @@ def __content_type_decorator(self):
logger.debug('... Produces: %s', self.produces, extra=vars(self))

mimetype = self.get_mimetype()
if produces_json(self.produces): # endpoint will return json
if all_json(self.produces): # endpoint will return json
logger.debug('... Produces json', extra=vars(self))
jsonify = Jsonifier(mimetype)
return jsonify
Expand Down
22 changes: 11 additions & 11 deletions connexion/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,29 +144,29 @@ def is_json_mimetype(mimetype):
return maintype == 'application' and (subtype == 'json' or subtype.endswith('+json'))


def produces_json(produces):
def all_json(mimetypes):
"""
Returns True if all mimetypes in produces are serialized with json
Returns True if all mimetypes are serialized with json
:type produces: list
:type mimetypes: list
:rtype: bool
>>> produces_json(['application/json'])
>>> all_json(['application/json'])
True
>>> produces_json(['application/x.custom+json'])
>>> all_json(['application/x.custom+json'])
True
>>> produces_json([])
>>> all_json([])
True
>>> produces_json(['application/xml'])
>>> all_json(['application/xml'])
False
>>> produces_json(['text/json'])
>>> all_json(['text/json'])
False
>>> produces_json(['application/json', 'other/type'])
>>> all_json(['application/json', 'other/type'])
False
>>> produces_json(['application/json', 'application/x.custom+json'])
>>> all_json(['application/json', 'application/x.custom+json'])
True
"""
return all(is_json_mimetype(mimetype) for mimetype in produces)
return all(is_json_mimetype(mimetype) for mimetype in mimetypes)


def boolean(s):
Expand Down

0 comments on commit e01171a

Please sign in to comment.