Skip to content

Commit e3b1848

Browse files
authored
fix(ns-asyncapi-2): retain meta & attributes during refracting (#3858)
This change is specific to cases when semantic ApiDOM is refractored from generic ApiDOM. Refs #3842
1 parent 333550e commit e3b1848

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Element, hasElementSourceMap } from '@swagger-api/apidom-core';
1+
import { Element, ObjectElement, deepmerge, hasElementSourceMap } from '@swagger-api/apidom-core';
22

33
export interface VisitorOptions {}
44

@@ -9,13 +9,20 @@ class Visitor {
99
Object.assign(this, options);
1010
}
1111

12-
// eslint-disable-next-line class-methods-use-this
12+
/* eslint-disable class-methods-use-this, no-param-reassign */
1313
public copyMetaAndAttributes(from: Element, to: Element) {
14-
// copy sourcemaps
15-
if (hasElementSourceMap(from)) {
16-
to.meta.set('sourceMap', from.meta.get('sourceMap'));
14+
if (from.meta.length > 0 || to.meta.length > 0) {
15+
to.meta = deepmerge(to.meta, from.meta) as ObjectElement;
16+
if (hasElementSourceMap(from)) {
17+
// avoid deep merging of source maps
18+
to.meta.set('sourceMap', from.meta.get('sourceMap'));
19+
}
20+
}
21+
if (from.attributes.length > 0 || from.meta.length > 0) {
22+
to.attributes = deepmerge(to.attributes, from.attributes) as ObjectElement; // eslint-disable-line no-param-reassign
1723
}
1824
}
25+
/* eslint-enable- class-methods-use-this, no-param-reassign */
1926
}
2027

2128
export default Visitor;

packages/apidom-ns-asyncapi-2/test/refractor/elements/Reference/__snapshots__/index.ts.snap

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`refractor elements ReferenceElement given generic ApiDOM element should refract to semantic ApiDOM tree 1`] = `
4+
(ReferenceElement
5+
(MemberElement
6+
(StringElement)
7+
(StringElement)))
8+
`;
9+
310
exports[`refractor elements ReferenceElement should refract to semantic ApiDOM tree 1`] = `
411
(ReferenceElement
512
(MemberElement

packages/apidom-ns-asyncapi-2/test/refractor/elements/Reference/index.ts

+30-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { expect } from 'chai';
2-
import { sexprs } from '@swagger-api/apidom-core';
1+
import { assert, expect } from 'chai';
2+
import { ObjectElement, sexprs, toValue } from '@swagger-api/apidom-core';
33

44
import { ReferenceElement } from '../../../../src';
55

@@ -13,6 +13,34 @@ describe('refractor', function () {
1313

1414
expect(sexprs(referenceElement)).toMatchSnapshot();
1515
});
16+
17+
context('given generic ApiDOM element', function () {
18+
let referenceElement: ReferenceElement;
19+
20+
beforeEach(function () {
21+
referenceElement = ReferenceElement.refract(
22+
new ObjectElement(
23+
{ $ref: '#/path/to/somewhere' },
24+
{ classes: ['example'] },
25+
{ attr: true },
26+
),
27+
) as ReferenceElement;
28+
});
29+
30+
specify('should refract to semantic ApiDOM tree', function () {
31+
expect(sexprs(referenceElement)).toMatchSnapshot();
32+
});
33+
34+
specify('should deepmerge meta', function () {
35+
assert.deepEqual(toValue(referenceElement.meta), {
36+
classes: ['json-reference', 'asyncapi-reference', 'example', 'reference-element'],
37+
});
38+
});
39+
40+
specify('should deepmerge attributes', function () {
41+
assert.isTrue(referenceElement.attributes.get('attr').equals(true));
42+
});
43+
});
1644
});
1745
});
1846
});

0 commit comments

Comments
 (0)