From 91843d7f131521673e3ae2e1a1bff8c180519b1c Mon Sep 17 00:00:00 2001 From: Ahson Khan Date: Thu, 11 Feb 2021 13:46:35 -0800 Subject: [PATCH 1/2] Make sure az_json_writer_append_json_text appends a comma between elements of an array. --- sdk/src/azure/core/az_json_writer.c | 6 +++ sdk/tests/core/test_az_json.c | 62 +++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/sdk/src/azure/core/az_json_writer.c b/sdk/src/azure/core/az_json_writer.c index b59d7b3307..59fcd656ad 100644 --- a/sdk/src/azure/core/az_json_writer.c +++ b/sdk/src/azure/core/az_json_writer.c @@ -783,6 +783,12 @@ az_json_writer_append_json_text(az_json_writer* ref_json_writer, az_span json_te az_span remaining_json = _get_remaining_span(ref_json_writer, _az_MINIMUM_STRING_CHUNK_SIZE); _az_RETURN_IF_NOT_ENOUGH_SIZE(remaining_json, _az_MINIMUM_STRING_CHUNK_SIZE); + if (ref_json_writer->_internal.need_comma) + { + remaining_json = az_span_copy_u8(remaining_json, ','); + ref_json_writer->_internal.bytes_written++; + } + _az_RETURN_IF_FAILED( az_json_writer_span_copy_chunked(ref_json_writer, &remaining_json, json_text)); diff --git a/sdk/tests/core/test_az_json.c b/sdk/tests/core/test_az_json.c index 04b489f7ba..f682c7f7f4 100644 --- a/sdk/tests/core/test_az_json.c +++ b/sdk/tests/core/test_az_json.c @@ -276,6 +276,53 @@ static void test_json_writer_append_nested(void** state) "\"foo\":[\"bar\",null,0,-12,12,9007199254740991]" "}"); } + + { + TEST_EXPECT_SUCCESS(az_json_writer_init(&writer, AZ_SPAN_FROM_BUFFER(array), NULL)); + TEST_EXPECT_SUCCESS(az_json_writer_append_begin_array(&writer)); + TEST_EXPECT_SUCCESS(az_json_writer_append_json_text(&writer, AZ_SPAN_FROM_STR("{\"a\": 1}"))); + TEST_EXPECT_SUCCESS(az_json_writer_append_json_text(&writer, AZ_SPAN_FROM_STR("{\"b\": 2}"))); + TEST_EXPECT_SUCCESS(az_json_writer_append_end_array(&writer)); + + az_span_to_str((char*)array, 200, az_json_writer_get_bytes_used_in_destination(&writer)); + assert_string_equal( + array, + "[" + "{\"a\": 1}," + "{\"b\": 2}" + "]"); + } + + { + TEST_EXPECT_SUCCESS(az_json_writer_init(&writer, AZ_SPAN_FROM_BUFFER(array), NULL)); + TEST_EXPECT_SUCCESS(az_json_writer_append_begin_object(&writer)); + TEST_EXPECT_SUCCESS(az_json_writer_append_property_name(&writer, AZ_SPAN_FROM_STR("foo"))); + TEST_EXPECT_SUCCESS(az_json_writer_append_json_text(&writer, AZ_SPAN_FROM_STR("[]"))); + TEST_EXPECT_SUCCESS(az_json_writer_append_property_name(&writer, AZ_SPAN_FROM_STR("bar"))); + TEST_EXPECT_SUCCESS(az_json_writer_append_json_text(&writer, AZ_SPAN_FROM_STR("[1]"))); + TEST_EXPECT_SUCCESS(az_json_writer_append_end_object(&writer)); + + az_span_to_str((char*)array, 200, az_json_writer_get_bytes_used_in_destination(&writer)); + assert_string_equal( + array, + "{" + "\"foo\":[]," + "\"bar\":[1]" + "}"); + } + + { + TEST_EXPECT_SUCCESS(az_json_writer_init(&writer, AZ_SPAN_FROM_BUFFER(array), NULL)); + TEST_EXPECT_SUCCESS(az_json_writer_append_begin_array(&writer)); + TEST_EXPECT_SUCCESS(az_json_writer_append_int32(&writer, 42)); + TEST_EXPECT_SUCCESS(az_json_writer_append_json_text(&writer, AZ_SPAN_FROM_STR("\"a\" "))); + TEST_EXPECT_SUCCESS(az_json_writer_append_json_text(&writer, AZ_SPAN_FROM_STR("\"b\""))); + TEST_EXPECT_SUCCESS(az_json_writer_append_int32(&writer, 24)); + TEST_EXPECT_SUCCESS(az_json_writer_append_end_array(&writer)); + + az_span_to_str((char*)array, 200, az_json_writer_get_bytes_used_in_destination(&writer)); + assert_string_equal(array, "[42,\"a\" ,\"b\",24]"); + } } } @@ -316,6 +363,14 @@ static void test_json_writer_append_nested_invalid(void** state) az_json_writer_append_json_text(&writer, AZ_SPAN_FROM_STR("fals ")), AZ_ERROR_UNEXPECTED_CHAR); } + { + TEST_EXPECT_SUCCESS(az_json_writer_init(&writer, AZ_SPAN_FROM_BUFFER(array), NULL)); + TEST_EXPECT_SUCCESS(az_json_writer_append_begin_array(&writer)); + assert_int_equal( + az_json_writer_append_json_text(&writer, AZ_SPAN_FROM_STR("{\"a\": 1},")), + AZ_ERROR_UNEXPECTED_CHAR); + TEST_EXPECT_SUCCESS(az_json_writer_append_json_text(&writer, AZ_SPAN_FROM_STR("{\"b\": 2}"))); + } { TEST_EXPECT_SUCCESS(az_json_writer_init(&writer, AZ_SPAN_FROM_BUFFER(array), NULL)); assert_int_equal( @@ -334,6 +389,13 @@ static void test_json_writer_append_nested_invalid(void** state) az_json_writer_append_json_text(&writer, AZ_SPAN_FROM_STR("{\"name\": ")), AZ_ERROR_UNEXPECTED_END); } + { + TEST_EXPECT_SUCCESS(az_json_writer_init(&writer, AZ_SPAN_FROM_BUFFER(array), NULL)); + TEST_EXPECT_SUCCESS(az_json_writer_append_json_text(&writer, AZ_SPAN_FROM_STR("1"))); + assert_int_equal( + az_json_writer_append_json_text(&writer, AZ_SPAN_FROM_STR("2")), + AZ_ERROR_JSON_INVALID_STATE); + } { TEST_EXPECT_SUCCESS(az_json_writer_init(&writer, AZ_SPAN_FROM_BUFFER(array), NULL)); TEST_EXPECT_SUCCESS(az_json_writer_append_begin_object(&writer)); From d11b242a909e87ea13f3513c43430d1eff2a51ce Mon Sep 17 00:00:00 2001 From: Ahson Khan Date: Thu, 11 Feb 2021 13:52:36 -0800 Subject: [PATCH 2/2] update changelog entry. --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 04c7699cf8..b3d2261dcc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,10 @@ # Release History -## 1.1.0-beta.2 (Unreleased) +## 1.1.0-beta.4 (Unreleased) +### Bug Fixes + +- [[#1600]](https://github.com/Azure/azure-sdk-for-c/pull/1600) Make sure `az_json_writer_append_json_text()` appends a comma between elements of a JSON array. ## 1.1.0-beta.1 (2020-10-06)