Skip to content

Commit c23c10a

Browse files
authored
Merge pull request #50 from advanced-rest-client/fix/W-11423667/xml-prefix
[W-11423667] xml prefix
2 parents 3bc0461 + e6b4e6f commit c23c10a

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@api-components/api-example-generator",
33
"description": "Examples generator from AMF model",
4-
"version": "4.4.18",
4+
"version": "4.4.19",
55
"license": "Apache-2.0",
66
"main": "index.js",
77
"module": "index.js",

src/ExampleGenerator.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,7 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
10861086
name,
10871087
opts.properties
10881088
);
1089-
this._xmlProcessDataProperty(doc, main, item, xmlName || name, isWrapped);
1089+
this._xmlProcessDataProperty(doc, main, item, xmlName || name, { isWrapped });
10901090
}
10911091
const s = new XMLSerializer();
10921092
let value = s.serializeToString(doc);
@@ -1576,6 +1576,14 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
15761576
return undefined;
15771577
}
15781578

1579+
_nameTagWithPrefix(name, prefix) {
1580+
let type = normalizeXmlTagName(name);
1581+
if (prefix) {
1582+
type = `${prefix}:${type}`
1583+
}
1584+
return type
1585+
}
1586+
15791587
/**
15801588
* Computes example from a range's properties for XML media type.
15811589
*
@@ -1587,14 +1595,13 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
15871595
* @return {String}
15881596
*/
15891597
_xmlExampleFromProperties(properties, typeName, parentType, xmlNamespace, xmlPrefix) {
1590-
const type = normalizeXmlTagName(typeName);
1598+
const type = this._nameTagWithPrefix(typeName, xmlPrefix)
15911599
let parent = parentType;
15921600
if (parent) {
15931601
parent = normalizeXmlTagName(parent);
15941602
}
1595-
const namespace = xmlNamespace ? `${xmlPrefix}:${xmlNamespace}` : '';
15961603
const doc = document.implementation.createDocument(
1597-
namespace,
1604+
xmlNamespace,
15981605
parent || type,
15991606
null
16001607
);
@@ -1646,6 +1653,7 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
16461653
isWrapped = false,
16471654
xmlAttribute,
16481655
xmlName,
1656+
xmlPrefix
16491657
} = this._computeXmlSerializationData(serialization);
16501658
const eKey = this._getAmfKey(this.ns.aml.vocabularies.apiContract.examples);
16511659
const examples = this._ensureArray(range[eKey]);
@@ -1657,7 +1665,7 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
16571665
this.ns.w3.shacl.name
16581666
));
16591667
}
1660-
this._xmlFromExamples(doc, node, examples[0], name);
1668+
this._xmlFromExamples(doc, node, examples[0], name, { xmlPrefix });
16611669
return;
16621670
}
16631671
if (this._hasType(range, this.ns.aml.vocabularies.shapes.UnionShape)) {
@@ -1685,7 +1693,7 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
16851693
return;
16861694
}
16871695
if (this._hasType(range, this.ns.aml.vocabularies.shapes.ArrayShape)) {
1688-
this._appendXmlArray(doc, node, range, { isWrapped, xmlName });
1696+
this._appendXmlArray(doc, node, range, { isWrapped, xmlName, xmlPrefix });
16891697
return;
16901698
}
16911699
this._appendXmlElement(doc, node, range);
@@ -1700,8 +1708,9 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
17001708
* @param {Element} node A node to which append values
17011709
* @param {Object} example AMF's example definition.
17021710
* @param {String} propertyName Name of the property being processed
1711+
* @param {XmlData=} xmlData XMLData
17031712
*/
1704-
_xmlFromExamples(doc, node, example, propertyName) {
1713+
_xmlFromExamples(doc, node, example, propertyName, xmlData) {
17051714
const sKey = this._getAmfKey(
17061715
this.ns.aml.vocabularies.document.structuredValue
17071716
);
@@ -1712,7 +1721,7 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
17121721
if (!structure) {
17131722
return;
17141723
}
1715-
this._xmlProcessDataProperty(doc, node, structure, propertyName);
1724+
this._xmlProcessDataProperty(doc, node, structure, propertyName, xmlData);
17161725
}
17171726

17181727
/**
@@ -1769,10 +1778,13 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
17691778
* @return {Element|null} Newly created element
17701779
*/
17711780
_appendXmlElement(doc, node, range, xmlData = {}) {
1772-
const name = xmlData.xmlName || this._getXmlNormalizedName(range);
1781+
let name = xmlData.xmlName || this._getXmlNormalizedName(range);
17731782
if (!name) {
17741783
return null;
17751784
}
1785+
if (xmlData.xmlPrefix) {
1786+
name = xmlData.xmlPrefix + ':' + name
1787+
}
17761788
let nodeValue = this._getValue(range, this.ns.w3.shacl.defaultValueStr);
17771789
if (!nodeValue) {
17781790
const eKey = this._getAmfKey(
@@ -1885,13 +1897,14 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
18851897
* @param {Element} node Current node
18861898
* @param {Object} property AMF property
18871899
* @param {string} name Current property name
1888-
* @param {Boolean} isWrapped Whether the `wrapped` property is set.
1900+
* @param {XmlData=} xmlData XMLData
18891901
*/
1890-
_xmlProcessDataProperty(doc, node, property, name, isWrapped = false) {
1902+
_xmlProcessDataProperty(doc, node, property, name, xmlData = {}) {
1903+
const isWrapped = xmlData.isWrapped || false;
18911904
if (!property || !name) {
18921905
return;
18931906
}
1894-
const tagName = normalizeXmlTagName(name);
1907+
const tagName = this._nameTagWithPrefix(name, xmlData.xmlPrefix);
18951908
const arrayProperty = this._hasType(
18961909
property,
18971910
this.ns.aml.vocabularies.data.Array

test/xml-data-processing.test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,13 @@ describe('XML processing', () => {
480480
const shape = AmfLoader.lookupType(amf, 'nexusInputMessage');
481481
const result = element.computeExamples(shape, 'application/xml');
482482
assert.lengthOf(result, 1);
483-
assert.include(result[0].value, 'xmlns="t0:http://xmlns.oracle.com/CSAAS/OPTYNexusToSFDCBPELProcess/Schema"');
483+
const example = '<?xml version="1.0" encoding="UTF-8"?>\n' +
484+
'<t0:nexusInputMessage xmlns:t0="http://xmlns.oracle.com/CSAAS/OPTYNexusToSFDCBPELProcess/Schema">\n' +
485+
' <t0:nexusInputType>\n' +
486+
' <t0:Primary_Contact_c>0031U00001ykLV7QAM</t0:Primary_Contact_c>\n' +
487+
' </t0:nexusInputType>\n' +
488+
'</t0:nexusInputMessage>\n';
489+
assert.equal(result[0].value, example);
484490
});
485491
});
486492
});

0 commit comments

Comments
 (0)