Skip to content

Commit

Permalink
Merge pull request #45 from SasinduDilshara/issue_6924
Browse files Browse the repository at this point in the history
Fix CCE when expected type is a array/regex type in xml text values
  • Loading branch information
gimantha authored Nov 28, 2024
2 parents 2616ee8 + ddfcbe7 commit 40ee711
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
1 change: 0 additions & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,3 @@ dependencies = [
modules = [
{org = "ballerina", packageName = "time", moduleName = "time"}
]

27 changes: 27 additions & 0 deletions ballerina/tests/fromXml_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -3608,3 +3608,30 @@ isolated function testTypeRefArray() {
Ports|error rec = parseString(s);
test:assertEquals(rec, {"port":[{"#content":"1"},{"#content":"1"}]});
}

@test:Config
function testXmlToRecordWithInvalidExpectedTypeForText() {
record {int[] \#content;}|error rec = parseAsType(xml `<A>42</A>`);
test:assertTrue(rec is Error);
test:assertEquals((<Error>rec).message(), "'string' value '42' cannot be converted to 'int[]'");

record {map<int> \#content;}|error rec2 = parseAsType(xml `<A>42</A>`);
test:assertTrue(rec2 is Error);
test:assertEquals((<Error>rec2).message(), "'string' value '42' cannot be converted to 'map<int>'");

record {record {string[] \#content;} B;}|error rec3 = parseAsType(xml `<A><B>Hello</B></A>`);
test:assertTrue(rec3 is Error);
test:assertEquals((<Error>rec3).message(), "'string' value 'Hello' cannot be converted to 'string[]'");

record {record {map<string> \#content;} B;}|error rec4 = parseAsType(xml `<A><B>Hello</B></A>`);
test:assertTrue(rec4 is Error);
test:assertEquals((<Error>rec4).message(), "'string' value 'Hello' cannot be converted to 'map<string>'");

record {string:RegExp \#content;}|error rec5 = parseAsType(xml `<A>42</A>`);
test:assertTrue(rec5 is Error);
test:assertEquals((<Error>rec5).message(), "unsupported input type");

record {record {string:RegExp \#content;} B;}|error rec6 = parseAsType(xml `<A><B>Hello</B></A>`);
test:assertTrue(rec6 is Error);
test:assertEquals((<Error>rec6).message(), "unsupported input type");
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,15 @@ private void convertText(String text, XmlAnalyzerData analyzerData) {
}
}

BString fieldName = StringUtils.fromString(currentField.getFieldName());
Type fieldType = TypeUtils.getReferredType(currentField.getFieldType());
Type fieldType = currentField.getFieldType();

Object convertedValue = null;
if (DataUtils.isRegExpType(fieldType)) {
throw DiagnosticLog.error(DiagnosticErrorCode.UNSUPPORTED_TYPE);
}

fieldType = TypeUtils.getReferredType(fieldType);
BString fieldName = StringUtils.fromString(currentField.getFieldName());
Object value = mapValue.get(fieldName);
if (fieldType.getTag() == TypeTags.UNION_TAG) {
XmlAnalyzerData clonedAnalyzerData = XmlAnalyzerData.copy(analyzerData);
Expand Down Expand Up @@ -229,7 +234,8 @@ private void convertText(String text, XmlAnalyzerData analyzerData) {
((BArray) value).add(currentIndex, convertedValue);
} else {
if (fieldType.getTag() == TypeTags.ARRAY_TAG) {
throw DiagnosticLog.error(DiagnosticErrorCode.FIELD_CANNOT_CAST_INTO_TYPE, fieldName, fieldType);
throw DiagnosticLog.error(DiagnosticErrorCode.CANNOT_CONVERT_TO_EXPECTED_TYPE,
PredefinedTypes.TYPE_STRING.getName(), text, fieldType);
}
mapValue.put(fieldName, convertedValue);
}
Expand Down

0 comments on commit 40ee711

Please sign in to comment.