Skip to content

Commit

Permalink
Add err for attrbutes with arr type in parseType
Browse files Browse the repository at this point in the history
  • Loading branch information
SasinduDilshara committed Sep 5, 2024
1 parent 9f186b5 commit ae10cca
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
30 changes: 30 additions & 0 deletions ballerina/tests/fromXml_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -3608,3 +3608,33 @@ isolated function testTypeRefArray() {
Ports|error rec = parseString(s);
test:assertEquals(rec, {"port":[{"#content":"1"},{"#content":"1"}]});
}

@test:Config {
groups: ["fromXml"]
}
function testXmlToRecordWithInvalidExpectedTypeForAttributes() {
xml value = xml `<a id="2">1</a>`;
record {int[] id;}|error rec = parseAsType(value);
test:assertEquals((<error>rec).message(), "attribute 'id' cannot be converted into the array type 'int[]'");

xml value2 = xml `<a id="2">
<id id="2">1</id>
<id id="2">2</id>
<id id="2">3</id>
</a>`;

record {int[] id;}|error rec2 = parseAsType(value2);
test:assertEquals(rec2, {id:[1,2,3]});

xml value3 = xml `<a id="1"><b id="2">3</b></a>`;
record {record{int[] id;} b;}|error rec3 = parseAsType(value3);
test:assertEquals((<error>rec3).message(), "attribute 'id' cannot be converted into the array type 'int[]'");

xml value4 = xml `<a id="2">
<id id="2">1</id>
<id id="2">2</id>
<id id="2">3</id>
</a>`;
record {record{int[] id;}[] id;}|error rec4 = parseAsType(value4);
test:assertEquals((<error>rec4).message(), "attribute 'id' cannot be converted into the array type 'int[]'");
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,11 @@ private void handleAttributes(BXmlItem xmlItem, BMap<BString, Object> 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()));
Expand Down
3 changes: 3 additions & 0 deletions native/src/main/resources/error.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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}''

0 comments on commit ae10cca

Please sign in to comment.