Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CCE when expected type is a array/regex type in xml text values #45

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,3 @@ dependencies = [
modules = [
{org = "ballerina", packageName = "time", moduleName = "time"}
]

27 changes: 27 additions & 0 deletions ballerina/tests/fromXml_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<A>42</A>`);
test:assertTrue(rec is Error);
test:assertEquals((<Error>rec).message(), "'string' value '42' cannot be converted to 'int[]'");

record {map<int> \#content;}|error rec2 = parseAsType(xml `<A>42</A>`);
test:assertTrue(rec2 is Error);
test:assertEquals((<Error>rec2).message(), "'string' value '42' cannot be converted to 'map<int>'");

record {record {string[] \#content;} B;}|error rec3 = parseAsType(xml `<A><B>Hello</B></A>`);
test:assertTrue(rec3 is Error);
test:assertEquals((<Error>rec3).message(), "'string' value 'Hello' cannot be converted to 'string[]'");

record {record {map<string> \#content;} B;}|error rec4 = parseAsType(xml `<A><B>Hello</B></A>`);
test:assertTrue(rec4 is Error);
test:assertEquals((<Error>rec4).message(), "'string' value 'Hello' cannot be converted to 'map<string>'");

record {string:RegExp \#content;}|error rec5 = parseAsType(xml `<A>42</A>`);
test:assertTrue(rec5 is Error);
test:assertEquals((<Error>rec5).message(), "unsupported input type");

record {record {string:RegExp \#content;} B;}|error rec6 = parseAsType(xml `<A><B>Hello</B></A>`);
test:assertTrue(rec6 is Error);
test:assertEquals((<Error>rec6).message(), "unsupported input type");
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,14 @@ private void convertText(String text, XmlAnalyzerData analyzerData) {
}
}

BString fieldName = StringUtils.fromString(currentField.getFieldName());
Type fieldType = TypeUtils.getReferredType(currentField.getFieldType());
Type fieldType = currentField.getFieldType();

if (DataUtils.isRegExpType(fieldType)) {
throw DiagnosticLog.error(DiagnosticErrorCode.UNSUPPORTED_TYPE);
}

fieldType = TypeUtils.getReferredType(fieldType);
BString fieldName = StringUtils.fromString(currentField.getFieldName());
Object convertedValue = DataUtils.convertStringToExpType(StringUtils.fromString(text), fieldType);
Object value = mapValue.get(fieldName);
if (value instanceof BArray) {
Expand All @@ -165,6 +170,10 @@ private void convertText(String text, XmlAnalyzerData analyzerData) {
}
((BArray) value).add(currentIndex, convertedValue);
} else {
if (fieldType.getTag() == TypeTags.ARRAY_TAG) {
throw DiagnosticLog.error(DiagnosticErrorCode.CANNOT_CONVERT_TO_EXPECTED_TYPE,
PredefinedTypes.TYPE_STRING.getName(), text, fieldType);
}
mapValue.put(fieldName, convertedValue);
}
}
Expand Down
Loading