From 931e91013b6ea6bb7425c3b1758dc50a6dfc730c Mon Sep 17 00:00:00 2001
From: a-rasin <90502898+a-rasin@users.noreply.github.com>
Date: Sun, 7 Jul 2024 14:16:43 +0300
Subject: [PATCH] fix: get oneListGroup to work as expected for array of
strings (#662)
If I have `oneListGroup` enabled I would expect that:
```json
{
a: ['(first)', 'second']
}
```
Would give me:
```xml
(first)(second)
```
But I get:
```xml
(first)
(second)
```
This commit fixes that issue.
Co-authored-by: Andreas Naziris
---
spec/j2x_spec.js | 15 ++++++++++++++-
src/xmlbuilder/json2xml.js | 8 +++++++-
2 files changed, 21 insertions(+), 2 deletions(-)
diff --git a/spec/j2x_spec.js b/spec/j2x_spec.js
index 79ec6ecb..73209f6a 100644
--- a/spec/j2x_spec.js
+++ b/spec/j2x_spec.js
@@ -482,7 +482,20 @@ describe("XMLBuilder", function() {
const expected = `12`;
expect(result).toEqual(expected);
});
-
+
+ it("should correctly handle values with oneListGroup", function() {
+ const jObj = {
+ "a": [
+ "(first)",
+ "(second)"
+ ],
+ };
+ const builder = new XMLBuilder({oneListGroup:"true", attributesGroupName: "@"});
+ const result = builder.build(jObj);
+ const expected = `(first)(second)`;
+ expect(result).toEqual(expected);
+ });
+
it('should build tag with only text node', async () => {
const schema_obj = {
field: {
diff --git a/src/xmlbuilder/json2xml.js b/src/xmlbuilder/json2xml.js
index 5707ee0f..6fe3f60c 100644
--- a/src/xmlbuilder/json2xml.js
+++ b/src/xmlbuilder/json2xml.js
@@ -130,7 +130,13 @@ Builder.prototype.j2x = function(jObj, level) {
listTagVal += this.processTextOrObjNode(item, key, level)
}
} else {
- listTagVal += this.buildTextValNode(item, key, '', level);
+ if (this.options.oneListGroup) {
+ let textValue = this.options.tagValueProcessor(key, item);
+ textValue = this.replaceEntitiesValue(textValue);
+ listTagVal += textValue;
+ } else {
+ listTagVal += this.buildTextValNode(item, key, '', level);
+ }
}
}
if(this.options.oneListGroup){