From f374fc2737400d8511b1b6d6eff1ec456df572c7 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Thu, 28 Nov 2024 17:04:59 +0530 Subject: [PATCH 1/8] [Automated] Update the native jar versions --- ballerina/Dependencies.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/ballerina/Dependencies.toml b/ballerina/Dependencies.toml index 7c9e3b6..9cef1b0 100644 --- a/ballerina/Dependencies.toml +++ b/ballerina/Dependencies.toml @@ -124,3 +124,4 @@ dependencies = [ modules = [ {org = "ballerina", packageName = "time", moduleName = "time"} ] + From 1a718a23d6c4efb8248d856be27f0523619daa10 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Thu, 5 Sep 2024 12:08:03 +0530 Subject: [PATCH 2/8] Add err for attrbutes with arr type in parseType --- ballerina/tests/fromXml_test.bal | 30 +++++++++++++++++++ .../xmldata/utils/DiagnosticErrorCode.java | 4 ++- .../lib/data/xmldata/xml/XmlTraversal.java | 5 ++++ native/src/main/resources/error.properties | 2 ++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/ballerina/tests/fromXml_test.bal b/ballerina/tests/fromXml_test.bal index 6033b2c..6192a8f 100644 --- a/ballerina/tests/fromXml_test.bal +++ b/ballerina/tests/fromXml_test.bal @@ -3635,3 +3635,33 @@ function testXmlToRecordWithInvalidExpectedTypeForText() { test:assertTrue(rec6 is Error); test:assertEquals((rec6).message(), "unsupported input type"); } + +@test:Config { + groups: ["fromXml"] +} +function testXmlToRecordWithInvalidExpectedTypeForAttributes() { + xml value = xml `1`; + record {int[] id;}|error rec = parseAsType(value); + test:assertEquals((rec).message(), "attribute 'id' cannot be converted into the array type 'int[]'"); + + xml value2 = xml ` + 1 + 2 + 3 + `; + + record {int[] id;}|error rec2 = parseAsType(value2); + test:assertEquals(rec2, {id:[1,2,3]}); + + xml value3 = xml `3`; + record {record{int[] id;} b;}|error rec3 = parseAsType(value3); + test:assertEquals((rec3).message(), "attribute 'id' cannot be converted into the array type 'int[]'"); + + xml value4 = xml ` + 1 + 2 + 3 + `; + record {record{int[] id;}[] id;}|error rec4 = parseAsType(value4); + test:assertEquals((rec4).message(), "attribute 'id' cannot be converted into the array type 'int[]'"); +} diff --git a/native/src/main/java/io/ballerina/lib/data/xmldata/utils/DiagnosticErrorCode.java b/native/src/main/java/io/ballerina/lib/data/xmldata/utils/DiagnosticErrorCode.java index 21fa9fd..a73d65a 100644 --- a/native/src/main/java/io/ballerina/lib/data/xmldata/utils/DiagnosticErrorCode.java +++ b/native/src/main/java/io/ballerina/lib/data/xmldata/utils/DiagnosticErrorCode.java @@ -54,7 +54,9 @@ public enum DiagnosticErrorCode { INVALID_SEQUENCE_ANNOTATION("XML_ERROR_0026", "invalid.sequence.annotation"), INVALID_CHOICE_ANNOTATION("XML_ERROR_0027", "invalid.choice.annotation"), INVALID_XSD_ANNOTATION("XML_ERROR_0028", "invalid.xsd.annotation"), - INVALID_XML("XML_ERROR_0029", "invalid.xml"); + INVALID_XML("XML_ERROR_0029", "invalid.xml"), + ATTRIBUTE_CANNOT_CONVERT_INTO_ARRAY_TYPE("XML_ERROR_0031", + "attributes.cannot.convert.to.array.type"); String diagnosticId; String messageKey; 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 f2cb4a0..4962b9d 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 @@ -841,6 +841,11 @@ private void handleAttributes(BXmlItem xmlItem, BMap currentNod analyzerData.rootRecord); } + if (field.getFieldType().getTag() == TypeTags.ARRAY_TAG) { + throw DiagnosticLog.error(DiagnosticErrorCode.ATTRIBUTE_CANNOT_CONVERT_INTO_ARRAY_TYPE, + field.getFieldName(), field.getFieldType()); + } + try { currentNode.put(StringUtils.fromString(field.getFieldName()), DataUtils.convertStringToExpType(attributeMap.get(key), field.getFieldType())); diff --git a/native/src/main/resources/error.properties b/native/src/main/resources/error.properties index f155b3f..ead22f1 100644 --- a/native/src/main/resources/error.properties +++ b/native/src/main/resources/error.properties @@ -106,3 +106,5 @@ error.invalid.xsd.annotation=\ error.invalid.xml=\ Invalid XML found: ''{0}'' +error.attributes.cannot.convert.to.array.type=\ + attribute ''{0}'' cannot be converted into the array type ''{1}'' From 6aa7f4cd2302276cfb87aa8d016d26cb0dbaeaf5 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Fri, 6 Sep 2024 12:05:43 +0530 Subject: [PATCH 3/8] Add errors to unsupported attribute types --- ballerina/tests/fromXml_test.bal | 27 +++++++++++++++++++ .../lib/data/xmldata/utils/DataUtils.java | 21 +++++++++++++++ .../xmldata/utils/DiagnosticErrorCode.java | 3 ++- .../lib/data/xmldata/xml/XmlTraversal.java | 14 +++++++--- native/src/main/resources/error.properties | 1 + 5 files changed, 61 insertions(+), 5 deletions(-) diff --git a/ballerina/tests/fromXml_test.bal b/ballerina/tests/fromXml_test.bal index 6192a8f..08ad4f4 100644 --- a/ballerina/tests/fromXml_test.bal +++ b/ballerina/tests/fromXml_test.bal @@ -3642,6 +3642,7 @@ function testXmlToRecordWithInvalidExpectedTypeForText() { function testXmlToRecordWithInvalidExpectedTypeForAttributes() { xml value = xml `1`; record {int[] id;}|error rec = parseAsType(value); + test:assertTrue(rec is Error); test:assertEquals((rec).message(), "attribute 'id' cannot be converted into the array type 'int[]'"); xml value2 = xml ` @@ -3655,6 +3656,7 @@ function testXmlToRecordWithInvalidExpectedTypeForAttributes() { xml value3 = xml `3`; record {record{int[] id;} b;}|error rec3 = parseAsType(value3); + test:assertTrue(rec3 is Error); test:assertEquals((rec3).message(), "attribute 'id' cannot be converted into the array type 'int[]'"); xml value4 = xml ` @@ -3663,5 +3665,30 @@ function testXmlToRecordWithInvalidExpectedTypeForAttributes() { 3 `; record {record{int[] id;}[] id;}|error rec4 = parseAsType(value4); + test:assertTrue(rec4 is Error); test:assertEquals((rec4).message(), "attribute 'id' cannot be converted into the array type 'int[]'"); + + xml value5 = xml `1`; + record {map id;}|error rec5 = parseAsType(value5); + test:assertTrue(rec5 is Error); + test:assertEquals((rec5).message(), "attribute 'id' cannot be converted into the array type 'map'"); + + xml value6 = xml `3`; + record {record{map id;} b;}|error rec6 = parseAsType(value6); + test:assertTrue(rec6 is Error); + test:assertEquals((rec6).message(), "attribute 'id' cannot be converted into the array type 'map'"); + + xml value7 = xml `1`; + record {string:RegExp id;}|error rec7 = parseAsType(value7); + test:assertTrue(rec7 is Error); + test:assertEquals((rec7).message(), "unsupported input type"); + + xml value8 = xml ` + 1 + 2 + 3 + `; + record {record{string:RegExp id;}[] id;}|error rec8 = parseAsType(value8); + test:assertTrue(rec8 is Error); + test:assertEquals((rec8).message(), "unsupported input type"); } diff --git a/native/src/main/java/io/ballerina/lib/data/xmldata/utils/DataUtils.java b/native/src/main/java/io/ballerina/lib/data/xmldata/utils/DataUtils.java index efca262..e3cf856 100644 --- a/native/src/main/java/io/ballerina/lib/data/xmldata/utils/DataUtils.java +++ b/native/src/main/java/io/ballerina/lib/data/xmldata/utils/DataUtils.java @@ -1012,6 +1012,27 @@ public static String generateStringFromXmlReader(Reader reader) throws IOExcepti return builder.toString(); } + public static boolean isSupportedTypeForAttributes(Type fieldType) { + if (TypeTags.isIntegerTypeTag(fieldType.getTag())) { + return true; + } + + if (TypeTags.isStringTypeTag(fieldType.getTag())) { + return true; + } + + if (TypeTags.isXMLTypeTag(fieldType.getTag())) { + return false; + } + + return switch (fieldType.getTag()) { + case TypeTags.FLOAT_TAG, TypeTags.BOOLEAN_TAG, TypeTags.NULL_TAG, + TypeTags.DECIMAL_TAG, TypeTags.BYTE_TAG, TypeTags.UNION_TAG, TypeTags.ANYDATA_TAG, + TypeTags.ANY_TAG, TypeTags.JSON_TAG -> true; + default -> false; + }; + } + public static boolean isContainsUnionType(Type expType) { if (expType == null) { return false; diff --git a/native/src/main/java/io/ballerina/lib/data/xmldata/utils/DiagnosticErrorCode.java b/native/src/main/java/io/ballerina/lib/data/xmldata/utils/DiagnosticErrorCode.java index a73d65a..07c1b37 100644 --- a/native/src/main/java/io/ballerina/lib/data/xmldata/utils/DiagnosticErrorCode.java +++ b/native/src/main/java/io/ballerina/lib/data/xmldata/utils/DiagnosticErrorCode.java @@ -56,7 +56,8 @@ public enum DiagnosticErrorCode { INVALID_XSD_ANNOTATION("XML_ERROR_0028", "invalid.xsd.annotation"), INVALID_XML("XML_ERROR_0029", "invalid.xml"), ATTRIBUTE_CANNOT_CONVERT_INTO_ARRAY_TYPE("XML_ERROR_0031", - "attributes.cannot.convert.to.array.type"); + "attributes.cannot.convert.to.array.type"), + CANNOT_CONVERT_ATTRIBUTE_TO_ARRAY_TYPE("XML_ERROR_0032", "cannot.convert.attributes.to.array.type"); String diagnosticId; String messageKey; 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 4962b9d..7af07b0 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 @@ -841,14 +841,20 @@ private void handleAttributes(BXmlItem xmlItem, BMap currentNod analyzerData.rootRecord); } - if (field.getFieldType().getTag() == TypeTags.ARRAY_TAG) { - throw DiagnosticLog.error(DiagnosticErrorCode.ATTRIBUTE_CANNOT_CONVERT_INTO_ARRAY_TYPE, - field.getFieldName(), field.getFieldType()); + Type fieldType = field.getFieldType(); + + if (DataUtils.isRegExpType(fieldType)) { + throw DiagnosticLog.error(DiagnosticErrorCode.UNSUPPORTED_TYPE); + } + + if (!DataUtils.isSupportedTypeForAttributes(TypeUtils.getReferredType(fieldType))) { + throw DiagnosticLog.error(DiagnosticErrorCode.CANNOT_CONVERT_ATTRIBUTE_TO_ARRAY_TYPE, + field.getFieldName(), fieldType); } try { currentNode.put(StringUtils.fromString(field.getFieldName()), - DataUtils.convertStringToExpType(attributeMap.get(key), field.getFieldType())); + DataUtils.convertStringToExpType(attributeMap.get(key), fieldType)); } catch (Exception e) { // Ignore: Expected type will mismatch when element and attribute having same name. } diff --git a/native/src/main/resources/error.properties b/native/src/main/resources/error.properties index ead22f1..fcc54da 100644 --- a/native/src/main/resources/error.properties +++ b/native/src/main/resources/error.properties @@ -107,4 +107,5 @@ error.invalid.xsd.annotation=\ error.invalid.xml=\ Invalid XML found: ''{0}'' error.attributes.cannot.convert.to.array.type=\ +error.cannot.convert.attributes.to.array.type=\ attribute ''{0}'' cannot be converted into the array type ''{1}'' From 58330d13a66703f103679b87d271ec33c631040a Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Fri, 6 Sep 2024 12:07:11 +0530 Subject: [PATCH 4/8] Revert dependency.toml file --- ballerina/Dependencies.toml | 1 - 1 file changed, 1 deletion(-) 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"} ] - From a483319734b0d0a8349cb66a6fbbeec5e73f6198 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Wed, 30 Oct 2024 16:46:29 +0530 Subject: [PATCH 5/8] [Automated] Update the native jar versions --- ballerina/Dependencies.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/ballerina/Dependencies.toml b/ballerina/Dependencies.toml index 7c9e3b6..9cef1b0 100644 --- a/ballerina/Dependencies.toml +++ b/ballerina/Dependencies.toml @@ -124,3 +124,4 @@ dependencies = [ modules = [ {org = "ballerina", packageName = "time", moduleName = "time"} ] + From 8e834069303ca2f97cd1fa2fa463e118875d8340 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Wed, 30 Oct 2024 17:17:02 +0530 Subject: [PATCH 6/8] Revert the dependenct toml file --- ballerina/Dependencies.toml | 1 - 1 file changed, 1 deletion(-) 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"} ] - From c78ecc7827c9ef90136870c3bf07c966e505c54b Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Wed, 30 Oct 2024 17:17:45 +0530 Subject: [PATCH 7/8] [Automated] Update the native jar versions --- ballerina/Dependencies.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/ballerina/Dependencies.toml b/ballerina/Dependencies.toml index 7c9e3b6..9cef1b0 100644 --- a/ballerina/Dependencies.toml +++ b/ballerina/Dependencies.toml @@ -124,3 +124,4 @@ dependencies = [ modules = [ {org = "ballerina", packageName = "time", moduleName = "time"} ] + From 89c764207ddb7436ac893d93fe1a7b4a061b8226 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Thu, 28 Nov 2024 14:26:27 +0530 Subject: [PATCH 8/8] Refactor error properties for attribute arrays --- native/src/main/resources/error.properties | 1 - 1 file changed, 1 deletion(-) diff --git a/native/src/main/resources/error.properties b/native/src/main/resources/error.properties index fcc54da..39f8892 100644 --- a/native/src/main/resources/error.properties +++ b/native/src/main/resources/error.properties @@ -106,6 +106,5 @@ error.invalid.xsd.annotation=\ error.invalid.xml=\ Invalid XML found: ''{0}'' -error.attributes.cannot.convert.to.array.type=\ error.cannot.convert.attributes.to.array.type=\ attribute ''{0}'' cannot be converted into the array type ''{1}''