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
40 changes: 40 additions & 0 deletions ballerina/tests/from_json_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -1752,3 +1752,43 @@ 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"}}});

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<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"}}});

record{|json...;|} recValue2 = check parseAsType(jsonValue, options = {allowDataProjection: {}});
test:assertEquals(recValue2, {"id": "chamil", "values": {"a":2, "b": 45, "c": {"x": "mnb", "y": "uio"}}});

map<anydata> mapValue3 = check parseAsType(jsonValue, options = {allowDataProjection: {}});
test:assertEquals(mapValue3, {"id": "chamil", "values": {"a":2, "b": 45, "c": {"x": "mnb", "y": "uio"}}});

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

map<int> mapValue5 = check parseAsType(jsonValue2, options = {allowDataProjection: false});
test:assertEquals(mapValue5, {id: 1, age: 23});

map<string> 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});
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
}
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