Skip to content

Commit

Permalink
Add errors for regex and array types for xml text values
Browse files Browse the repository at this point in the history
  • Loading branch information
SasinduDilshara committed Sep 6, 2024
1 parent 2d60fc6 commit e7a239d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
5 changes: 2 additions & 3 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ modules = [
[[package]]
org = "ballerina"
name = "io"
version = "1.6.1"
version = "1.6.2"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
Expand Down Expand Up @@ -116,12 +116,11 @@ modules = [
[[package]]
org = "ballerina"
name = "time"
version = "2.4.0"
version = "2.4.1"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
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 @@ -141,9 +141,14 @@ private void convertText(String text, XmlAnalyzerData analyzerData) {
}
}

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

if (DataUtils.isRegExpType(fieldType)) {
throw DiagnosticLog.error(DiagnosticErrorCode.UNSUPPORTED_TYPE);
}

fieldType = TypeUtils.getReferredType(fieldType);
BString fieldName = StringUtils.fromString(currentField.getFieldName());
Object convertedValue = DataUtils.convertStringToExpType(StringUtils.fromString(text), fieldType);
Object value = mapValue.get(fieldName);
if (value instanceof BArray) {
Expand All @@ -165,6 +170,10 @@ private void convertText(String text, XmlAnalyzerData analyzerData) {
}
((BArray) value).add(currentIndex, convertedValue);
} else {
if (fieldType.getTag() == TypeTags.ARRAY_TAG) {
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 e7a239d

Please sign in to comment.