Fix `Couldn't parse: falling back to Raw` for empty JSON array (#6619)

#### Description

fix #6603 

#### Checklist

 - [x] I have updated tests where applicable.
 - [ ] I have added an entry to the CHANGELOG.

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Ha Anh Vu 2024-02-05 17:11:03 +07:00 committed by GitHub
parent 6c7089f7a3
commit 1a02ebb89f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 1 deletions

View File

@ -30,7 +30,12 @@ def is_graphql_query(data):
def is_graphql_batch_query(data):
return isinstance(data, list) and isinstance(data[0], dict) and "query" in data[0]
return (
isinstance(data, list)
and len(data) > 0
and isinstance(data[0], dict)
and "query" in data[0]
)
class ViewGraphQL(base.View):

View File

@ -19,6 +19,7 @@ def test_render_priority():
assert 0 == v.render_priority(
b"""[{"xquery": "query P { \\n }"}]""", content_type="application/json"
)
assert 0 == v.render_priority(b"""[]""", content_type="application/json")
assert 0 == v.render_priority(b"}", content_type="application/json")

View File

@ -78,6 +78,22 @@ def test_format_json():
[("text", " "), ("text", "]"), ("text", "")],
[("text", ""), ("text", "}")],
]
assert list(json.format_json({"list": []})) == [
[("text", "{"), ("text", "")],
[
("text", " "),
("Token_Name_Tag", '"list"'),
("text", ": "),
("text", "[]"),
("text", ""),
],
[("text", ""), ("text", "}")],
]
assert list(json.format_json(None)) == [[("Token_Keyword_Constant", "null")]]
assert list(json.format_json(True)) == [[("Token_Keyword_Constant", "true")]]
assert list(json.format_json(1)) == [[("Token_Literal_Number", "1")]]
assert list(json.format_json("test")) == [[("Token_Literal_String", '"test"')]]
assert list(json.format_json([])) == [[("text", "[]")]]
def test_view_json():
@ -88,6 +104,7 @@ def test_view_json():
assert v(b"[1, 2, 3, 4, 5]")
assert v(b'{"foo" : 3}')
assert v(b'{"foo": true, "nullvalue": null}')
assert v(b"[]")
@given(binary())