Skip to content

Commit 36ef40e

Browse files
committed
chore: extending trackedId to parameters
Signed-off-by: Pawel Psztyc <[email protected]>
1 parent 3d01408 commit 36ef40e

File tree

5 files changed

+27
-22
lines changed

5 files changed

+27
-22
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/amf-helper-mixin",
33
"description": "A mixin with common functions user by most AMF components to compute AMF values",
4-
"version": "4.5.11",
4+
"version": "4.5.12",
55
"license": "Apache-2.0",
66
"main": "index.js",
77
"module": "index.js",

src/AmfSerializer.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ export declare class AmfSerializer extends AmfHelperMixin(Object) {
2727
shape(object: Shape): ApiShape;
2828
anyShape(object: AnyShape): ApiAnyShape;
2929
/**
30-
* Filters examples that should be rendered for a payload identified by `payloadId`.
30+
* Filters examples that should be rendered for a payload identified by `trackedId`.
3131
*
3232
* This function is copied from old `api-example-generator/ExampleGenerator`.
3333
*/
34-
filterTrackedExamples(examples: Example[], payloadId: string): Example[];
34+
filterTrackedExamples(examples: Example[], trackedId: string): Example[];
3535
/**
3636
* Kind of the opposite of the `filterTrackedExamples`. It gathers examples that only have been
3737
* defined for the parent Shape (ed in the type declaration). It filters out all examples

src/AmfSerializer.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,9 @@ export class AmfSerializer extends AmfHelperMixin(Object) {
206206
const schemas = object[this._getAmfKey(ns.aml.vocabularies.shapes.schema)];
207207
if (Array.isArray(schemas) && schemas.length) {
208208
const [schema] = schemas;
209-
result.schema = this.unknownShape(schema);
209+
result.schema = this.unknownShape(schema, {
210+
trackedId: object['@id'],
211+
});
210212
}
211213
const payloads = object[this._getAmfKey(ns.aml.vocabularies.apiContract.payload)];
212214
if (Array.isArray(payloads) && payloads.length) {
@@ -247,10 +249,10 @@ export class AmfSerializer extends AmfHelperMixin(Object) {
247249
}
248250
// this must be before the ArrayShape
249251
if (types.includes(ns.aml.vocabularies.shapes.TupleShape)) {
250-
return this.tupleShape(/** @type TupleShape */ (object));
252+
return this.tupleShape(/** @type TupleShape */ (object), options);
251253
}
252254
if (types.includes(ns.aml.vocabularies.shapes.ArrayShape) || types.includes(ns.aml.vocabularies.shapes.MatrixShape)) {
253-
return this.arrayShape(/** @type ArrayShape */ (object));
255+
return this.arrayShape(/** @type ArrayShape */ (object), options);
254256
}
255257
if (types.includes(ns.aml.vocabularies.shapes.RecursiveShape)) {
256258
return this.recursiveShape(/** @type RecursiveShape */ (object));
@@ -410,8 +412,8 @@ export class AmfSerializer extends AmfHelperMixin(Object) {
410412
const { ns } = this;
411413
const examples = target[this._getAmfKey(ns.aml.vocabularies.apiContract.examples)];
412414
if (Array.isArray(examples) && examples.length) {
413-
if (options.payloadId) {
414-
const filtered = this.filterTrackedExamples(examples, options.payloadId);
415+
if (options.trackedId) {
416+
const filtered = this.filterTrackedExamples(examples, options.trackedId);
415417
result.examples = filtered.map((item) => this.example(item));
416418
} else {
417419
const filtered = this.filterNonTrackedExamples(examples);
@@ -431,19 +433,19 @@ export class AmfSerializer extends AmfHelperMixin(Object) {
431433
}
432434

433435
/**
434-
* Filters examples that should be rendered for a payload identified by `payloadId`.
436+
* Filters examples that should be rendered for a payload identified by `trackedId`.
435437
*
436438
* This function is copied from old `api-example-generator/ExampleGenerator`.
437439
*
438440
* @param {Example[]} examples
439-
* @param {string} payloadId
441+
* @param {string} trackedId
440442
* @returns {Example[]}
441443
*/
442-
filterTrackedExamples(examples, payloadId) {
444+
filterTrackedExamples(examples, trackedId) {
443445
const { docSourceMaps } = this.ns.raml.vocabularies;
444446
const sourceKey = this._getAmfKey(docSourceMaps.sources);
445447
const trackedKey = this._getAmfKey(docSourceMaps.trackedElement);
446-
const longId = payloadId.indexOf('amf') === -1 ? `amf://id${payloadId}` : payloadId;
448+
const longId = trackedId.indexOf('amf') === -1 ? `amf://id${trackedId}` : trackedId;
447449
return examples.filter((item) => {
448450
let example = item;
449451
if (Array.isArray(example)) {
@@ -468,7 +470,7 @@ export class AmfSerializer extends AmfHelperMixin(Object) {
468470
return true;
469471
}
470472
const ids = value.split(',');
471-
if (ids.indexOf(longId) !== -1 || ids.indexOf(payloadId) !== -1) {
473+
if (ids.indexOf(longId) !== -1 || ids.indexOf(trackedId) !== -1) {
472474
return true;
473475
}
474476
return false;
@@ -675,7 +677,8 @@ export class AmfSerializer extends AmfHelperMixin(Object) {
675677
const anyOf = /** @type Shape[] */ (object[this._getAmfKey(this.ns.aml.vocabularies.shapes.anyOf)]);
676678
const result = /** @type ApiUnionShape */ (this.anyShape(object, options));
677679
if (Array.isArray(anyOf) && anyOf.length) {
678-
result.anyOf = anyOf.map((shape) => this.unknownShape(shape));
680+
const opt = { ...options, trackedId: undefined };
681+
result.anyOf = anyOf.map((shape) => this.unknownShape(shape, opt));
679682
} else {
680683
result.anyOf = [];
681684
}
@@ -811,11 +814,12 @@ export class AmfSerializer extends AmfHelperMixin(Object) {
811814

812815
/**
813816
* @param {ArrayShape} object
817+
* @param {ShapeProcessingOptions=} options
814818
* @returns {ApiArrayShape}
815819
*/
816-
arrayShape(object) {
820+
arrayShape(object, options={}) {
817821
let target = object;
818-
const result = /** @type ApiArrayShape */ (this.dataArrangeShape(target));
822+
const result = /** @type ApiArrayShape */ (this.dataArrangeShape(target, options));
819823
if (this.isLink(target)) {
820824
const value = /** @type ArrayShape */ (this.getLinkTarget(target));
821825
if (value) {
@@ -833,11 +837,12 @@ export class AmfSerializer extends AmfHelperMixin(Object) {
833837

834838
/**
835839
* @param {TupleShape} object
840+
* @param {ShapeProcessingOptions=} options
836841
* @returns {ApiTupleShape}
837842
*/
838-
tupleShape(object) {
843+
tupleShape(object, options) {
839844
let target = object;
840-
const result = /** @type ApiTupleShape */ (this.dataArrangeShape(target));
845+
const result = /** @type ApiTupleShape */ (this.dataArrangeShape(target, options));
841846
if (this.isLink(target)) {
842847
const value = /** @type TupleShape */ (this.getLinkTarget(target));
843848
if (value) {
@@ -1445,7 +1450,7 @@ export class AmfSerializer extends AmfHelperMixin(Object) {
14451450
[schema] = schema;
14461451
}
14471452
result.schema = this.unknownShape(schema, {
1448-
payloadId: result.id,
1453+
trackedId: result.id,
14491454
});
14501455
}
14511456
const examples = object[this._getAmfKey(ns.aml.vocabularies.apiContract.examples)];

src/types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,13 +510,13 @@ export interface ApiParametrizedTrait extends ApiParametrizedDeclaration {
510510

511511
export interface ShapeProcessingOptions {
512512
/**
513-
* This is set when serializing a payload.
513+
* This is set when serializing a shape / parameter.
514514
* It is used to determine which example of the schema to include.
515515
*
516516
* When an example has the `tracked-element` in the source maps then this
517517
* is used to determine the only examples included to the schema.
518518
*
519519
* Note, the value of the tracked-element can be a list of IDs separated by coma.
520520
*/
521-
payloadId?: string;
521+
trackedId?: string;
522522
}

0 commit comments

Comments
 (0)