Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
rename from serialization/deserializers to content_types
  • Loading branch information
dtkav committed Dec 12, 2019
commit fd89862255829b5285d5c89e473b5a423f2d9310
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@

from jsonschema import ValidationError

from ..exceptions import ExtraParameterProblem, BadRequestProblem
from ..problem import problem
from ..types import coerce_type
from ..utils import is_null
from .exceptions import ExtraParameterProblem, BadRequestProblem
from .types import coerce_type
from .utils import is_null

logger = logging.getLogger('connexion.serialization.deserializers')
logger = logging.getLogger('connexion.content_types')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't we just do logging.getLogger(__name__) here (and everywhere else)? That's what I do in my other projects and it prevents pointless work when refactoring.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I think I was planning to stick to repo convention for now, and then doing a single PR to fix them all. Happy to do it incrementally though.



class ContentHandler(object):
Expand Down Expand Up @@ -120,7 +119,7 @@ class MultiPartFormDataContentHandler(FormDataContentHandler):
)


DEFAULT_DESERIALIZERS = (
KNOWN_CONTENT_TYPES = (
StreamingContentHandler,
JSONContentHandler,
FormDataContentHandler,
Expand Down
12 changes: 8 additions & 4 deletions connexion/decorators/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from ..exceptions import ExtraParameterProblem, BadRequestProblem, UnsupportedMediaTypeProblem
from ..http_facts import FORM_CONTENT_TYPES
from ..json_schema import Draft4RequestValidator, Draft4ResponseValidator
from ..serialization.deserializers import DEFAULT_DESERIALIZERS
from ..content_types import KNOWN_CONTENT_TYPES
from ..types import TypeValidationError, coerce_type
from ..utils import all_json, boolean, is_json_mimetype, is_null, is_nullable

Expand Down Expand Up @@ -56,7 +56,7 @@ def __init__(self, schema, consumes, api, is_null_value_valid=False, validator=N
de(self.validator,
self.schema,
self.strict_validation,
self.is_null_value_valid) for de in DEFAULT_DESERIALIZERS
self.is_null_value_valid) for de in KNOWN_CONTENT_TYPES
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.is_null_value_valid) for de in KNOWN_CONTENT_TYPES
self.is_null_value_valid) for de in ContentHandler.__subclasses__()

that way all you have to do to register a new one is subclass ContentHandler and make sure to import your ContentHandler subclass before initializing Connexion (or more accurately before whatever point it is that creates the RequestBodyValidator).

Actually, I think I would turn this into a classmethod on ContentHandler so that it can look for grandchildren not just children classes as described here: https://stackoverflow.com/a/3862957

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. I see register_content_handler below for registering additional content types. I would still like to see a way to replace default content handlers.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL!

]

def register_content_handler(self, cv):
Expand All @@ -69,7 +69,8 @@ def register_content_handler(self, cv):
def lookup_content_handler(self, request):
matches = [
v for v in self._content_handlers
if v.regex.match(request.content_type)
if request.content_type is not None and
v.regex.match(request.content_type)
]
if len(matches) > 1:
logger.warning("Content could be handled by multiple validators")
Expand All @@ -85,7 +86,10 @@ def __call__(self, function):
@functools.wraps(function)
def wrapper(request):
content_handler = self.lookup_content_handler(request)
exact_match = request.content_type in self.consumes
exact_match = (
request.content_type is not None and
request.content_type in self.consumes
)
partial_match = content_handler and content_handler.name in self.consumes
if not (exact_match or partial_match):
raise UnsupportedMediaTypeProblem(
Expand Down
Empty file.