diff --git a/ballerina/tests/test_ref_types_with_annotations.bal b/ballerina/tests/test_ref_types_with_annotations.bal index ef24a3c..6c99568 100644 --- a/ballerina/tests/test_ref_types_with_annotations.bal +++ b/ballerina/tests/test_ref_types_with_annotations.bal @@ -166,3 +166,104 @@ function testTypeRefAnnotations2() returns error? { test:assertTrue(xmlResult is xml:Element); test:assertEquals(xmlResult.toString(), string `1234`); } + +@Name { + value: "root" +} +type TypeRefType3 record { + @Namespace { + uri: "http://www.example.com/", + prefix: "ex" + } + @Name { + value: "A" + } + int a; +}; + +@test:Config +function testTypeRefAnnotations3() returns error? { + TypeRefType3 t = {a: 1}; + xml xmlResult = check toXml(t); + test:assertTrue(xmlResult is xml:Element); + test:assertEquals(xmlResult.toString(), string `1`); +} + +@Namespace { + uri: "http://ballerina.io", + prefix: "a1" +} +@Name { + value: "root" +} +type TypeRefType4 record { + @Name { + value: "nestedA" + } + NestedTypeRefType5 a; + + @Name { + value: "nestedB" + } + NestedTypeRefType6 b; +}; + +@Namespace { + uri: "http://nested-ballerina.io", + prefix: "nestedA" +} +type NestedTypeRefType5 record { + @Name { + value: "A" + } + @Namespace { + uri: "http://nested-ballerina.io/a", + prefix: "a" + } + int a; + + @Namespace { + uri: "http://nested-ballerina.io/a", + prefix: "a" + } + @Name { + value: "B" + } + int b; +}; + +@Namespace { + uri: "http://nested-ballerina.io", + prefix: "nestedB" +} +type NestedTypeRefType6 record { + @Name { + value: "A" + } + @Namespace { + uri: "http://nested-ballerina.io/a", + prefix: "a" + } + int a; + + @Name { + value: "B" + } + @Namespace { + uri: "http://nested-ballerina.io/a", + prefix: "a" + } + int b; +}; + +@test:Config +function testTypeRefAnnotations4() returns error? { + TypeRefType4 t = { + a: {a: 1, b: 2}, + b: {a: 3, b: 4} + }; + + xml xmlResult = check toXml(t); + test:assertTrue(xmlResult is xml:Element); + test:assertEquals(xmlResult.toString(), string `1234`); +} 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 b0a0c28..e1387e6 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 @@ -738,8 +738,10 @@ private static void addPrimitiveValue(QName qName, BMap annotat BString annotationKey = StringUtils.fromString(Constants.FIELD + (localPart.getValue().replaceAll(Constants.RECORD_FIELD_NAME_ESCAPE_CHAR_REGEX, "\\\\$0"))); BMap currentValue; + BString prevKey = key; if (record.containsKey(key)) { currentValue = (BMap) record.get(key); + prevKey = key; key = StringUtils.fromString(contentFieldName); } else { currentValue = record; @@ -747,7 +749,15 @@ private static void addPrimitiveValue(QName qName, BMap annotat if (annotations.containsKey(annotationKey)) { BMap annotationValue = (BMap) annotations.get(annotationKey); - currentValue.put(StringUtils.fromString(processFieldAnnotation(annotationValue, key.getValue())), value); + String keyName = processFieldAnnotation(annotationValue, prevKey.getValue()); + if (key.getValue().equals(contentFieldName)) { + currentValue.put(StringUtils.fromString(contentFieldName), value); + if (!keyName.equals(prevKey.getValue())) { + record.put(StringUtils.fromString(keyName), record.remove(prevKey)); + } + } else { + currentValue.put(StringUtils.fromString(keyName), value); + } } else { currentValue.put(key, value); } diff --git a/native/src/main/java/io/ballerina/lib/data/xmldata/utils/ToXmlUtils.java b/native/src/main/java/io/ballerina/lib/data/xmldata/utils/ToXmlUtils.java index 487bf28..5db8c37 100644 --- a/native/src/main/java/io/ballerina/lib/data/xmldata/utils/ToXmlUtils.java +++ b/native/src/main/java/io/ballerina/lib/data/xmldata/utils/ToXmlUtils.java @@ -140,7 +140,7 @@ value, allNamespaces, getEmptyStringMap(), options, key, getChildElementType( return CreateElement.createElement(rootTagBstring, getEmptyStringMap(), output); } return output; - } catch (BError e) { + } catch (Exception e) { return DiagnosticLog.createXmlError(e.getMessage()); } }