From 5a61d2b0a248952e2be36c5051d377196edcf208 Mon Sep 17 00:00:00 2001 From: Sasindu Alahakoon Date: Thu, 5 Sep 2024 12:08:03 +0530 Subject: [PATCH] Add err for attrbutes with arr type in parseType --- ballerina/tests/fromXml_test.bal | 30 +++++++++++++++++++ gradle.properties | 2 +- .../xmldata/utils/DiagnosticErrorCode.java | 4 ++- .../lib/data/xmldata/xml/XmlTraversal.java | 5 ++++ native/src/main/resources/error.properties | 3 ++ 5 files changed, 42 insertions(+), 2 deletions(-) diff --git a/ballerina/tests/fromXml_test.bal b/ballerina/tests/fromXml_test.bal index 8159c92..cb9e5fc 100644 --- a/ballerina/tests/fromXml_test.bal +++ b/ballerina/tests/fromXml_test.bal @@ -3585,3 +3585,33 @@ isolated function testDuplicateField() { test:assertTrue(err4 is Error); test:assertEquals(( err4).message(), "duplicate field 'name'"); } + +@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/gradle.properties b/gradle.properties index f2fe05e..43b3c66 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,7 +1,7 @@ org.gradle.caching=true group=io.ballerina.lib version=1.0.0-SNAPSHOT -ballerinaLangVersion=2201.10.0-20240814-095500-9d14866a +ballerinaLangVersion=2201.10.0 checkstyleToolVersion=10.12.0 puppycrawlCheckstyleVersion=10.12.0 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 0d90190..db96d1f 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 @@ -41,7 +41,9 @@ public enum DiagnosticErrorCode { UNSUPPORTED_TYPE("XML_ERROR_014", "unsupported.type"), STREAM_BROKEN("XML_ERROR_015", "stream.broken"), XML_PARSE_ERROR("XML_ERROR_016", "xml.parse.error"), - UNDEFINED_FIELD("XML_ERROR_0017", "undefined.field"); + UNDEFINED_FIELD("XML_ERROR_0017", "undefined.field"), + ATTRIBUTE_CANNOT_CONVERT_INTO_ARRAY_TYPE("XML_ERROR_0017", + "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 21a0f0e..a3ce229 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 @@ -661,6 +661,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 bcb5cf9..5dd9bc7 100644 --- a/native/src/main/resources/error.properties +++ b/native/src/main/resources/error.properties @@ -70,3 +70,6 @@ error.xml.parse.error=\ error.undefined.field=\ undefined field ''{0}'' in record ''{1}'' + +error.attributes.cannot.convert.to.array.type=\ + attribute ''{0}'' cannot be converted into the array type ''{1}''