Skip to content

Commit 7e40336

Browse files
authored
fix(ns-workflows-1): retain meta & attributes during refracting (#3860)
This change is specific to cases when semantic ApiDOM is refractoredfrom generic ApiDOM. Refs #3842
1 parent e3b1848 commit 7e40336

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { hasElementSourceMap, Element } 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-workflows-1/test/refractor/elements/Info/__snapshots__/index.ts.snap

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

3+
exports[`refractor elements InfoElement given generic ApiDOM element should refract to semantic ApiDOM tree 1`] = `(InfoElement)`;
4+
35
exports[`refractor elements InfoElement should refract to semantic ApiDOM tree 1`] = `
46
(InfoElement
57
(MemberElement

packages/apidom-ns-workflows-1/test/refractor/elements/Info/index.ts

+26-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, toValue, sexprs } from '@swagger-api/apidom-core';
33

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

@@ -17,6 +17,30 @@ describe('refractor', function () {
1717

1818
expect(sexprs(infoElement)).toMatchSnapshot();
1919
});
20+
21+
context('given generic ApiDOM element', function () {
22+
let infoElement: InfoElement;
23+
24+
beforeEach(function () {
25+
infoElement = InfoElement.refract(
26+
new ObjectElement({}, { classes: ['example'] }, { attr: true }),
27+
) as InfoElement;
28+
});
29+
30+
specify('should refract to semantic ApiDOM tree', function () {
31+
expect(sexprs(infoElement)).toMatchSnapshot();
32+
});
33+
34+
specify('should deepmerge meta', function () {
35+
assert.deepEqual(toValue(infoElement.meta), {
36+
classes: ['info', 'example'],
37+
});
38+
});
39+
40+
specify('should deepmerge attributes', function () {
41+
assert.isTrue(infoElement.attributes.get('attr').equals(true));
42+
});
43+
});
2044
});
2145
});
2246
});

0 commit comments

Comments
 (0)