diff --git a/ballerina/Dependencies.toml b/ballerina/Dependencies.toml index 0180731..0674d75 100644 --- a/ballerina/Dependencies.toml +++ b/ballerina/Dependencies.toml @@ -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"}, @@ -116,7 +116,7 @@ modules = [ [[package]] org = "ballerina" name = "time" -version = "2.4.0" +version = "2.4.1" scope = "testOnly" dependencies = [ {org = "ballerina", name = "jballerina.java"} @@ -124,4 +124,3 @@ dependencies = [ modules = [ {org = "ballerina", packageName = "time", moduleName = "time"} ] - diff --git a/ballerina/tests/fromXml_test.bal b/ballerina/tests/fromXml_test.bal index f8f63ed..85b784d 100644 --- a/ballerina/tests/fromXml_test.bal +++ b/ballerina/tests/fromXml_test.bal @@ -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 `42`); + test:assertTrue(rec is Error); + test:assertEquals((rec).message(), "'string' value '42' cannot be converted to 'int[]'"); + + record {map \#content;}|error rec2 = parseAsType(xml `42`); + test:assertTrue(rec2 is Error); + test:assertEquals((rec2).message(), "'string' value '42' cannot be converted to 'map'"); + + record {record {string[] \#content;} B;}|error rec3 = parseAsType(xml `Hello`); + test:assertTrue(rec3 is Error); + test:assertEquals((rec3).message(), "'string' value 'Hello' cannot be converted to 'string[]'"); + + record {record {map \#content;} B;}|error rec4 = parseAsType(xml `Hello`); + test:assertTrue(rec4 is Error); + test:assertEquals((rec4).message(), "'string' value 'Hello' cannot be converted to 'map'"); + + record {string:RegExp \#content;}|error rec5 = parseAsType(xml `42`); + test:assertTrue(rec5 is Error); + test:assertEquals((rec5).message(), "unsupported input type"); + + record {record {string:RegExp \#content;} B;}|error rec6 = parseAsType(xml `Hello`); + test:assertTrue(rec6 is Error); + test:assertEquals((rec6).message(), "unsupported input type"); +} diff --git a/native/src/main/java/io/ballerina/lib/data/xmldata/xml/XmlTraversal.java b/native/src/main/java/io/ballerina/lib/data/xmldata/xml/XmlTraversal.java index 21a0f0e..eb3f7b6 100644 --- a/native/src/main/java/io/ballerina/lib/data/xmldata/xml/XmlTraversal.java +++ b/native/src/main/java/io/ballerina/lib/data/xmldata/xml/XmlTraversal.java @@ -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) { @@ -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); } }