Skip to content

Commit

Permalink
Merge pull request #40936 from gimantha/master
Browse files Browse the repository at this point in the history
Fix "Runtime assignment of () to string"
  • Loading branch information
hasithaa authored Jul 7, 2023
2 parents 89d1ca8 + 63a9478 commit ce39c51
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ public enum ErrorCodes implements DiagnosticCode {

REGEXP_INVALID_HEX_DIGIT("regexp.invalid.hex.digit", "RUNTIME_0120"),
CONFIG_TOML_INVALID_MODULE_STRUCTURE_WITH_VARIABLE("config.toml.invalid.module.structure.with.variable",
"RUNTIME_0121");
"RUNTIME_0121"),
EMPTY_XML_SEQUENCE_HAS_NO_ATTRIBUTES("empty.xml.sequence.no.attributes", "RUNTIME_0122");

private final String errorMsgKey;
private final String errorCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ incorrect.action.invocation = invalid action invocation. connector variable expe
invalid.var.assignment = invalid usage of 'var'
xml.attribute.map.update.not.allowed = xml attributes cannot be updated as a collection. update attributes one at a time
xml.qname.update.not.allowed = cannot assign values to an xml qualified name
empty.xml.sequence.no.attributes = empty xml sequence cannot have attributes
undefined.namespace = undefined namespace ''{0}''
incorrect.function.arguments = incorrect arguments for function pointer - ''{0}''
server.connector.already.exist = server connector config with port - {0} already exist with different parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public class GetAttribute {

public static Object getAttribute(BXml xmlVal, BString attrName, boolean optionalFiledAccess) {
if (xmlVal.getNodeType() == XmlNodeType.SEQUENCE && xmlVal.size() == 0) {
if (!optionalFiledAccess) {
return createError(XML_OPERATION_ERROR, ErrorHelper.getErrorDetails(
ErrorCodes.EMPTY_XML_SEQUENCE_HAS_NO_ATTRIBUTES));
}
return null;
}
if (!IsElement.isElement(xmlVal)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,9 @@ public void testPrintAttribMap() {
System.setOut(original);
}
}

@Test
public void testAttributesInEmptyXMLSequence() {
BRunUtil.invoke(xmlAttrProgFile, "testAttributesInEmptyXMLSequence");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,34 @@ function testCharacterReferencesInXmlAttributeValue() {
panic error("Assertion error, expected `" + expected + "`, found `" + att + "`");
}

function testAttributesInEmptyXMLSequence() {
xml testXml = xml `
<ClinicalDocument>
<realmCode code="US"/>
<typeId root="2.16.840.1.113883.1.3" extension="POCD_HD000040"/>
<templateId root="2.16.840.1.113883"/>
</ClinicalDocument>
`;

xml idElement = testXml/<id>;
string|error idVal = idElement.root;
string? optionalIdVal = checkpanic idElement?.root;
assertEquality((), optionalIdVal);
if idVal is error {
error error(_, ...details) = idVal;
assertEquality("empty xml sequence cannot have attributes", details["message"]);
return;
}
panic error("expected 'error', found 'string'");
}

public function print(any|error... values) = @java:Method {
'class: "org.ballerinalang.test.utils.interop.Utils"
} external;

function assertEquality(anydata expected, anydata actual) {
if expected == actual {
return;
}
panic error("expected '" + expected.toString() + "', found '" + actual.toString() + "'");
}

0 comments on commit ce39c51

Please sign in to comment.