@api-components/api-example-generator
Advanced tools
Comparing version
{ | ||
"name": "@api-components/api-example-generator", | ||
"description": "Examples generator from AMF model", | ||
"version": "4.4.11", | ||
"version": "4.4.12", | ||
"license": "Apache-2.0", | ||
@@ -6,0 +6,0 @@ "main": "index.js", |
@@ -37,2 +37,9 @@ import { AmfHelperMixin } from '@api-components/amf-helper-mixin/amf-helper-mixin.js'; | ||
/** | ||
* @typedef {Object} XmlData | ||
* @property {Boolean=} isWrapped Value of 'xmlWrapped' property of AMF's object | ||
* @property {String=} xmlName Value of 'xmlName' property of AMF's object | ||
* @property {Boolean=} xmlAttribute Value of 'xmlAttribute' property of AMF's object | ||
*/ | ||
/** | ||
* Reads property name from AMF's "data" item. The key is an object key | ||
@@ -968,8 +975,35 @@ * that has a form of "data uri#property name" or "data:property name". | ||
* Generates XML example string value from AMF's structured value definition. | ||
* @param {Object} serialization Value of the `serialization` property of AMF's object. | ||
* @return {XmlData} | ||
*/ | ||
_computeXmlSerializationData(serialization) { | ||
if (!serialization) { | ||
return {}; | ||
} | ||
if (serialization instanceof Array) { | ||
serialization = serialization[0]; | ||
} | ||
const isWrapped = /** @type boolean */ (this._getValue( | ||
serialization, | ||
this.ns.aml.vocabularies.shapes.xmlWrapped | ||
)); | ||
const xmlName = this._getValue( | ||
serialization, | ||
this.ns.aml.vocabularies.shapes.xmlName | ||
); | ||
const xmlAttribute = this._getValue( | ||
serialization, | ||
this.ns.aml.vocabularies.shapes.xmlAttribute | ||
); | ||
return { isWrapped, xmlName, xmlAttribute }; | ||
} | ||
/** | ||
* Generates XML example string value from AMF's structured value definition. | ||
* @param {Object} property AMF property | ||
* @param {string} name Current property name | ||
* @property {Array<Object>} properties Value of the `property` property of AMF's object. | ||
* @return {Boolean} | ||
* @return {XmlData} | ||
*/ | ||
_computeIsWrapped(property, name, properties) { | ||
_computeXmlData(property, name, properties) { | ||
const arrayProperty = this._hasType( | ||
@@ -980,3 +1014,3 @@ property, | ||
if (!arrayProperty) { | ||
return false; | ||
return {}; | ||
} | ||
@@ -988,3 +1022,3 @@ | ||
if (!propertySchema) { | ||
return false; | ||
return {}; | ||
} | ||
@@ -995,3 +1029,3 @@ | ||
if (!range) { | ||
return false; | ||
return {}; | ||
} | ||
@@ -1005,10 +1039,4 @@ | ||
); | ||
let serialization = range[sKey]; | ||
if (serialization instanceof Array) { | ||
serialization = serialization[0]; | ||
} | ||
return /** @type boolean */ (this._getValue( | ||
serialization, | ||
this.ns.aml.vocabularies.shapes.xmlWrapped | ||
)); | ||
const serialization = range[sKey]; | ||
return this._computeXmlSerializationData(serialization); | ||
} | ||
@@ -1041,4 +1069,8 @@ | ||
const name = dataNameFromKey(key); | ||
const isWrapped = this._computeIsWrapped(item, name, opts.properties); | ||
this._xmlProcessDataProperty(doc, main, item, name, isWrapped); | ||
const { isWrapped, xmlName } = this._computeXmlData( | ||
item, | ||
name, | ||
opts.properties | ||
); | ||
this._xmlProcessDataProperty(doc, main, item, xmlName || name, isWrapped); | ||
} | ||
@@ -1542,10 +1574,12 @@ const s = new XMLSerializer(); | ||
} | ||
const { | ||
isWrapped = false, | ||
xmlAttribute, | ||
xmlName, | ||
} = this._computeXmlSerializationData(serialization); | ||
const eKey = this._getAmfKey(this.ns.aml.vocabularies.apiContract.examples); | ||
const examples = this._ensureArray(range[eKey]); | ||
if (examples && examples.length) { | ||
let name = /** @type {string} */ (this._getValue( | ||
serialization, | ||
this.ns.aml.vocabularies.shapes.xmlName | ||
)); | ||
if (!name) { | ||
let name = xmlName; | ||
if (!xmlName) { | ||
name = /** @type {string} */ (this._getValue( | ||
@@ -1573,16 +1607,7 @@ range, | ||
} | ||
let isWrapped = false; | ||
if (serialization) { | ||
const isAtribute = this._getValue( | ||
serialization, | ||
this.ns.aml.vocabularies.shapes.xmlAttribute | ||
); | ||
if (isAtribute) { | ||
this._appendXmlAttribute(node, range, serialization); | ||
if (xmlAttribute) { | ||
this._appendXmlAttribute(node, range, xmlName); | ||
return; | ||
} | ||
isWrapped = /** @type {boolean} */ (this._getValue( | ||
serialization, | ||
this.ns.aml.vocabularies.shapes.xmlWrapped | ||
)); | ||
} | ||
@@ -1594,3 +1619,3 @@ if (this._hasType(range, this.ns.w3.shacl.NodeShape)) { | ||
if (this._hasType(range, this.ns.aml.vocabularies.shapes.ArrayShape)) { | ||
this._appendXmlArray(doc, node, range, isWrapped); | ||
this._appendXmlArray(doc, node, range, { isWrapped, xmlName }); | ||
return; | ||
@@ -1647,9 +1672,6 @@ } | ||
* @param {Object} range AMF range | ||
* @param {Object} serialization Serialization info | ||
* @param {String} xmlName Value of 'xmlName' property of AMF's object | ||
*/ | ||
_appendXmlAttribute(node, range, serialization) { | ||
let name = /** @type {string} */ (this._getValue( | ||
serialization, | ||
this.ns.aml.vocabularies.shapes.xmlName | ||
)); | ||
_appendXmlAttribute(node, range, xmlName) { | ||
let name = /** @type {string} */ xmlName; | ||
if (!name) { | ||
@@ -1679,6 +1701,7 @@ name = /** @type {string} */ (this._getValue( | ||
* @param {Object} range AMF range | ||
* @param {XmlData} xmlData XmlData | ||
* @return {Element|null} Newly created element | ||
*/ | ||
_appendXmlElement(doc, node, range) { | ||
const name = this._getXmlNormalizedName(range); | ||
_appendXmlElement(doc, node, range, xmlData = {}) { | ||
const name = xmlData.xmlName || this._getXmlNormalizedName(range); | ||
if (!name) { | ||
@@ -1752,7 +1775,7 @@ return null; | ||
* @param {Object} range AMF range | ||
* @param {Boolean} isWrapped Whether RAML's `wrapped` property is set. | ||
* @param {XmlData} xmlData XMLData | ||
*/ | ||
_appendXmlArray(doc, node, range, isWrapped) { | ||
_appendXmlArray(doc, node, range, xmlData) { | ||
let processNode = node; | ||
const element = this._appendXmlElement(doc, processNode, range); | ||
const element = this._appendXmlElement(doc, processNode, range, xmlData); | ||
processNode.appendChild(element); | ||
@@ -1768,4 +1791,4 @@ processNode = element; | ||
const prop = properties[i]; | ||
if (isWrapped) { | ||
const name = this._getXmlNormalizedName(prop); | ||
if (xmlData.isWrapped) { | ||
const name = xmlData.xmlName || this._getXmlNormalizedName(prop); | ||
if (!name) { | ||
@@ -1772,0 +1795,0 @@ continue; |
200744
0.36%2746
0.77%