Skip to content

Commit

Permalink
Merge pull request #27 from prakanth97/issue_6694
Browse files Browse the repository at this point in the history
Fix record to xml conversion missing namespace when namespace annotation defined in field level and annotation lookup logic
  • Loading branch information
prakanth97 authored Jul 5, 2024
2 parents a8b2209 + b281d17 commit a15b675
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 32 deletions.
6 changes: 3 additions & 3 deletions ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
org = "ballerina"
name = "data.xmldata"
version = "0.1.1"
version = "0.1.2"
authors = ["Ballerina"]
keywords = ["xml"]
repository = "https://github.com/ballerina-platform/module-ballerina-data-xmldata"
Expand All @@ -12,5 +12,5 @@ export = ["data.xmldata"]
[[platform.java17.dependency]]
groupId = "io.ballerina.lib"
artifactId = "data-native"
version = "0.1.1"
path = "../native/build/libs/data.xmldata-native-0.1.1.jar"
version = "0.1.2"
path = "../native/build/libs/data.xmldata-native-0.1.2-SNAPSHOT.jar"
2 changes: 1 addition & 1 deletion ballerina/CompilerPlugin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ id = "constraint-compiler-plugin"
class = "io.ballerina.lib.data.xmldata.compiler.XmldataCompilerPlugin"

[[dependency]]
path = "../compiler-plugin/build/libs/data.xmldata-compiler-plugin-0.1.1.jar"
path = "../compiler-plugin/build/libs/data.xmldata-compiler-plugin-0.1.2-SNAPSHOT.jar"
2 changes: 1 addition & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ distribution-version = "2201.9.0"
[[package]]
org = "ballerina"
name = "data.xmldata"
version = "0.1.1"
version = "0.1.2"
dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
Expand Down
165 changes: 165 additions & 0 deletions ballerina/tests/toXml_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,171 @@ isolated function testRecordWithNamespaceAnnotationToXml1() returns error? {
test:assertEquals(result.toString(), expected, msg = "testComplexRecordToXml result incorrect");
}

type AddressR1 record {|
string city;
int code;
|};

type Wsa_ReplyTo1 record {
@Namespace {prefix: "wsa", uri: "example1.com"}
AddressR1 Address;
};

type Htng_ReplyTo1 record {
@Namespace {prefix: "wsa", uri: "example1.com"}
AddressR1 Address;
};

@Name {value: "soap"}
type Soap1 record {
@Name {value: "ReplyTo"}
@Namespace {prefix: "wsa", uri: "example1.com"}
Wsa_ReplyTo1 wsaReplyTo;
@Name {value: "ReplyTo"}
@Namespace {prefix: "htng", uri: "example2.com"}
Htng_ReplyTo1 htngReplyTo;
};

@test:Config {
groups: ["toXml", "testFail"]
}
isolated function testXmlToRecordWithNamespaceAttachedToFields() returns error? {
Soap1 val = {
htngReplyTo: {
Address: {
code: 40000,
city: "Colombo"
}
},
wsaReplyTo: {
Address: {
code: 10000,
city: "Kandy"
}
}
};

xml xmlVal = check toXml(val);
string expected = "<soap>" +
"<wsa:ReplyTo xmlns:wsa=\"example1.com\">" +
"<wsa:Address><city>Kandy</city><code>10000</code></wsa:Address>" +
"</wsa:ReplyTo><htng:ReplyTo xmlns:htng=\"example2.com\">" +
"<wsa:Address xmlns:wsa=\"example1.com\"><city>Colombo</city><code>40000</code>" +
"</wsa:Address></htng:ReplyTo></soap>";
test:assertEquals(xmlVal.toString(), expected);
}

@Namespace {prefix: "wsa", uri: "example1.com"}
type AddressR2 record {|
string city;
int code;
|};

@Namespace {prefix: "wsa", uri: "example1.com"}
type Wsa_ReplyTo2 record {
@Namespace {prefix: "wsa", uri: "example1.com"}
AddressR2 Address;
};

