1
0
mirror of https://github.com/ansible/awx.git synced 2024-10-31 15:21:13 +03:00

Merge pull request #6121 from jangsutsr/6118_allow_drf_parser_to_accept_trailing_commas

Allow DRF parser to accept trailing commas
This commit is contained in:
Aaron Tan 2017-04-28 09:37:23 -04:00 committed by GitHub
commit 209acc3278
2 changed files with 36 additions and 1 deletions

View File

@ -12,6 +12,26 @@ from rest_framework import parsers
from rest_framework.exceptions import ParseError from rest_framework.exceptions import ParseError
def _remove_trailing_commas(data):
left = 0
right = 0
in_string = False
ret = []
while left != len(data):
if data[left] == ',' and not in_string:
while right != len(data) and data[right] in ',\n\t\r ':
right += 1
if right == len(data) or data[right] not in '}]':
ret.append(',')
else:
if data[left] == '"' and (left - 1 >= 0 and data[left - 1] != '\\'):
in_string = not in_string
ret.append(data[left])
right += 1
left = right
return ''.join(ret)
class JSONParser(parsers.JSONParser): class JSONParser(parsers.JSONParser):
""" """
Parses JSON-serialized data, preserving order of dictionary keys. Parses JSON-serialized data, preserving order of dictionary keys.
@ -25,7 +45,7 @@ class JSONParser(parsers.JSONParser):
encoding = parser_context.get('encoding', settings.DEFAULT_CHARSET) encoding = parser_context.get('encoding', settings.DEFAULT_CHARSET)
try: try:
data = stream.read().decode(encoding) data = _remove_trailing_commas(stream.read().decode(encoding))
obj = json.loads(data, object_pairs_hook=OrderedDict) obj = json.loads(data, object_pairs_hook=OrderedDict)
if not isinstance(obj, dict): if not isinstance(obj, dict):
raise ParseError(_('JSON parse error - not a JSON object')) raise ParseError(_('JSON parse error - not a JSON object'))

View File

@ -0,0 +1,15 @@
import pytest
from awx.api.parsers import _remove_trailing_commas
@pytest.mark.parametrize('input_, output', [
('{"foo": "bar"}', '{"foo": "bar"}'),
('{"foo": "bar",\n\t\r }', '{"foo": "bar"}'),
('{"foo": ["alice", "bob"]}', '{"foo": ["alice","bob"]}'),
('{"foo": ["alice", "bob",\n\t\r ]}', '{"foo": ["alice","bob"]}'),
('{"foo": "\\"bar,\n\t\r }"}', '{"foo": "\\"bar,\n\t\r }"}'),
('{"foo": ["\\"alice,\n\t\r ]", "bob"]}', '{"foo": ["\\"alice,\n\t\r ]","bob"]}'),
])
def test_remove_trailing_commas(input_, output):
assert _remove_trailing_commas(input_) == output