Skip to content

Commit

Permalink
Handle namespace attached to the field and type
Browse files Browse the repository at this point in the history
  • Loading branch information
prakanth97 committed Jul 4, 2024
1 parent 60fad59 commit 0f46dd1
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 18 deletions.
88 changes: 73 additions & 15 deletions ballerina/tests/toXml_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// under the License.

import ballerina/test;
import ballerina/io;

@test:Config {
groups: ["toXml"]
Expand Down Expand Up @@ -1165,36 +1166,36 @@ isolated function testRecordWithNamespaceAnnotationToXml1() returns error? {
test:assertEquals(result.toString(), expected, msg = "testComplexRecordToXml result incorrect");
}

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

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

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

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

@test:Config {
groups: ["toXml"]
}
isolated function testXmlToRecordWithNamespaceAttachedToFields() returns error? {
Soap val = {
Soap1 val = {
htngReplyTo: {
Address: {
code: 40000,
Expand All @@ -1213,14 +1214,71 @@ isolated function testXmlToRecordWithNamespaceAttachedToFields() returns error?
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\">" +
"<htng:Address><city>Colombo</city><code>40000</code></htng:Address>" +
"</htng:ReplyTo>" +
"</soap>";
"</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>";
io:println(xmlVal);
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>";
io:println(xmlVal);
test:assertEquals(xmlVal.toString(), expected);
}

type RequestorID record {
@Attribute
string ID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,17 +460,25 @@ private static BMap<BString, Object> addFields(BMap<BString, Object> input, Type
} else if (fieldType.getTag() == TypeTags.TYPE_REFERENCED_TYPE_TAG) {
Type referredType = TypeUtils.getReferredType(fieldType);
BMap<BString, Object> namespaceAnnotRecord = ValueCreator.createMapValue(Constants.JSON_MAP_TYPE);
boolean doesNamespaceDefinedInField = false;
if (annotations.size() > 0) {
String fieldName = key;
key = getKeyNameFromAnnotation(annotations, key);
QName qName = addFieldNamespaceAnnotation(fieldName, key, annotations, namespaceAnnotRecord);
if (!qName.getNamespaceURI().equals("")) {
doesNamespaceDefinedInField = true;
}
String localPart = qName.getLocalPart();
key = qName.getPrefix().isBlank() ? localPart : qName.getPrefix() + ":" + localPart;
}
BMap<BString, Object> subRecordAnnotations = ((RecordType) referredType).getAnnotations();
key = getElementName(subRecordAnnotations, key);

BMap<BString, Object> annotationRecord = ValueCreator.createMapValue(Constants.JSON_MAP_TYPE);
processSubRecordAnnotation(subRecordAnnotations, annotationRecord);
if (!doesNamespaceDefinedInField) {
BMap<BString, Object> subRecordAnnotations = ((RecordType) referredType).getAnnotations();
key = getElementName(subRecordAnnotations, key);
processSubRecordAnnotation(subRecordAnnotations, annotationRecord);
}

BMap<BString, Object> subRecordValue = addFields(((BMap<BString, Object>) value), referredType);
addNamespaceToSubRecord(key, namespaceAnnotRecord, subRecordValue);
if (annotationRecord.size() > 0) {
Expand Down Expand Up @@ -744,6 +752,9 @@ private static String getElementName(BMap<BString, Object> annotation, String ke
key = prefix.getValue().concat(Constants.COLON).concat(key);
}
}
}

for (BString value : keys) {
if (value.getValue().endsWith(Constants.NAME)) {
key = processNameAnnotation(annotation, key, value, hasNamespaceAnnotation);
}
Expand Down

0 comments on commit 0f46dd1

Please sign in to comment.