Skip to content

Commit

Permalink
Fix incorrect xml when field name with underscore
Browse files Browse the repository at this point in the history
  • Loading branch information
prakanth97 committed Jul 4, 2024
1 parent 532f953 commit 60fad59
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
26 changes: 26 additions & 0 deletions ballerina/tests/toXml_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -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(), "<Source><RequestorID ID=\"1\" ID_Context=\"2\" Type=\"3\"/></Source>");
}

@test:Config {
groups: ["toXml"]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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$$";
}
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ private static void addNamespaceToSubRecord(String key, BMap<BString, Object> na
private static QName addFieldNamespaceAnnotation(String fieldName, String key, BMap<BString, Object> annotations,
BMap<BString, Object> 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<BString, Object> annotationValue = (BMap<BString, Object>) annotations.get(annotationKey);
Expand Down Expand Up @@ -547,7 +547,7 @@ private static BMap<BString, Object> getFieldNamespaceAndNameAnnotations(String
BMap<BString, Object> 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;
}
Expand Down Expand Up @@ -591,7 +591,7 @@ private static void addPrimitiveValue(QName qName, BMap<BString, Object> 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<BString, Object> currentValue;
if (record.containsKey(key)) {
currentValue = (BMap<BString, Object>) record.get(key);
Expand Down Expand Up @@ -648,7 +648,7 @@ private static void processArray(Type childType, BMap<BString, Object> annotatio
@SuppressWarnings("unchecked")
private static String getKeyNameFromAnnotation(BMap<BString, Object> 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<BString, Object> annotationValue = (BMap<BString, Object>) annotations.get(annotationKey);
return processFieldAnnotation(annotationValue, keyName);
Expand Down

0 comments on commit 60fad59

Please sign in to comment.