Skip to content

Commit

Permalink
Skip validation and input loading on input_schema of None; resolves #30
Browse files Browse the repository at this point in the history
…; Update documentation; Bump version v0.31
  • Loading branch information
hfaran committed Feb 23, 2014
1 parent 706bc24 commit ba1bd4b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 9 deletions.
6 changes: 6 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
2 changes: 1 addition & 1 deletion tests/func_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand Down
2 changes: 1 addition & 1 deletion tests/test_tornado_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand Down
2 changes: 1 addition & 1 deletion tornado_json/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# Alternatively, just put the version in a text file or something to avoid
# this.

__version__ = '0.30'
__version__ = '0.31'
13 changes: 7 additions & 6 deletions tornado_json/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit ba1bd4b

Please sign in to comment.