Skip to content

Commit

Permalink
fix(ns-ads): retain meta & attributes during refracting (#3857)
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 b243110 commit 333550e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 9 deletions.
@@ -1,4 +1,4 @@
import { hasElementSourceMap, Element } from '@swagger-api/apidom-core';
import { Element, ObjectElement, hasElementSourceMap, deepmerge } 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,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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

exports[`refractor elements InfoElement should refract to semantic ApiDOM tree 1`] = `
(InfoElement
(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 } from '@swagger-api/apidom-core';

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

Expand All @@ -14,6 +14,35 @@ describe('refractor', function () {

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

context('given generic ApiDOM element', function () {
let infoElement: InfoElement;

beforeEach(function () {
infoElement = InfoElement.refract(
new ObjectElement(
{
title: 'title of API Design System',
description: 'description of the API Design System',
},
{ meta: true },
{ attr: true },
),
) as InfoElement;
});

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

specify('should deepmerge meta', function () {
assert.isTrue(infoElement.meta.get('meta').equals(true));
});

specify('should deepmerge attributes', function () {
assert.isTrue(infoElement.attributes.get('attr').equals(true));
});
});
});
});
});
Expand Up @@ -37,11 +37,11 @@ describe('refractor', function () {
expect(sexprs(contactElement)).toMatchSnapshot();
});

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

specify('should retain meta', function () {
specify('should deepmerge meta', function () {
assert.isTrue(contactElement.meta.get('meta').equals(true));
});
});
Expand Down

0 comments on commit 333550e

Please sign in to comment.