Skip to content

Commit dcf5e84

Browse files
author
Chris Wiechmann
committed
Support for XML-Declaration added
1 parent e705ee2 commit dcf5e84

File tree

4 files changed

+56
-16
lines changed

4 files changed

+56
-16
lines changed

api-builder-plugin-fn-xml-node/src/flow-nodes.yml

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ flow-nodes:
22
xml-node:
33
name: XML
44
icon: xml-node-icon.png
5-
description: Provides support to handle XML-Payload
5+
description: 'Provides support to handle XML-Payload. To learn more please read: https://github.com/nashwaan/xml-js'
66
category: Utils
77
methods:
88
xml2json:
99
name: XML to JSON
10-
description: Converts XML payload into JSON data.
10+
description: 'Converts XML payload into JSON data. To learn more please read: https://github.com/nashwaan/xml-js'
1111
parameters:
1212
xmlData:
1313
name: XML Input data
@@ -84,17 +84,20 @@ flow-nodes:
8484

8585
json2xml:
8686
name: JSON to XML
87-
description: Converts JSON payload into XML.
87+
description: 'Converts JSON payload into XML. To learn more please read: https://github.com/nashwaan/xml-js'
8888
parameters:
8989
jsonData:
9090
name: JSON Input data
9191
description: 'JSON Data.'
9292
required: true
93-
schema:
94-
type: string
93+
schema:
94+
oneOf: [
95+
{ type: 'string' },
96+
{ type: 'object' }
97+
]
9598
asString:
9699
name: As string
97-
description: Wether return the JSON as String or as an Object. Defaults to false.
100+
description: Wether return the generated JSON as a String or as an Object. By default a Javascript Object is returned.
98101
required: false
99102
initialType: boolean
100103
schema:
@@ -103,7 +106,6 @@ flow-nodes:
103106
spaces:
104107
name: Spaces
105108
description: 'Number of spaces to be used for indenting XML output. Passing characters like " " or "\t" are also accepted.'
106-
group: Standard Options
107109
required: false
108110
initialType: number
109111
schema:
@@ -114,12 +116,18 @@ flow-nodes:
114116
compact:
115117
name: Compact
116118
description: 'Whether the input object is in compact form or not. By default, input is expected to be in compact form. IMPORTANT: Remember to set this option compact: false if you are supplying non-compact json.'
117-
group: Standard Options
118119
required: false
119120
initialType: boolean
120121
schema:
121122
default: true
122123
type: boolean
124+
declaration:
125+
name: Declaration
126+
description: 'XML declaration attributes to use. For instance { "version": "1.0", "encoding": "utf-8" }. The XML-Declaration might also be provided using the attribute _declaration in your JSON-Data.'
127+
required: false
128+
initialType: object
129+
schema:
130+
type: object
123131
fullTagEmptyElement:
124132
name: Full tag empty element
125133
description: 'Whether to produce element without sub-elements as full tag pairs <a></a> rather than self closing tag <a/>'

api-builder-plugin-fn-xml-node/src/json2xml.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ var jp = require('jsonpath');
2121
* does not define "next", the first defined output).
2222
*/
2323
async function json2xml(params, options) {
24-
const { jsonData, spaces, compact, fullTagEmptyElement, indentCdata, indentAttributes, ignoreDeclaration, ignoreInstruction, ignoreAttributes, ignoreComment, ignoreCdata, ignoreDoctype, ignoreText } = params;
24+
const { spaces, declaration, compact, fullTagEmptyElement, indentCdata, indentAttributes, ignoreDeclaration, ignoreInstruction, ignoreAttributes, ignoreComment, ignoreCdata, ignoreDoctype, ignoreText } = params;
25+
let { jsonData} = params;
2526
const { logger } = options;
2627
if (!jsonData) {
2728
throw new Error('Missing required parameter: jsonData');
2829
}
29-
debugger;
3030

3131
const json2xmlOptions = {
3232
spaces: spaces ? spaces : 0,
@@ -45,14 +45,20 @@ async function json2xml(params, options) {
4545
};
4646

4747
let result;
48+
debugger;
4849
try {
49-
if(jsonData instanceof Object) {
50-
logger.debug('Converting given JSON Object into XML.');
51-
result = convert.js2xml(jsonData, json2xmlOptions);
52-
} else {
53-
logger.debug('Converting given JSON String into XML.');
54-
result = convert.json2xml(jsonData, json2xmlOptions);
50+
// Convert String into an object
51+
if (typeof jsonData === 'string' || jsonData instanceof String) {
52+
jsonData = JSON.parse(jsonData);
53+
}
54+
if(Object.keys(jsonData).length === 0) {
55+
throw new Error(`Error creating XML from JSON, as the given Javascript object is empty.`);
56+
}
57+
if(declaration) {
58+
jsonData = Object.assign({_declaration: { _attributes: declaration } }, jsonData);
5559
}
60+
logger.debug('Converting given JSON Object into XML.');
61+
result = convert.js2xml(jsonData, json2xmlOptions);
5662
} catch (e) {
5763
logger.error(e.message);
5864
throw new Error(`Failed to convert JSON into XML. Error: ${e.message}`);

api-builder-plugin-fn-xml-node/test/json2xmlTest.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,24 @@ describe('json2xmlTest', () => {
5050
.and.to.have.property('message');
5151
expect(value.message).to.include('Failed to convert JSON into XML. Error: The JSON structure is invalid');
5252
});
53+
54+
it('should fail with an empty Javascript object', async () => {
55+
const { value, output } = await flowNode.json2xml({ jsonData: {} });
56+
57+
expect(output).to.equal('error');
58+
expect(value).to.be.instanceOf(Error)
59+
.and.to.have.property('message');
60+
expect(value.message).to.include('Error creating XML from JSON, as the given Javascript object is empty.');
61+
});
62+
63+
it('should include the given XML declaration', async () => {
64+
var xmlMessage = require('fs').readFileSync('./test/testMessages/basic_message-no-attributes-incl-declaration.xml', 'utf8');
65+
var jsonMessage = require('fs').readFileSync('./test/testMessages/basic_message.json', 'utf8');
66+
const { value, output } = await flowNode.json2xml({ jsonData: jsonMessage, spaces: '\t', declaration: { version: "1.0", encoding: "utf-8" } });
67+
68+
expect(output).to.equal('next');
69+
expect(value).to.be.a('string');
70+
expect(value).to.deep.equal(xmlMessage);
71+
});
5372
});
5473
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<note>
3+
<title>Happy</title>
4+
<todo>Work</todo>
5+
<todo>Play</todo>
6+
<aBoolean>false</aBoolean>
7+
</note>

0 commit comments

Comments
 (0)