Skip to content

Commit 0ca16cf

Browse files
author
alexperez
committed
feat: implement _computeJsonOrValue method to handle AMF range definitions
1 parent 73e36a7 commit 0ca16cf

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/ExampleGenerator.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,6 +1425,9 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
14251425
return this._computeJsonPropertyValue(oneOfOptions[0], typeName);
14261426
}
14271427
}
1428+
if (this._hasType(range, this.ns.w3.shacl.Shape)) {
1429+
return this._computeJsonOrValue(range);
1430+
}
14281431
return undefined;
14291432
}
14301433

@@ -1649,6 +1652,43 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
16491652
return examples.reduce((acc, value) => ({ ...acc, ...value }), {});
16501653
}
16511654

1655+
/**
1656+
* Computes a JSON object or value from a given range.
1657+
*
1658+
* @param {Object} range AMF's range definition for a shape.
1659+
* @return {any|undefined} A JavaScript object or value computed from the range.
1660+
*/
1661+
_computeJsonOrValue(range) {
1662+
const key = this._getAmfKey(this.ns.w3.shacl.or);
1663+
const list = this._ensureArray(range[key]);
1664+
if (!list) {
1665+
return undefined;
1666+
}
1667+
1668+
const examples = [];
1669+
const anyOfKey = this._getAmfKey(this.ns.aml.vocabularies.shapes.anyOf);
1670+
1671+
// Iterate over all items in the list
1672+
for (const item of list) {
1673+
const properties = this._ensureArray(item[anyOfKey]);
1674+
if (properties) {
1675+
// Iterate over all properties to get all propertiesSchemas
1676+
for (const property of properties) {
1677+
const propertiesSchemas = this._listProperties(property);
1678+
if (propertiesSchemas) {
1679+
const propertiesExamples = this._jsonExampleFromProperties(propertiesSchemas);
1680+
if (propertiesExamples && typeof propertiesExamples === 'object') {
1681+
examples.push(propertiesExamples);
1682+
}
1683+
}
1684+
}
1685+
}
1686+
}
1687+
1688+
// Merge all examples into a single object
1689+
return examples.reduce((acc, value) => ({ ...acc, ...value }), {});
1690+
}
1691+
16521692
/**
16531693
* Computes JSON object as an example from a range that is an object.
16541694
*

0 commit comments

Comments
 (0)