Skip to content

Commit

Permalink
Merge pull request #54 from MaryamZi/fix-7362
Browse files Browse the repository at this point in the history
Fix toJson failing with repeated simple value members
  • Loading branch information
MaryamZi authored Nov 14, 2024
2 parents 94297e4 + adcdadd commit 06e613a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
54 changes: 54 additions & 0 deletions ballerina/tests/to_json_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,60 @@ function testToJsonWithCyclicValues() {
test:assertEquals("the value has a cyclic reference", r4Err.message());
}

@test:Config
function testToJsonWithoutCyclicValuesWithRepeatedSimpleValueMembers() {
byte byteVal = 3;
json jsonVal = {
"a": "abc",
"b": 1,
"c": true,
"d": null,
"e": 1,
"f": null,
"g": true,
"h": byteVal,
"i": 2f,
"j": 2f,
"k": "non-dup",
"l": 3d,
"m": byteVal,
"n": 3d,
"o": "abc",
"p": false
};
json jsonRes = toJson(jsonVal);
test:assertEquals(jsonVal, jsonRes);
test:assertNotExactEquals(jsonVal, jsonRes);
}

@test:Config
function testToJsonWithCyclicValuesWithOtherSimpleValueMembers() {
byte byteVal = 3;
map<json> jsonVal = {
"a": "abc",
"b": 1,
"c": true,
"d": null,
"e": 1,
"f": null,
"g": true,
"h": byteVal,
"i": 2f,
"j": 2f,
"k": "non-dup",
"l": 3d,
"m": byteVal,
"n": 3d,
"o": "abc",
"p": false
};
jsonVal["q"] = jsonVal;
json|error r1 = trap toJsonWithCyclicValues(jsonVal);
test:assertTrue(r1 is error);
error r1Err = <error> r1;
test:assertEquals("the value has a cyclic reference", r1Err.message());
}

function toJsonWithCyclicValues(anydata val) returns json {
return toJson(val);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.ballerina.runtime.api.types.RecordType;
import io.ballerina.runtime.api.utils.JsonUtils;
import io.ballerina.runtime.api.utils.StringUtils;
import io.ballerina.runtime.api.utils.TypeUtils;
import io.ballerina.runtime.api.values.BArray;
import io.ballerina.runtime.api.values.BError;
import io.ballerina.runtime.api.values.BMap;
Expand Down Expand Up @@ -96,6 +97,10 @@ public static Object toJson(Object value) {
}

public static Object toJson(Object value, Set<Object> visitedValues) {
if (isSimpleBasicTypeOrString(value)) {
return value;
}

if (!visitedValues.add(value)) {
throw DiagnosticLog.error(DiagnosticErrorCode.CYCLIC_REFERENCE);
}
Expand Down Expand Up @@ -136,7 +141,11 @@ public static Object toJson(Object value, Set<Object> visitedValues) {
return JsonUtils.convertToJson(value);
}

public static BString getNameAnnotation(BMap<BString, Object> value, BString key) {
private static boolean isSimpleBasicTypeOrString(Object value) {
return value == null || TypeUtils.getType(value).getTag() < 7;
}

private static BString getNameAnnotation(BMap<BString, Object> value, BString key) {
if (!(value.getType() instanceof RecordType recordType)) {
return key;
}
Expand Down

0 comments on commit 06e613a

Please sign in to comment.