Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIXED] Handling of JSON empty arrays #590

Merged
merged 1 commit into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,13 @@ _jsonGetArray(char **ptr, nats_JSONArray **newArray, int nested)
{
p = _jsonTrimSpace(p);

if ((typ == TYPE_NOT_SET) && (*p == ']'))
{
array.typ = TYPE_NULL;
end = true;
break;
}

// Initialize the field before parsing.
memset(&field, 0, sizeof(nats_JSONField));

Expand Down Expand Up @@ -1454,6 +1461,12 @@ nats_JSONGetArrayField(nats_JSON *json, const char *fieldName, int fieldType, na
return nats_setError(NATS_INVALID_ARG,
"Field '%s' is not an array, it has type: %d",
field->name, field->typ);
// If empty array, return NULL/OK
if (field->value.varr->typ == TYPE_NULL)
{
*retField = NULL;
return NATS_OK;
}
if (fieldType != field->value.varr->typ)
return nats_setError(NATS_INVALID_ARG,
"Asked for field '%s' as an array of type: %d, but it is an array of type: %d",
Expand Down
42 changes: 42 additions & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3198,6 +3198,7 @@ test_natsJSON(void)
"{ \"test\": [\"abc,def\"]}",
"{ \"test\": [{\"a\": 1}, {\"b\": \"c\"}]}",
"{ \"test\": [[{\"a\": 1}], [{\"b\": \"c\"}]]}",
"{ \"test\": []}",
"{ \"test\": {\"inner\":\"a\",\"inner2\":2,\"inner3\":false,\"inner4\":{\"inner_inner1\" : 1.234}}}",
"{ \"test\": \"a\\\"b\\\"c\"}",
"{ \"test\": \"\\\"\\\\/\b\f\n\r\t\\uabcd\"}",
Expand Down Expand Up @@ -4113,6 +4114,47 @@ test_natsJSON(void)
&& (strstr(nats_GetLastError(NULL), "on purpose")));
nats_clearLastError();
nats_JSONDestroy(json);
json = NULL;

test("Parse empty array: ");
s = nats_JSONParse(&json, "{\"empty\":[]}", -1);
testCond(s == NATS_OK);

test("Get empty array array: ");
s = nats_JSONGetArrayArray(json, "empty", &arrArrVal, &arrCount);
testCond((s == NATS_OK) && (arrArrVal == NULL) && (arrCount == 0));

test("Get empty obj array: ");
s = nats_JSONGetArrayObject(json, "empty", &arrObjVal, &arrCount);
testCond((s == NATS_OK) && (arrObjVal == NULL) && (arrCount == 0));

test("Get empty ulong array: ");
s = nats_JSONGetArrayULong(json, "empty", &arrULongVal, &arrCount);
testCond((s == NATS_OK) && (arrULongVal == NULL) && (arrCount == 0));

test("Get empty long array: ");
s = nats_JSONGetArrayLong(json, "empty", &arrLongVal, &arrCount);
testCond((s == NATS_OK) && (arrLongVal == NULL) && (arrCount == 0));

test("Get empty int array: ");
s = nats_JSONGetArrayInt(json, "empty", &arrIntVal, &arrCount);
testCond((s == NATS_OK) && (arrIntVal == NULL) && (arrCount == 0));

test("Get empty double array: ");
s = nats_JSONGetArrayDouble(json, "empty", &arrDoubleVal, &arrCount);
testCond((s == NATS_OK) && (arrDoubleVal == NULL) && (arrCount == 0));

test("Get empty bool array: ");
s = nats_JSONGetArrayBool(json, "empty", &arrBoolVal, &arrCount);
testCond((s == NATS_OK) && (arrBoolVal == NULL) && (arrCount == 0));

test("Get empty string array: ");
s = nats_JSONGetArrayStr(json, "empty", &arrVal, &arrCount);
testCond((s == NATS_OK) && (arrVal == NULL) && (arrCount == 0));

nats_JSONDestroy(json);
json = NULL;

}

static void
Expand Down