Skip to content

Commit

Permalink
Fix annotation lookup logic
Browse files Browse the repository at this point in the history
  • Loading branch information
prakanth97 committed Jul 4, 2024
1 parent 0f46dd1 commit b281d17
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 21 deletions.
33 changes: 29 additions & 4 deletions ballerina/tests/toXml_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// under the License.

import ballerina/test;
import ballerina/io;

@test:Config {
groups: ["toXml"]
Expand Down Expand Up @@ -1192,7 +1191,7 @@ type Soap1 record {
};

@test:Config {
groups: ["toXml"]
groups: ["toXml", "testFail"]
}
isolated function testXmlToRecordWithNamespaceAttachedToFields() returns error? {
Soap1 val = {
Expand All @@ -1217,7 +1216,6 @@ isolated function testXmlToRecordWithNamespaceAttachedToFields() returns error?
"</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);
}

Expand Down Expand Up @@ -1275,7 +1273,6 @@ isolated function testXmlToRecordWithNamespaceAttachedToFieldsAndTypes() returns
"</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);
}

Expand Down Expand Up @@ -1305,6 +1302,34 @@ isolated function testUnderscoreInTheFieldName() returns error? {
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ public static QualifiedName validateAndGetXmlNameFromRecordAnnotation(RecordType
BMap<BString, Object> annotations = recordType.getAnnotations();
String localName = recordName;
for (BString annotationsKey : annotations.getKeys()) {
String key = annotationsKey.getValue();
if (!key.contains(Constants.FIELD) && key.endsWith(Constants.NAME)) {
if (isNameAnnotationKey(annotationsKey.getValue())) {
String name = ((BMap<BString, Object>) annotations.get(annotationsKey)).get(Constants.VALUE).toString();
String localPart = elementName.getLocalPart();
if (!name.equals(localPart)) {
Expand All @@ -81,7 +80,7 @@ public static QualifiedName validateAndGetXmlNameFromRecordAnnotation(RecordType
// Handle the namespace annotation.
for (BString annotationsKey : annotations.getKeys()) {
String key = annotationsKey.getValue();
if (!key.contains(Constants.FIELD) && key.endsWith(Constants.NAMESPACE)) {
if (isNamespaceAnnotationKey(key)) {
Map<BString, Object> namespaceAnnotation =
((Map<BString, Object>) annotations.get(StringUtils.fromString(key)));
BString uri = (BString) namespaceAnnotation.get(Constants.URI);
Expand All @@ -106,8 +105,7 @@ private static ArrayList<String> getNamespace(RecordType recordType) {
BMap<BString, Object> annotations = recordType.getAnnotations();
ArrayList<String> namespace = new ArrayList<>();
for (BString annotationsKey : annotations.getKeys()) {
String key = annotationsKey.getValue();
if (!key.contains(Constants.FIELD) && key.endsWith(Constants.NAMESPACE)) {
if (isNamespaceAnnotationKey(annotationsKey.getValue())) {
BMap<BString, Object> namespaceAnnotation = (BMap<BString, Object>) annotations.get(annotationsKey);
namespace.add(namespaceAnnotation.containsKey(Constants.PREFIX) ?
((BString) namespaceAnnotation.get(Constants.PREFIX)).getValue() : "");
Expand Down Expand Up @@ -184,7 +182,7 @@ public static Map<QualifiedName, Field> getAllAttributesInRecordType(RecordType
@SuppressWarnings("unchecked")
public static QualifiedName getFieldNameFromRecord(Map<BString, Object> fieldAnnotation, String fieldName) {
for (BString key : fieldAnnotation.keySet()) {
if (key.getValue().endsWith(Constants.NAMESPACE)) {
if (isNamespaceAnnotationKey(key.getValue())) {
Map<BString, Object> namespaceAnnotation = ((Map<BString, Object>) fieldAnnotation.get(key));
BString uri = (BString) namespaceAnnotation.get(Constants.URI);
BString prefix = (BString) namespaceAnnotation.get(Constants.PREFIX);
Expand All @@ -198,7 +196,7 @@ public static QualifiedName getFieldNameFromRecord(Map<BString, Object> fieldAnn
@SuppressWarnings("unchecked")
private static String getModifiedName(Map<BString, Object> fieldAnnotation, String attributeName) {
for (BString key : fieldAnnotation.keySet()) {
if (key.getValue().endsWith(Constants.NAME)) {
if (isNameAnnotationKey(key.getValue())) {
return ((Map<BString, Object>) fieldAnnotation.get(key)).get(Constants.VALUE).toString();
}
}
Expand Down Expand Up @@ -525,7 +523,7 @@ private static QName addFieldNamespaceAnnotation(String fieldName, String key, B
if (annotations.containsKey(annotationKey)) {
BMap<BString, Object> annotationValue = (BMap<BString, Object>) annotations.get(annotationKey);
for (BString fieldKey : annotationValue.getKeys()) {
if (fieldKey.toString().endsWith(Constants.NAMESPACE)) {
if (isNamespaceAnnotationKey(fieldKey.getValue())) {
return processFieldNamespaceAnnotation(annotationValue, key, fieldKey, recordValue,
isAttributeField);
}
Expand All @@ -542,7 +540,7 @@ public static boolean isAttributeField(BString annotationKey, BMap<BString, Obje

BMap<BString, Object> annotationValue = (BMap<BString, Object>) annotations.get(annotationKey);
for (BString fieldKey : annotationValue.getKeys()) {
if (fieldKey.toString().endsWith(Constants.ATTRIBUTE)) {
if (isAttributeAnnotationKey(fieldKey.getValue())) {
return true;
}
}
Expand All @@ -563,7 +561,7 @@ private static BMap<BString, Object> getFieldNamespaceAndNameAnnotations(String
BMap<BString, Object> annotationValue = (BMap<BString, Object>) parentAnnotations.get(annotationKey);
for (BString fieldKey : annotationValue.getKeys()) {
String keyName = fieldKey.getValue();
if (keyName.endsWith(Constants.NAMESPACE) || keyName.endsWith(Constants.NAME)) {
if (isNamespaceAnnotationKey(keyName) || isNameAnnotationKey(keyName)) {
nsFieldAnnotation.put(fieldKey, annotationValue.get(fieldKey));
break;
}
Expand Down Expand Up @@ -694,7 +692,7 @@ private static BMap<BString, Object> processParentAnnotation(Type type, BMap<BSt
private static String processFieldAnnotation(BMap<BString, Object> annotation, String key) {
for (BString value : annotation.getKeys()) {
String stringValue = value.getValue();
if (stringValue.endsWith(Constants.NAME)) {
if (isNameAnnotationKey(stringValue)) {
BMap<BString, Object> names = (BMap<BString, Object>) annotation.get(value);
String name = names.get(StringUtils.fromString(VALUE)).toString();
if (key.contains(Constants.COLON)) {
Expand All @@ -705,7 +703,7 @@ private static String processFieldAnnotation(BMap<BString, Object> annotation, S
key = name;
}
}
if (stringValue.endsWith(Constants.ATTRIBUTE)) {
if (isAttributeAnnotationKey(stringValue)) {
key = ATTRIBUTE_PREFIX.concat(key);
}
}
Expand All @@ -718,10 +716,10 @@ private static BString processAnnotation(BMap<BString, Object> annotation, Strin
for (BString value : annotation.getKeys()) {
if (!value.getValue().contains(Constants.FIELD)) {
String stringValue = value.getValue();
if (stringValue.endsWith(Constants.NAME)) {
if (isNameAnnotationKey(stringValue)) {
key = processNameAnnotation(annotation, key, value, hasNamespaceAnnotation);
}
if (stringValue.endsWith(Constants.NAMESPACE)) {
if (isNamespaceAnnotationKey(stringValue)) {
hasNamespaceAnnotation = true;
key = processNamespaceAnnotation(annotation, key, value, namespaces);
}
Expand All @@ -733,7 +731,7 @@ private static BString processAnnotation(BMap<BString, Object> annotation, Strin
private static void processSubRecordAnnotation(BMap<BString, Object> annotation, BMap<BString, Object> subRecord) {
BString[] keys = annotation.getKeys();
for (BString value : keys) {
if (value.getValue().endsWith(Constants.NAMESPACE)) {
if (isNamespaceAnnotationKey(value.getValue())) {
processNamespaceAnnotation(annotation, "", value, subRecord);
}
}
Expand All @@ -744,7 +742,7 @@ private static String getElementName(BMap<BString, Object> annotation, String ke
BString[] keys = annotation.getKeys();
boolean hasNamespaceAnnotation = false;
for (BString value : keys) {
if (value.getValue().endsWith(Constants.NAMESPACE)) {
if (isNamespaceAnnotationKey(value.getValue())) {
hasNamespaceAnnotation = true;
BMap<BString, Object> namespaceAnnotation = (BMap<BString, Object>) annotation.get(value);
BString prefix = (BString) namespaceAnnotation.get(Constants.PREFIX);
Expand All @@ -755,7 +753,7 @@ private static String getElementName(BMap<BString, Object> annotation, String ke
}

for (BString value : keys) {
if (value.getValue().endsWith(Constants.NAME)) {
if (isNameAnnotationKey(value.getValue())) {
key = processNameAnnotation(annotation, key, value, hasNamespaceAnnotation);
}
}
Expand Down Expand Up @@ -816,6 +814,18 @@ private static String addAttributeToRecord(BString prefix, BString uri, String k
return prefix.getValue().concat(Constants.COLON).concat(key);
}

private static boolean isNamespaceAnnotationKey(String key) {
return key.startsWith(Constants.MODULE_NAME) && key.endsWith(Constants.NAMESPACE);
}

private static boolean isNameAnnotationKey(String key) {
return key.startsWith(Constants.MODULE_NAME) && key.endsWith(Constants.NAME);
}

private static boolean isAttributeAnnotationKey(String key) {
return key.startsWith(Constants.MODULE_NAME) && key.endsWith(Constants.ATTRIBUTE);
}

/**
* Holds data required for the traversing.
*
Expand Down

0 comments on commit b281d17

Please sign in to comment.