From f8d1ab946205440ed3c44511ef42e5ad13fb9e5e Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Sun, 24 Jul 2022 16:17:03 -0400 Subject: JSON schema -- accept single item in place of array When the schema wants a variable-length array, allow a single item as well as allowing an array. --- libtests/json.cc | 20 +++++++++++--------- libtests/libtests.testcov | 2 +- libtests/qtest/json/json.out | 3 ++- 3 files changed, 14 insertions(+), 11 deletions(-) (limited to 'libtests') diff --git a/libtests/json.cc b/libtests/json.cc index 9c5720f4..a509c2be 100644 --- a/libtests/json.cc +++ b/libtests/json.cc @@ -162,7 +162,9 @@ test_schema() "x": "ecks" }, "s": [ - "esses" + { + "ss": "esses" + } ] } }, @@ -236,6 +238,8 @@ test_schema() JSON bad_schema = JSON::parse(R"({"a": true, "b": "potato?"})"); check_schema(bad_schema, bad_schema, 0, false, "bad schema field type"); + // "two" exercises the case of the JSON containing a single + // element where the schema has an array. JSON good = JSON::parse(R"( { "one": { @@ -245,17 +249,15 @@ test_schema() "x": [1, null] }, "s": [ - null, - "anything" + {"ss": null}, + {"ss": "anything"} ] } }, - "two": [ - { - "glarp": "enspliel", - "goose": 3.14 - } - ], + "two": { + "glarp": "enspliel", + "goose": 3.14 + }, "three": { "": { "z": "ebra" diff --git a/libtests/libtests.testcov b/libtests/libtests.testcov index 21def9f3..7f71d8e0 100644 --- a/libtests/libtests.testcov +++ b/libtests/libtests.testcov @@ -36,7 +36,6 @@ Pl_PNGFilter decodePaeth 0 Pl_TIFFPredictor processRow 1 JSON wanted dictionary 0 JSON key missing in object 0 -JSON wanted array 0 JSON schema array error 0 JSON key extra in object 0 QPDFArgParser read args from stdin 0 @@ -93,3 +92,4 @@ JSON 16 high high 0 JSON 16 low not after high 0 JSON 16 dangling high 0 JSON parse duplicate key 0 +JSON schema array for single item 0 diff --git a/libtests/qtest/json/json.out b/libtests/qtest/json/json.out index c7cb85a7..811f8aed 100644 --- a/libtests/qtest/json/json.out +++ b/libtests/qtest/json/json.out @@ -4,7 +4,8 @@ top-level object is supposed to be a dictionary --- missing items json key ".one.a": key "q" is present in schema but missing in object json key ".one.a.r" is supposed to be a dictionary -json key ".one.a.s" is supposed to be an array +json key ".one.a.s": key "ss" is present in schema but missing in object +json key ".one.a.s": key "z" is not present in schema but appears in object json key ".one.a": key "t" is not present in schema but appears in object json key ".three.anything": key "z" is present in schema but missing in object json key ".three.anything": key "x" is not present in schema but appears in object -- cgit v1.2.3-54-g00ecf