Skip to content

Commit 931e910

Browse files
a-rasinAndreas Naziris
and
Andreas Naziris
authored
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 <a>(first)(second)</a> ``` But I get: ```xml <a> <a>(first)</a> <a>(second)</a> </a> ``` This commit fixes that issue. Co-authored-by: Andreas Naziris <[email protected]>
1 parent b8e40c8 commit 931e910

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

Diff for: spec/j2x_spec.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,20 @@ describe("XMLBuilder", function() {
482482
const expected = `<a><b>1</b><b>2</b></a>`;
483483
expect(result).toEqual(expected);
484484
});
485-
485+
486+
it("should correctly handle values with oneListGroup", function() {
487+
const jObj = {
488+
"a": [
489+
"(first)",
490+
"(second)"
491+
],
492+
};
493+
const builder = new XMLBuilder({oneListGroup:"true", attributesGroupName: "@"});
494+
const result = builder.build(jObj);
495+
const expected = `<a>(first)(second)</a>`;
496+
expect(result).toEqual(expected);
497+
});
498+
486499
it('should build tag with only text node', async () => {
487500
const schema_obj = {
488501
field: {

Diff for: src/xmlbuilder/json2xml.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,13 @@ Builder.prototype.j2x = function(jObj, level) {
130130
listTagVal += this.processTextOrObjNode(item, key, level)
131131
}
132132
} else {
133-
listTagVal += this.buildTextValNode(item, key, '', level);
133+
if (this.options.oneListGroup) {
134+
let textValue = this.options.tagValueProcessor(key, item);
135+
textValue = this.replaceEntitiesValue(textValue);
136+
listTagVal += textValue;
137+
} else {
138+
listTagVal += this.buildTextValNode(item, key, '', level);
139+
}
134140
}
135141
}
136142
if(this.options.oneListGroup){

0 commit comments

Comments
 (0)