Skip to content

Commit

Permalink
fix(ns-asyncapi-2): retain meta & attributes during refracting (#3858)
Browse files Browse the repository at this point in the history
This change is specific to cases when semantic ApiDOM is refractored
from generic ApiDOM.

Refs #3842
  • Loading branch information
char0n committed Feb 24, 2024
1 parent 333550e commit e3b1848
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
17 changes: 12 additions & 5 deletions packages/apidom-ns-asyncapi-2/src/refractor/visitors/Visitor.ts
@@ -1,4 +1,4 @@
import { Element, hasElementSourceMap } from '@swagger-api/apidom-core';
import { Element, ObjectElement, deepmerge, hasElementSourceMap } from '@swagger-api/apidom-core';

export interface VisitorOptions {}

Expand All @@ -9,13 +9,20 @@ class Visitor {
Object.assign(this, options);
}

// eslint-disable-next-line class-methods-use-this
/* eslint-disable class-methods-use-this, no-param-reassign */
public copyMetaAndAttributes(from: Element, to: Element) {
// copy sourcemaps
if (hasElementSourceMap(from)) {
to.meta.set('sourceMap', from.meta.get('sourceMap'));
if (from.meta.length > 0 || to.meta.length > 0) {
to.meta = deepmerge(to.meta, from.meta) as ObjectElement;
if (hasElementSourceMap(from)) {
// avoid deep merging of source maps
to.meta.set('sourceMap', from.meta.get('sourceMap'));
}
}
if (from.attributes.length > 0 || from.meta.length > 0) {
to.attributes = deepmerge(to.attributes, from.attributes) as ObjectElement; // eslint-disable-line no-param-reassign
}
}
/* eslint-enable- class-methods-use-this, no-param-reassign */
}

export default Visitor;
@@ -1,5 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`refractor elements ReferenceElement given generic ApiDOM element should refract to semantic ApiDOM tree 1`] = `
(ReferenceElement
(MemberElement
(StringElement)
(StringElement)))
`;

exports[`refractor elements ReferenceElement should refract to semantic ApiDOM tree 1`] = `
(ReferenceElement
(MemberElement
Expand Down
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import { sexprs } from '@swagger-api/apidom-core';
import { assert, expect } from 'chai';
import { ObjectElement, sexprs, toValue } from '@swagger-api/apidom-core';

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

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

expect(sexprs(referenceElement)).toMatchSnapshot();
});

context('given generic ApiDOM element', function () {
let referenceElement: ReferenceElement;

beforeEach(function () {
referenceElement = ReferenceElement.refract(
new ObjectElement(
{ $ref: '#/path/to/somewhere' },
{ classes: ['example'] },
{ attr: true },
),
) as ReferenceElement;
});

specify('should refract to semantic ApiDOM tree', function () {
expect(sexprs(referenceElement)).toMatchSnapshot();
});

specify('should deepmerge meta', function () {
assert.deepEqual(toValue(referenceElement.meta), {
classes: ['json-reference', 'asyncapi-reference', 'example', 'reference-element'],
});
});

specify('should deepmerge attributes', function () {
assert.isTrue(referenceElement.attributes.get('attr').equals(true));
});
});
});
});
});

0 comments on commit e3b1848

Please sign in to comment.