mirror of
https://github.com/ansible/awx.git
synced 2024-10-31 06:51:10 +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:
commit
209acc3278
@ -12,6 +12,26 @@ from rest_framework import parsers
|
||||
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):
|
||||
"""
|
||||
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)
|
||||
|
||||
try:
|
||||
data = stream.read().decode(encoding)
|
||||
data = _remove_trailing_commas(stream.read().decode(encoding))
|
||||
obj = json.loads(data, object_pairs_hook=OrderedDict)
|
||||
if not isinstance(obj, dict):
|
||||
raise ParseError(_('JSON parse error - not a JSON object'))
|
||||
|
15
awx/main/tests/unit/api/test_parsers.py
Normal file
15
awx/main/tests/unit/api/test_parsers.py
Normal 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
|
Loading…
Reference in New Issue
Block a user