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

Fix invalid compile time errors when expected type is map and data projection is false #49

Merged
merged 9 commits into from
Nov 13, 2024
59 changes: 59 additions & 0 deletions ballerina/tests/from_json_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -1752,3 +1752,62 @@ 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"}}};
json jsonValue2 = {id: 1, age: 23};
json jsonValue3 = {name: "abc", address: "N/A"};

map<json> 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<json>);

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<json> mapValue2 = check parseAsType(jsonValue, options = {allowDataProjection: {}});
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved
test:assertEquals(mapValue2, {"id": "chamil", "values": {"a":2, "b": 45, "c": {"x": "mnb", "y": "uio"}}});
test:assertTrue(mapValue2 is map<json>);

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<anydata> 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<anydata>);

map<json>? 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<json>?);

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<int> mapValue5 = check parseAsType(jsonValue2, options = {allowDataProjection: false});
test:assertEquals(mapValue5, {id: 1, age: 23});
test:assertTrue(mapValue5 is map<int>);

map<string> mapValue6 = check parseAsType(jsonValue3, options = {allowDataProjection: false});
test:assertEquals(mapValue6, {name: "abc", address: "N/A"});
test:assertTrue(mapValue6 is map<string>);

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});
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved
test:assertEquals(recValue5, {name: "abc", address: "N/A"});
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved
test:assertTrue(recValue5 is record{|string...;|});

map<record{|json...;|}|string|int> 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<anydata>);
SasinduDilshara marked this conversation as resolved.
Show resolved Hide resolved

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<json>?);
}
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ private Object traverseMapValue(BMap<BString, Object> map, Object currentJsonNod
if (restType.peek() != null) {
Type restFieldType = TypeUtils.getReferredType(restType.peek());
addRestField(restFieldType, key, map.get(key), currentJsonNode);
continue;
}
if (allowDataProjection) {
continue;
Expand Down
Loading