diff --git a/ballerina/Dependencies.toml b/ballerina/Dependencies.toml
index 9cef1b0..7c9e3b6 100644
--- a/ballerina/Dependencies.toml
+++ b/ballerina/Dependencies.toml
@@ -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 a5ae2f1..6033b2c 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 1340ca2..f2cb4a0 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
@@ -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);
@@ -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);
}