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

[Master] Fix xml to record conversion for default value node #42699

Merged
merged 6 commits into from
Jun 17, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,10 @@
if (xmlNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
Element xmlElementNode = (Element) xmlNode;
boolean isLeafXMLElementNode = isLeafXMLElementNode(xmlElementNode);
if (!isLeafXMLElementNode || xmlElementNode.getAttributes().getLength() > 0) {
NamedNodeMap xmlAttributesMap = xmlElementNode.getAttributes();
if (!isLeafXMLElementNode || xmlAttributesMap.getLength() > 1
|| (xmlAttributesMap.getLength() == 1
&& !XMLNS_PREFIX.equals(xmlAttributesMap.item(0).getPrefix()))) {
generateRecords(xmlElementNode, isClosed, recordToTypeDescNodes, recordToAnnotationNodes,
recordToElementNodes, diagnosticMessages, textFieldName, withNameSpace);
}
Expand Down Expand Up @@ -320,18 +323,25 @@
}
}
}
if (isLeafXMLElementNode(xmlElement) && xmlElement.getAttributes().getLength() > 0) {
int attributeLength = xmlElement.getAttributes().getLength();
org.w3c.dom.Node attributeItem = xmlElement.getAttributes().item(0);
if (isLeafXMLElementNode(xmlElement) && attributeLength > 0) {
if (attributeLength == 1 && attributeItem.getPrefix() != null
&& XMLNS_PREFIX.equals(attributeItem.getPrefix())) {
return recordFields;

Check warning on line 331 in misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordConverter.java

View check run for this annotation

Codecov / codecov/patch

misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordConverter.java#L331

Added line #L331 was not covered by tests
}
Token fieldType = getPrimitiveTypeName(xmlElement.getFirstChild().getNodeValue());
IdentifierToken fieldName = AbstractNodeFactory.createIdentifierToken(textFieldName == null ?
escapeIdentifier("#content") : textFieldName);
Token semicolon = AbstractNodeFactory.createToken(SyntaxKind.SEMICOLON_TOKEN);
RecordFieldNode recordFieldNode = NodeFactory.createRecordFieldNode(null, null, fieldType,
fieldName, null, semicolon);
recordFields.add(recordFieldNode);
for (int j = 0; j < xmlElement.getAttributes().getLength(); j++) {
for (int j = 0; j < attributeLength; j++) {
org.w3c.dom.Node xmlAttributeNode = xmlElement.getAttributes().item(j);
if (xmlAttributeNode.getNodeType() == org.w3c.dom.Node.ATTRIBUTE_NODE) {
RecordFieldNode recordField = (RecordFieldNode) getRecordField(xmlAttributeNode);
if (xmlAttributeNode.getNodeType() == org.w3c.dom.Node.ATTRIBUTE_NODE
&& !XMLNS_PREFIX.equals(xmlAttributeNode.getPrefix())) {
Node recordField = getRecordField(xmlAttributeNode);
recordFields.add(recordField);
}
}
Expand Down Expand Up @@ -461,7 +471,10 @@
Token optionalFieldToken = isOptionalField ? questionMarkToken : null;
Token semicolonToken = AbstractNodeFactory.createToken(SyntaxKind.SEMICOLON_TOKEN);

if (isLeafXMLElementNode(xmlElementNode) && xmlElementNode.getAttributes().getLength() == 0) {
NamedNodeMap xmlAttributesMap = xmlElementNode.getAttributes();
if (isLeafXMLElementNode(xmlElementNode) && (xmlAttributesMap.getLength() == 0 ||
(xmlAttributesMap.getLength() == 1
&& XMLNS_PREFIX.equals(xmlAttributesMap.item(0).getPrefix())))) {
typeName = getPrimitiveTypeName(xmlElementNode.getFirstChild().getNodeValue());
} else {
// At the moment all are considered as Objects here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,16 @@ public class XMLToRecordConverterTests {
private final Path sample32Bal = RES_DIR.resolve(BAL_DIR)
.resolve("sample_32.bal");

private final Path sample33XML = RES_DIR.resolve(XML_DIR)
.resolve("sample_33.xml");
private final Path sample33Bal = RES_DIR.resolve(BAL_DIR)
.resolve("sample_33.bal");

private final Path sample34XML = RES_DIR.resolve(XML_DIR)
.resolve("sample_34.xml");
private final Path sample34Bal = RES_DIR.resolve(BAL_DIR)
.resolve("sample_34.bal");

private static final String XMLToRecordServiceEP = "xmlToRecord/convert";


Expand Down Expand Up @@ -486,7 +496,7 @@ public void testXMLWithMultipleNamespacesAndSameElement() throws IOException {
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
}

@Test(description = "testXMLWithMultipleNamespaces2")
@Test(description = "testXMLWithMultipleNamespaces")
public void testXMLWithMultipleNamespaces2() throws IOException {
String xmlFileContent = Files.readString(sample29XML);
String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false,
Expand All @@ -495,7 +505,7 @@ public void testXMLWithMultipleNamespaces2() throws IOException {
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
}

@Test(description = "testXMLWithMultipleNamespaces3")
@Test(description = "testXMLWithMultipleNamespaces")
public void testXMLWithMultipleNamespaces3() throws IOException {
String xmlFileContent = Files.readString(sample30XML);
String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false,
Expand All @@ -521,4 +531,22 @@ public void testXMLWithSameElementAndWithoutMultipleNamespaces() throws IOExcept
String expectedCodeBlock = Files.readString(sample32Bal).replaceAll("\\s+", "");
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
}

@Test(description = "textXMLWithDefaultValueNode")
public void textXMLWithDefaultValueNode() throws IOException {
String xmlFileContent = Files.readString(sample33XML);
String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false,
null, true).getCodeBlock().replaceAll("\\s+", "");
String expectedCodeBlock = Files.readString(sample33Bal).replaceAll("\\s+", "");
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
}

@Test(description = "textXMLWithDefaultValueNode")
public void textXMLWithDefaultValueNode2() throws IOException {
String xmlFileContent = Files.readString(sample34XML);
String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false,
"__text", true).getCodeBlock().replaceAll("\\s+", "");
String expectedCodeBlock = Files.readString(sample34Bal).replaceAll("\\s+", "");
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@xmldata:Namespace {prefix: "a", uri: "[www.example.org/personAddress](http://www.example.org/personAddress)"}
type A_Address record {
@xmldata:Namespace {prefix: "c", uri: "[www.example.org/addressCity](http://www.example.org/addressCity)"}
string city;
@xmldata:Namespace {prefix: "s", uri: "[www.example.org/addressState](http://www.example.org/addressState)"}
string state;
@xmldata:Namespace {prefix: "ct", uri: "[www.example.org/addressCountry](http://www.example.org/addressCountry)"}
string country;
};

@xmldata:Name {value: "person"}
@xmldata:Namespace {prefix: "p", uri: "[www.example.org/personData](http://www.example.org/personData)"}
type P_Person record {
@xmldata:Namespace {prefix: "n", uri: "[www.example.org/personName](http://www.example.org/personName)"}
string name;
@xmldata:Namespace {prefix: "a", uri: "[www.example.org/personAddress](http://www.example.org/personAddress)"}
A_Address address;
@xmldata:Attribute
string age;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
@xmldata:Namespace {prefix: "n", uri: "http://www.example.org/personName"}
type N_Name record {
string __text;
@xmldata:Attribute
string 'type;
};

@xmldata:Namespace {prefix: "p", uri: "http://www.example.org/personData"}
type P_Address record {
@xmldata:Namespace {prefix: "p", uri: "http://www.example.org/personData"}
string street;
@xmldata:Namespace {prefix: "p", uri: "http://www.example.org/personData"}
string city;
};

@xmldata:Namespace {prefix: "p", uri: "http://www.example.org/personData"}
type P_Occupation record {
string __text;
@xmldata:Attribute
string country;
};

@xmldata:Name {value: "person"}
@xmldata:Namespace {prefix: "p", uri: "http://www.example.org/personData"}
type P_Person record {
@xmldata:Namespace {prefix: "n", uri: "http://www.example.org/personName"}
N_Name name;
@xmldata:Namespace {prefix: "p", uri: "http://www.example.org/personData"}
P_Address address;
@xmldata:Namespace {prefix: "p", uri: "http://www.example.org/personData"}
P_Occupation occupation;
@xmldata:Attribute
string age;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<p:person xmlns:p="[www.example.org/personData](http://www.example.org/personData)" age="30">
<n:name xmlns:n="[www.example.org/personName](http://www.example.org/personName)">John</n:name>
<a:address xmlns:a="[www.example.org/personAddress](http://www.example.org/personAddress)">
<c:city xmlns:c="[www.example.org/addressCity](http://www.example.org/addressCity)">Hollywood</c:city>
<s:state xmlns:s="[www.example.org/addressState](http://www.example.org/addressState)">FL</s:state>
<ct:country xmlns:ct="[www.example.org/addressCountry](http://www.example.org/addressCountry)">USA</ct:country>
</a:address>
</p:person>
mindula marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<p:person xmlns:p="http://www.example.org/personData" age="30">
<n:name xmlns:n="http://www.example.org/personName" type="first">John</n:name>
<p:address xmlns:p="http://www.example.org/personData">
<p:street>Main Street</p:street>
<p:city>New York</p:city>
</p:address>
<p:occupation xmlns:p="http://www.example.org/personData" country="USA">Software Engineer</p:occupation>
</p:person>
Loading