From 61e791cac36c5cebad0b48b2df2c176535bf325a Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Mon, 4 Nov 2024 16:29:27 +0530 Subject: [PATCH 1/5] Fix issues with map types when data projection is false --- ballerina/tests/from_json_test.bal | 20 +++++++++++++++++++ .../lib/data/jsondata/json/JsonTraverse.java | 1 + 2 files changed, 21 insertions(+) diff --git a/ballerina/tests/from_json_test.bal b/ballerina/tests/from_json_test.bal index 8be3947..993c0b4 100644 --- a/ballerina/tests/from_json_test.bal +++ b/ballerina/tests/from_json_test.bal @@ -1637,3 +1637,23 @@ function testReadonlyFieldsWithNameAnnotation() returns error? { ReadonlyFieldsRec27 r7 = check parseAsType(user); test:assertEquals(r7, {testId: 4012, testTaxNo: "N/A", testName: "John Doe"}); } + +@test:Config +function testMapAsExpectedTypeWithJsonSource() returns error? { + json jsonValue = {id: "chamil", values: {a: 2, b: 45, c: {x: "mnb", y: "uio"}}}; + + map mapValue = check parseAsType(jsonValue, options = {allowDataProjection: false}); + test:assertEquals(mapValue, {"id":"chamil","values":{"a":2,"b":45,"c":{"x":"mnb","y":"uio"}}}); + + record{|json...;|} recValue = check parseAsType(jsonValue, options = {allowDataProjection: false}); + test:assertEquals(recValue, {"id":"chamil","values":{"a":2,"b":45,"c":{"x":"mnb","y":"uio"}}}); + + map mapValue2 = check parseAsType(jsonValue, options = {allowDataProjection: {}}); + test:assertEquals(mapValue2, {"id":"chamil","values":{"a":2,"b":45,"c":{"x":"mnb","y":"uio"}}}); + + record{|json...;|} recValue2 = check parseAsType(jsonValue, options = {allowDataProjection: {}}); + test:assertEquals(recValue2, {"id":"chamil","values":{"a":2,"b":45,"c":{"x":"mnb","y":"uio"}}}); + + map mapValue3 = check parseAsType(jsonValue, options = {allowDataProjection: {}}); + test:assertEquals(mapValue3, {"id":"chamil","values":{"a":2,"b":45,"c":{"x":"mnb","y":"uio"}}}); +} diff --git a/native/src/main/java/io/ballerina/lib/data/jsondata/json/JsonTraverse.java b/native/src/main/java/io/ballerina/lib/data/jsondata/json/JsonTraverse.java index ff219cf..1c2e59b 100644 --- a/native/src/main/java/io/ballerina/lib/data/jsondata/json/JsonTraverse.java +++ b/native/src/main/java/io/ballerina/lib/data/jsondata/json/JsonTraverse.java @@ -212,6 +212,7 @@ private Object traverseMapValue(BMap map, Object currentJsonNod if (restType.peek() != null) { Type restFieldType = TypeUtils.getReferredType(restType.peek()); addRestField(restFieldType, key, map.get(key), currentJsonNode); + continue; } if (allowDataProjection) { continue; From 3e80cae882625b4498d4602241ee5d4d344c5245 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Mon, 4 Nov 2024 16:53:55 +0530 Subject: [PATCH 2/5] Added test for nlable types with map as expected types --- ballerina/tests/from_json_test.bal | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ballerina/tests/from_json_test.bal b/ballerina/tests/from_json_test.bal index 993c0b4..c46f999 100644 --- a/ballerina/tests/from_json_test.bal +++ b/ballerina/tests/from_json_test.bal @@ -1656,4 +1656,10 @@ function testMapAsExpectedTypeWithJsonSource() returns error? { map mapValue3 = check parseAsType(jsonValue, options = {allowDataProjection: {}}); test:assertEquals(mapValue3, {"id":"chamil","values":{"a":2,"b":45,"c":{"x":"mnb","y":"uio"}}}); + + map? mapValue4 = check parseAsType(jsonValue, options = {allowDataProjection: false}); + test:assertEquals(mapValue4, {"id":"chamil","values":{"a":2,"b":45,"c":{"x":"mnb","y":"uio"}}}); + + record{|json?...;|} recValue3 = check parseAsType(jsonValue, options = {allowDataProjection: false}); + test:assertEquals(recValue3, {"id":"chamil","values":{"a":2,"b":45,"c":{"x":"mnb","y":"uio"}}}); } From 907da11a2710ca5da519003e7920cad082df7733 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Tue, 5 Nov 2024 09:07:51 +0530 Subject: [PATCH 3/5] Update testMapAsExpectedTypeWithJsonSource test function --- ballerina/tests/from_json_test.bal | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/ballerina/tests/from_json_test.bal b/ballerina/tests/from_json_test.bal index b87d9ab..e9eaa45 100644 --- a/ballerina/tests/from_json_test.bal +++ b/ballerina/tests/from_json_test.bal @@ -1756,25 +1756,39 @@ function testReadonlyFieldsWithNameAnnotation() returns error? { @test:Config function testMapAsExpectedTypeWithJsonSource() returns error? { json jsonValue = {id: "chamil", values: {a: 2, b: 45, c: {x: "mnb", y: "uio"}}}; + json jsonValue2 = {id: 1, age: 23}; + json jsonValue3 = {name: "abc", address: "N/A"}; map mapValue = check parseAsType(jsonValue, options = {allowDataProjection: false}); - test:assertEquals(mapValue, {"id":"chamil","values":{"a":2,"b":45,"c":{"x":"mnb","y":"uio"}}}); + test:assertEquals(mapValue, {"id": "chamil", "values": {"a":2, "b": 45, "c": {"x": "mnb", "y": "uio"}}}); record{|json...;|} recValue = check parseAsType(jsonValue, options = {allowDataProjection: false}); - test:assertEquals(recValue, {"id":"chamil","values":{"a":2,"b":45,"c":{"x":"mnb","y":"uio"}}}); + test:assertEquals(recValue, {"id": "chamil", "values": {"a":2, "b": 45, "c": {"x": "mnb", "y": "uio"}}}); map mapValue2 = check parseAsType(jsonValue, options = {allowDataProjection: {}}); - test:assertEquals(mapValue2, {"id":"chamil","values":{"a":2,"b":45,"c":{"x":"mnb","y":"uio"}}}); + test:assertEquals(mapValue2, {"id": "chamil", "values": {"a":2, "b": 45, "c": {"x": "mnb", "y": "uio"}}}); record{|json...;|} recValue2 = check parseAsType(jsonValue, options = {allowDataProjection: {}}); - test:assertEquals(recValue2, {"id":"chamil","values":{"a":2,"b":45,"c":{"x":"mnb","y":"uio"}}}); + test:assertEquals(recValue2, {"id": "chamil", "values": {"a":2, "b": 45, "c": {"x": "mnb", "y": "uio"}}}); map mapValue3 = check parseAsType(jsonValue, options = {allowDataProjection: {}}); - test:assertEquals(mapValue3, {"id":"chamil","values":{"a":2,"b":45,"c":{"x":"mnb","y":"uio"}}}); + test:assertEquals(mapValue3, {"id": "chamil", "values": {"a":2, "b": 45, "c": {"x": "mnb", "y": "uio"}}}); map? mapValue4 = check parseAsType(jsonValue, options = {allowDataProjection: false}); - test:assertEquals(mapValue4, {"id":"chamil","values":{"a":2,"b":45,"c":{"x":"mnb","y":"uio"}}}); + test:assertEquals(mapValue4, {"id": "chamil", "values": {"a":2, "b": 45, "c": {"x": "mnb", "y": "uio"}}}); record{|json?...;|} recValue3 = check parseAsType(jsonValue, options = {allowDataProjection: false}); - test:assertEquals(recValue3, {"id":"chamil","values":{"a":2,"b":45,"c":{"x":"mnb","y":"uio"}}}); + test:assertEquals(recValue3, {"id": "chamil", "values": {"a":2, "b": 45, "c": {"x": "mnb", "y": "uio"}}}); + + map mapValue5 = check parseAsType(jsonValue2, options = {allowDataProjection: false}); + test:assertEquals(mapValue5, {id: 1, age: 23}); + + map mapValue6 = check parseAsType(jsonValue3, options = {allowDataProjection: false}); + test:assertEquals(mapValue6, {name: "abc", address: "N/A"}); + + record{|int...;|} recValue4 = check parseAsType(jsonValue2, options = {allowDataProjection: false}); + test:assertEquals(recValue4, {id: 1, age: 23}); + + record{|string...;|} recValue5 = check parseAsType(jsonValue3, options = {allowDataProjection: false}); + test:assertEquals(recValue5, {name: "abc", address: "N/A"}); } From aa624d1aeee7d01c956c3259756b68615f155d1f Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Tue, 12 Nov 2024 10:10:27 +0530 Subject: [PATCH 4/5] Add union tests for parseAsType function with data projection false --- ballerina/tests/from_json_test.bal | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/ballerina/tests/from_json_test.bal b/ballerina/tests/from_json_test.bal index e9eaa45..565e701 100644 --- a/ballerina/tests/from_json_test.bal +++ b/ballerina/tests/from_json_test.bal @@ -1761,34 +1761,53 @@ function testMapAsExpectedTypeWithJsonSource() returns error? { map mapValue = check parseAsType(jsonValue, options = {allowDataProjection: false}); test:assertEquals(mapValue, {"id": "chamil", "values": {"a":2, "b": 45, "c": {"x": "mnb", "y": "uio"}}}); + test:assertTrue(mapValue is map); record{|json...;|} recValue = check parseAsType(jsonValue, options = {allowDataProjection: false}); test:assertEquals(recValue, {"id": "chamil", "values": {"a":2, "b": 45, "c": {"x": "mnb", "y": "uio"}}}); + test:assertTrue(recValue is record{|json...;|}); map mapValue2 = check parseAsType(jsonValue, options = {allowDataProjection: {}}); test:assertEquals(mapValue2, {"id": "chamil", "values": {"a":2, "b": 45, "c": {"x": "mnb", "y": "uio"}}}); + test:assertTrue(mapValue2 is map); record{|json...;|} recValue2 = check parseAsType(jsonValue, options = {allowDataProjection: {}}); test:assertEquals(recValue2, {"id": "chamil", "values": {"a":2, "b": 45, "c": {"x": "mnb", "y": "uio"}}}); + test:assertTrue(recValue2 is record{|json...;|}); map mapValue3 = check parseAsType(jsonValue, options = {allowDataProjection: {}}); test:assertEquals(mapValue3, {"id": "chamil", "values": {"a":2, "b": 45, "c": {"x": "mnb", "y": "uio"}}}); + test:assertTrue(mapValue3 is map); map? mapValue4 = check parseAsType(jsonValue, options = {allowDataProjection: false}); test:assertEquals(mapValue4, {"id": "chamil", "values": {"a":2, "b": 45, "c": {"x": "mnb", "y": "uio"}}}); + test:assertTrue(mapValue4 is map?); record{|json?...;|} recValue3 = check parseAsType(jsonValue, options = {allowDataProjection: false}); test:assertEquals(recValue3, {"id": "chamil", "values": {"a":2, "b": 45, "c": {"x": "mnb", "y": "uio"}}}); + test:assertTrue(recValue3 is record{|json?...;|}); map mapValue5 = check parseAsType(jsonValue2, options = {allowDataProjection: false}); test:assertEquals(mapValue5, {id: 1, age: 23}); + test:assertTrue(mapValue5 is map); map mapValue6 = check parseAsType(jsonValue3, options = {allowDataProjection: false}); test:assertEquals(mapValue6, {name: "abc", address: "N/A"}); + test:assertTrue(mapValue6 is map); record{|int...;|} recValue4 = check parseAsType(jsonValue2, options = {allowDataProjection: false}); test:assertEquals(recValue4, {id: 1, age: 23}); + test:assertTrue(recValue4 is record{|int...;|}); record{|string...;|} recValue5 = check parseAsType(jsonValue3, options = {allowDataProjection: false}); test:assertEquals(recValue5, {name: "abc", address: "N/A"}); + test:assertTrue(recValue5 is record{|string...;|}); + + map mapValue7 = check parseAsType(jsonValue, options = {allowDataProjection: {}}); + test:assertEquals(mapValue7, {"id": "chamil", "values": {"a":2, "b": 45, "c": {"x": "mnb", "y": "uio"}}}); + test:assertTrue(mapValue7 is map); + + record{|record{|json...;|}|string|int...;|}? mapValue8 = check parseAsType(jsonValue, options = {allowDataProjection: false}); + test:assertEquals(mapValue8, {"id": "chamil", "values": {"a":2, "b": 45, "c": {"x": "mnb", "y": "uio"}}}); + test:assertTrue(mapValue8 is map?); } From 8e98e64d863c7bb9e4a9b942b252e922707aca14 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Wed, 13 Nov 2024 09:01:49 +0530 Subject: [PATCH 5/5] update testMapAsExpectedTypeWithJsonSource tests with aseert type --- ballerina/tests/from_json_test.bal | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ballerina/tests/from_json_test.bal b/ballerina/tests/from_json_test.bal index 565e701..3cf497e 100644 --- a/ballerina/tests/from_json_test.bal +++ b/ballerina/tests/from_json_test.bal @@ -1805,9 +1805,9 @@ function testMapAsExpectedTypeWithJsonSource() returns error? { map mapValue7 = check parseAsType(jsonValue, options = {allowDataProjection: {}}); test:assertEquals(mapValue7, {"id": "chamil", "values": {"a":2, "b": 45, "c": {"x": "mnb", "y": "uio"}}}); - test:assertTrue(mapValue7 is map); + test:assertTrue(mapValue7 is map); record{|record{|json...;|}|string|int...;|}? mapValue8 = check parseAsType(jsonValue, options = {allowDataProjection: false}); test:assertEquals(mapValue8, {"id": "chamil", "values": {"a":2, "b": 45, "c": {"x": "mnb", "y": "uio"}}}); - test:assertTrue(mapValue8 is map?); + test:assertTrue(mapValue8 is record{|record{|json...;|}|string|int...;|}?); }