Skip to content

Commit 2866595

Browse files
authored
Merge pull request #63 from advanced-rest-client/fix/W-14846319/Imported-Schema-or-XML-Example-does-not-show-in-Documentation-Panel
[W-14846319] Imported Schema or XML Example does not show in Documentation Panel
2 parents 54db28c + f0942db commit 2866595

File tree

3 files changed

+58
-20
lines changed

3 files changed

+58
-20
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.26",
4+
"version": "4.4.27",
55
"license": "Apache-2.0",
66
"main": "index.js",
77
"module": "index.js",

src/ExampleGenerator.js

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ export const formatXml = xml => {
9494
const type = single
9595
? 'single'
9696
: closing
97-
? 'closing'
98-
: opening
99-
? 'opening'
100-
: 'other';
97+
? 'closing'
98+
: opening
99+
? 'opening'
100+
: 'other';
101101
const fromTo = lastType + '->' + type;
102102
lastType = type;
103103
let padding = '';
@@ -395,7 +395,7 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
395395
}
396396
this._resolve(schema);
397397
const xmlSerialization = this.xmlSerialization(schema);
398-
const {xmlName, xmlNamespace, xmlPrefix} = this._computeXmlSerializationData(xmlSerialization)
398+
const { xmlName, xmlNamespace, xmlPrefix } = this._computeXmlSerializationData(xmlSerialization)
399399
if (!options.typeName) {
400400
const typeName = this.computeTypeName(schema, xmlName);
401401
if (typeName) {
@@ -650,6 +650,44 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
650650
const referenceIdData = this._ensureArray(example[referenceIdKey]);
651651
if (Array.isArray(referenceIdData) && referenceIdData.length > 0) {
652652
raw = (this._getValue(referenceIdData[0], this.ns.aml.vocabularies.document.raw));
653+
654+
// Map raw to external fragment
655+
if (!raw) {
656+
// It first retrieves the @id property from the first element
657+
// of referenceIdData array and assigns it to referenceId.
658+
const referenceId = referenceIdData[0]['@id']
659+
660+
// It calls the _computeReferences method with this.amf as an
661+
// argument to get the root references and assigns
662+
// the result to rootReferences.
663+
const rootReferences = this._computeReferences(this.amf)
664+
665+
// It maps over each item in rootReferences,
666+
// and for each item, it computes references twice in a nested manner.
667+
// It then gets the second element from externalFragments and computes its encodes.
668+
// The result of this map operation is an array of
669+
// encoded external fragments, which is assigned to encodesOfExternalFragments.
670+
const encodesOfExternalFragments = rootReferences.map((item) => {
671+
const shapeFragment = this._computeReferences(item)
672+
const externalFragments = this._computeReferences(shapeFragment)
673+
// Get second element from externalFragments
674+
const externalFragmentExample = externalFragments[1]
675+
return this._computeEncodes(externalFragmentExample)
676+
})
677+
678+
// It finds an element in encodesOfExternalFragments where
679+
// the @id property matches referenceId and assigns
680+
// it to exmapleExternalFragmentByReferenceId.
681+
const exmapleExternalFragmentByReferenceId = encodesOfExternalFragments.find(externalFrament => (
682+
externalFrament['@id'] === referenceId
683+
))
684+
685+
const rawKey = this._getAmfKey(this.ns.aml.vocabularies.document.raw)
686+
// Finally, it calls the _getValue method with
687+
// exmapleExternalFragmentByReferenceId and rawKey
688+
// as arguments and assigns the result to raw.
689+
raw = this._getValue(exmapleExternalFragmentByReferenceId, rawKey)
690+
}
653691
}
654692
}
655693
let title = /** @type {string} */ (this._getValue(
@@ -762,11 +800,11 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
762800
if (result.raw) {
763801
try {
764802
result.value = this.computeRaw(raw)
765-
return result
803+
return result
766804
} catch (_) {
767805
// ...
768806
}
769-
}
807+
}
770808

771809
result.value = JSON.stringify(parts, null, 2);
772810
return result;
@@ -807,7 +845,7 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
807845
* @param {String} raw
808846
* @returns string JSON formatted
809847
*/
810-
computeRaw (raw) {
848+
computeRaw(raw) {
811849
const accountEntries = raw.split('-\n');
812850
const accounts = [];
813851
for (const entry of accountEntries) {
@@ -817,15 +855,15 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
817855
for (const line of lines) {
818856
if (line !== '') {
819857
const [key, value] = line.split(': ');
820-
account[key.trim()] = Number(value) ? Number(value) : value.trim()
858+
account[key.trim()] = Number(value) ? Number(value) : value.trim()
821859
}
822860
}
823861
accounts.push(account);
824862
}
825863
}
826864
return JSON.stringify(accounts, null, 2);
827865
}
828-
866+
829867

830868
/**
831869
* Computes list of examples for an array shape.
@@ -1114,7 +1152,7 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
11141152
* @param {ExampleOptions=} opts Examples processing options
11151153
* @return {String}
11161154
*/
1117-
_xmlFromStructure(structure, opts={}) {
1155+
_xmlFromStructure(structure, opts = {}) {
11181156
let typeName = (opts && opts.typeName) || UNKNOWN_TYPE;
11191157
typeName = normalizeXmlTagName(typeName);
11201158
const doc = document.implementation.createDocument('', typeName, null);
@@ -1310,17 +1348,17 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
13101348
});
13111349
} else {
13121350
const aKey = this._getAmfKey(this.ns.aml.vocabularies.shapes.anyOf);
1313-
let value
1351+
let value
13141352
const anyOf = this._ensureArray(range[aKey]);
1315-
if(anyOf){
1316-
for(let anyOfIndex=0; anyOfIndex<anyOf.length; anyOfIndex++) {
1353+
if (anyOf) {
1354+
for (let anyOfIndex = 0; anyOfIndex < anyOf.length; anyOfIndex++) {
13171355
const exampleValue = this._computeJsonPropertyValue(anyOf[anyOfIndex]);
1318-
if(exampleValue!==null){
1356+
if (exampleValue !== null) {
13191357
value = exampleValue;
13201358
break;
13211359
}
13221360
}
1323-
}else{
1361+
} else {
13241362
value = this._computeJsonPropertyValue(range);
13251363
}
13261364
if (value === undefined) {
@@ -1585,7 +1623,7 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
15851623
examples.push(propertiesExamples);
15861624
}
15871625
}
1588-
return examples.reduce((acc, value) => ({...acc, ...value}), {});
1626+
return examples.reduce((acc, value) => ({ ...acc, ...value }), {});
15891627
}
15901628

15911629
/**
@@ -1772,7 +1810,7 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
17721810
}
17731811
if (serialization) {
17741812
if (xmlAttribute) {
1775-
1813+
17761814
this._appendXmlAttribute(node, range, xmlName, examples[0]);
17771815
return;
17781816
}

0 commit comments

Comments
 (0)