From ba1bd4bb674272d779fee45cc93ca3f15bc9aa38 Mon Sep 17 00:00:00 2001 From: hfaran Date: Sat, 22 Feb 2014 22:37:41 -0800 Subject: [PATCH] Skip validation and input loading on input_schema of None; resolves #30; Update documentation; Bump version v0.31 --- docs/changelog.rst | 6 ++++++ tests/func_test.py | 2 +- tests/test_tornado_json.py | 2 +- tornado_json/__init__.py | 2 +- tornado_json/schema.py | 13 +++++++------ 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 67a08da..7b6395f 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,6 +5,12 @@ _ --------- +v0.31 - On input schema of ``None``, input is presumed to be ``None`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +* Rather than forcing an input schema of ``None`` with ``GET`` and ``DELETE`` methods, whether input is JSON-decoded or not, is dependent on whether the provided input schema is ``None`` or not. This means that ``get`` and ``delete`` methods can now have request bodies if desired. + + v0.30 - URL Annotations ~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/func_test.py b/tests/func_test.py index 21f7d9b..72dd1bc 100644 --- a/tests/func_test.py +++ b/tests/func_test.py @@ -47,7 +47,7 @@ class ExplodingHandler(requesthandlers.APIHandler): apid = { "get": { - "input_schema": "This doesn't matter because GET request", + "input_schema": None, "output_schema": { "type": "number", }, diff --git a/tests/test_tornado_json.py b/tests/test_tornado_json.py index bc2030e..e855bab 100644 --- a/tests/test_tornado_json.py +++ b/tests/test_tornado_json.py @@ -103,7 +103,7 @@ class TerribleHandler(MockRequestHandler): apid = { "get": { - "input_schema": "This doesn't matter because GET request", + "input_schema": None, "output_schema": { "type": "number", }, diff --git a/tornado_json/__init__.py b/tornado_json/__init__.py index f3b7f24..d6534de 100644 --- a/tornado_json/__init__.py +++ b/tornado_json/__init__.py @@ -5,4 +5,4 @@ # Alternatively, just put the version in a text file or something to avoid # this. -__version__ = '0.30' +__version__ = '0.31' diff --git a/tornado_json/schema.py b/tornado_json/schema.py index 3ecc7c4..562296b 100644 --- a/tornado_json/schema.py +++ b/tornado_json/schema.py @@ -30,10 +30,12 @@ def _wrapper(self, *args, **kwargs): # Get name of method method_name = rh_method.__name__ - # Special case for GET, DELETE requests (since there is no data to - # validate) - if method_name not in ["get", "delete"]: - # If input is not valid JSON, fail + input_schema = type(self).apid[method_name]["input_schema"] + # In case the specified input_schema is ``None``, we + # don't json.loads the input, but just set it to ``None`` + # instead. + if input_schema is not None: + # Attempt to json.loads the input try: # TODO: Assuming UTF-8 encoding for all requests, # find a nice way of determining this from charset @@ -44,11 +46,10 @@ def _wrapper(self, *args, **kwargs): raise jsonschema.ValidationError( "Input is malformed; could not decode JSON object." ) - # Validate the received input jsonschema.validate( input_, - type(self).apid[method_name]["input_schema"] + input_schema ) else: input_ = None