From ddfcbe73b571d5e14e1f87c51c0b56be68731275 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Fri, 6 Sep 2024 10:35:25 +0530 Subject: [PATCH] Add errors for regex and array types for xml text values --- ballerina/Dependencies.toml | 1 - ballerina/tests/fromXml_test.bal | 27 +++++++++++++++++++ .../lib/data/xmldata/xml/XmlTraversal.java | 12 ++++++--- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/ballerina/Dependencies.toml b/ballerina/Dependencies.toml index 9cef1b0d..7c9e3b67 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 a5ae2f1e..6033b2c2 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 1340ca28..f2cb4a0a 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); }