From 60fad59d7dc18836f2f45cd40ed9a31a6636ab8d Mon Sep 17 00:00:00 2001
From: prakanth <50439067+prakanth97@users.noreply.github.com>
Date: Thu, 4 Jul 2024 22:08:32 +0530
Subject: [PATCH] Fix incorrect xml when field name with underscore
---
ballerina/tests/toXml_test.bal | 26 +++++++++++++++++++
.../lib/data/xmldata/utils/Constants.java | 2 +-
.../lib/data/xmldata/utils/DataUtils.java | 8 +++---
3 files changed, 31 insertions(+), 5 deletions(-)
diff --git a/ballerina/tests/toXml_test.bal b/ballerina/tests/toXml_test.bal
index 76e9338..374b734 100644
--- a/ballerina/tests/toXml_test.bal
+++ b/ballerina/tests/toXml_test.bal
@@ -1221,6 +1221,32 @@ isolated function testXmlToRecordWithNamespaceAttachedToFields() returns error?
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(), "");
+}
+
@test:Config {
groups: ["toXml"]
}
diff --git a/native/src/main/java/io/ballerina/lib/data/xmldata/utils/Constants.java b/native/src/main/java/io/ballerina/lib/data/xmldata/utils/Constants.java
index 2e3e477..bbf227b 100644
--- a/native/src/main/java/io/ballerina/lib/data/xmldata/utils/Constants.java
+++ b/native/src/main/java/io/ballerina/lib/data/xmldata/utils/Constants.java
@@ -58,6 +58,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$$";
}
diff --git a/native/src/main/java/io/ballerina/lib/data/xmldata/utils/DataUtils.java b/native/src/main/java/io/ballerina/lib/data/xmldata/utils/DataUtils.java
index e124c4e..1f3a4f6 100644
--- a/native/src/main/java/io/ballerina/lib/data/xmldata/utils/DataUtils.java
+++ b/native/src/main/java/io/ballerina/lib/data/xmldata/utils/DataUtils.java
@@ -512,7 +512,7 @@ private static void addNamespaceToSubRecord(String key, BMap na
private static QName addFieldNamespaceAnnotation(String fieldName, String key, BMap annotations,
BMap recordValue) {
BString annotationKey = StringUtils.fromString(Constants.FIELD
- + (fieldName.replaceAll(Constants.NON_NUMERIC_STRING_REGEX, "\\\\$0")));
+ + (fieldName.replaceAll(Constants.RECORD_FIELD_NAME_ESCAPE_CHAR_REGEX, "\\\\$0")));
boolean isAttributeField = isAttributeField(annotationKey, annotations);
if (annotations.containsKey(annotationKey)) {
BMap annotationValue = (BMap) annotations.get(annotationKey);
@@ -547,7 +547,7 @@ private static BMap getFieldNamespaceAndNameAnnotations(String
BMap nsFieldAnnotation = ValueCreator.createMapValue(Constants.JSON_MAP_TYPE);
BString annotationKey =
StringUtils.fromString((Constants.FIELD
- + (key.replaceAll(Constants.NON_NUMERIC_STRING_REGEX, "\\\\$0"))));
+ + (key.replaceAll(Constants.RECORD_FIELD_NAME_ESCAPE_CHAR_REGEX, "\\\\$0"))));
if (!parentAnnotations.containsKey(annotationKey)) {
return nsFieldAnnotation;
}
@@ -591,7 +591,7 @@ private static void addPrimitiveValue(QName qName, BMap annotat
BString key = qName.getPrefix().isBlank() ?
localPart : StringUtils.fromString(qName.getPrefix() + ":" + localPart);
BString annotationKey = StringUtils.fromString(Constants.FIELD
- + (localPart.getValue().replaceAll(Constants.NON_NUMERIC_STRING_REGEX, "\\\\$0")));
+ + (localPart.getValue().replaceAll(Constants.RECORD_FIELD_NAME_ESCAPE_CHAR_REGEX, "\\\\$0")));
BMap currentValue;
if (record.containsKey(key)) {
currentValue = (BMap) record.get(key);
@@ -648,7 +648,7 @@ private static void processArray(Type childType, BMap annotatio
@SuppressWarnings("unchecked")
private static String getKeyNameFromAnnotation(BMap annotations, String keyName) {
BString annotationKey = StringUtils.fromString(Constants.FIELD
- + (keyName.replaceAll(Constants.NON_NUMERIC_STRING_REGEX, "\\\\$0")));
+ + (keyName.replaceAll(Constants.RECORD_FIELD_NAME_ESCAPE_CHAR_REGEX, "\\\\$0")));
if (annotations.containsKey(annotationKey)) {
BMap annotationValue = (BMap) annotations.get(annotationKey);
return processFieldAnnotation(annotationValue, keyName);