Skip to content

Commit

Permalink
Support xsi:nil Attribute in xml to json/record conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
daneshk committed Dec 11, 2024
1 parent ec5fa61 commit 3c69c0c
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
62 changes: 62 additions & 0 deletions ballerina/tests/from_xml_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -1294,3 +1294,65 @@ isolated function testFromXmlWithNameAnnotation() returns error? {
Appointments result = check fromXml(xmlPayload);
test:assertEquals(result, expected, msg = "testFromXmlWithNameAnnotation result incorrect");
}

@Name {
value: "book"
}
type Book record {|
string name;
BookDetails details;
|};

type BookDetails record {|
string? author;
string language;
|};

@test:Config {
groups: ["fromXml"]
}
function testFromXmlWithNilElementAndWithoutPreserveNS() returns Error? {
xml x1 = xml `<book><name>Sherlock Holmes</name>
<details xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<author xsi:nil="true"/>
<language>English</language>
</details></book>`;
Book expected = {
name: "Sherlock Holmes",
details: {
author: (),
language: "English"
}
};
Book result = check fromXml(x1);
test:assertEquals(result, expected, msg = "testFromXmlWithNilElement result incorrect");
}

@Name {
value: "book"
}
type Book1 record {|
string name;
BookDetails1 details;
|};

type BookDetails1 record {|
string author;
string language;
|};

@test:Config {
groups: ["fromXml"]
}
function testFromXmlWithNilElementAndWithoutNillableField() returns Error? {
xml x1 = xml `<book><name>Sherlock Holmes</name>
<details xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<author xsi:nil="true"/>
<language>English</language>
</details></book>`;
Book1|Error result = fromXml(x1);
test:assertTrue(result is error, msg = "testFromXmlWithNilElementAndWithoutNillableField result incorrect");
if (result is error) {
test:assertTrue(result.message().includes("field 'details.author' in record 'xmldata:BookDetails1' should be of type 'string', found '()'"));
}
}
10 changes: 10 additions & 0 deletions ballerina/tests/xml_from_json_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -1096,3 +1096,13 @@ isolated function testWithRootTagConfig() {
test:assertFail("failed to convert json to xml");
}
}

@test:Config {
groups: ["fromJson"]
}
function testFromJsonWithNull() returns error? {
json data = null;
xml? result = check fromJson({"name":"Sherlock Holmes", "details":{"author":null, "language":"English"}}, {rootTag: "book"});
xml expected = xml `<book><name>Sherlock Holmes</name><details><author/><language>English</language></details></book>`;
test:assertEquals(result, expected, msg = "testFromJsonWithNull result incorrect");
}
16 changes: 15 additions & 1 deletion ballerina/tests/xml_to_json_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,20 @@ function testToJsonWithNilElementAndWithoutPreserveNS() returns Error? {
<author xsi:nil="true"/>
<language>English</language>
</details>`;
json j = check toJson(x, {preserveNamespaces: false});
json j = check toJson(x1, {preserveNamespaces: false});
test:assertEquals(j, {"name":"Sherlock Holmes", "details":{"author":null, "language":"English"}}, msg = "testToJsonWithNilElement result incorrect");
}

@test:Config {
groups: ["toJson"]
}
function testToJsonWithNilElementAndPreserveNS() returns Error? {
xml x1 = xml `<name>Sherlock Holmes</name>
<details xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<author xsi:nil="true"/>
<language>English</language>
</details>`;
json j = check toJson(x1, {preserveNamespaces: true});
test:assertEquals(j, {"name":"Sherlock Holmes","details":{"author":{"@xsi:nil":"true"},"language":"English",
"@xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance"}}, msg = "testToJsonWithNilElement result incorrect");
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ private static void processAttributeWithAnnotation(BXmlItem xmlItem, String attr
@SuppressWarnings("unchecked")
private static BMap<BString, Object> insertDataToMap(BMap<BString, Object> childrenData, Object children,
BMap<BString, Object> rootNode, String keyValue,
Type fieldType, FieldDetails fieldDetails, boolean nilValue) throws Exception {
Type fieldType, FieldDetails fieldDetails, boolean nilValue)
throws Exception {
if (childrenData.size() > 0) {
if (children instanceof BMap) {
BMap<BString, Object> data = (BMap<BString, Object>) children;
Expand Down Expand Up @@ -653,7 +654,7 @@ private static Object validateResult(Object result, BString elementName) {
Object validateResult;
if (result == null) {
validateResult = fromString(EMPTY_STRING);
} else if (result instanceof BMap && ((BMap<?, ?>) result).get(elementName) != null) {
} else if (result instanceof BMap && ((BMap<?, ?>) result).containsKey(elementName)) {
validateResult = ((BMap<?, ?>) result).get(elementName);
} else {
validateResult = result;
Expand Down

0 comments on commit 3c69c0c

Please sign in to comment.