@Namespace {prefix: "htng", uri: "example2.com"}
type Htng_ReplyTo2 record {
@Namespace {prefix: "wsa", uri: "example1.com"}
AddressR2 Address;
};

@Name {value: "soap"}
type Soap2 record {
@Name {value: "ReplyTo"}
@Namespace {prefix: "wsa", uri: "example1.com"}
Wsa_ReplyTo2 wsaReplyTo;
@Name {value: "ReplyTo"}
@Namespace {prefix: "htng", uri: "example2.com"}
Htng_ReplyTo2 htngReplyTo;
};

@test:Config {
groups: ["toXml"]
}
isolated function testXmlToRecordWithNamespaceAttachedToFieldsAndTypes() returns error? {
Soap2 val = {
htngReplyTo: {
Address: {
code: 40000,
city: "Colombo"
}
},
wsaReplyTo: {
Address: {
code: 10000,
city: "Kandy"
}
}
};

xml xmlVal = check toXml(val);
string expected = "<soap>" +
"<wsa:ReplyTo xmlns:wsa=\"example1.com\">" +
"<wsa:Address><city>Kandy</city><code>10000</code></wsa:Address>" +
"</wsa:ReplyTo><htng:ReplyTo xmlns:htng=\"example2.com\">" +
"<wsa:Address xmlns:wsa=\"example1.com\"><city>Colombo</city><code>40000</code>" +
"</wsa:Address></htng:ReplyTo></soap>";
test:assertEquals(xmlVal.toString(), expected);
}

type RequestorID record {
@Attribute
string ID;
@Attribute
string ID_Context;
@Attribute
string Type;
};

type Source record {
RequestorID RequestorID;
};

@test:Config {
groups: ["toXml"]
}
isolated function testUnderscoreInTheFieldName() returns error? {
Source s = {
RequestorID: {
ID: "1",
ID_Context: "2",
Type: "3"}};
xml xmlVal = check toXml(s);
test:assertEquals(xmlVal.toString(), "<Source><RequestorID ID=\"1\" ID_Context=\"2\" Type=\"3\"/></Source>");
}

@Namespace {
uri: "example.com"
}
type File record {|
@Namespace {
uri: "example.com"
}
string fileName;
@Namespace {
uri: "example.com"
}
string fileNamespace;
|};

@test:Config {
groups: ["toXml"]
}
isolated function testToRecordFieldNameEndsWithNameOrNamespace() returns error? {
File file = {
fileName: "test.bal",
fileNamespace: "wso2.com"
};

xml result = check toXml(file);
string expected = "<File xmlns=\"example.com\"><fileName>test.bal</fileName><fileNamespace>wso2.com</fileNamespace></File>";
test:assertEquals(result.toString(), expected);
}

@test:Config {
groups: ["toXml"]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ private Constants() {}
public static final ArrayType JSON_ARRAY_TYPE = TypeCreator.createArrayType(PredefinedTypes.TYPE_JSON);
public static final String FIELD = "$field$.";
public static final String NAMESPACE = "Namespace";
public static final String MODULE_NAME = "ballerina/data.xmldata";
public static final BString URI = StringUtils.fromString("uri");
public static final BString PREFIX = StringUtils.fromString("prefix");
public static final String ATTRIBUTE = "Attribute";
Expand All @@ -58,6 +59,6 @@ private Constants() {}
public static final BString ATTRIBUTE_PREFIX = StringUtils.fromString("attributePrefix");
public static final BString TEXT_FIELD_NAME = StringUtils.fromString("textFieldName");
public static final BString ALLOW_DATA_PROJECTION = StringUtils.fromString("allowDataProjection");
public static final String NON_NUMERIC_STRING_REGEX = "[^a-zA-Z\\d\s]";
public static final String RECORD_FIELD_NAME_ESCAPE_CHAR_REGEX = "[^a-zA-Z\\d\s_]";
public static final String NS_ANNOT_NOT_DEFINED = "$$ns_annot_not_defined$$";
}
Loading

0 comments on commit a15b675

Please sign in to comment.