Skip to content

Commit

Permalink
fix(ns-openapi-2): retain meta & attributes during refracting (#3844)
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 6a9685b commit b243110
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 10 deletions.
5 changes: 3 additions & 2 deletions packages/apidom-ns-openapi-2/src/refractor/index.ts
Expand Up @@ -17,15 +17,16 @@ const refract = <T extends Element>(
{ specPath = ['visitors', 'document', 'objects', 'Swagger', '$visitor'], plugins = [] } = {},
): T => {
const element = baseRefract(value);

const resolvedSpec = dereference(specification);

/**
* This is where generic ApiDOM becomes semantic (namespace applied).
* We don't allow consumers to hook into this translation.
* Though we allow consumers to define their onw plugins on already transformed ApiDOM.
*/
const RootVistorClass = path(specPath, resolvedSpec) as typeof VisitorClass;
const rootVisitor = new RootVistorClass({ specObj: resolvedSpec });
const RootVisitorClass = path(specPath, resolvedSpec) as typeof VisitorClass;
const rootVisitor = new RootVisitorClass({ specObj: resolvedSpec });

visit(element, rootVisitor);

Expand Down
17 changes: 12 additions & 5 deletions packages/apidom-ns-openapi-2/src/refractor/visitors/Visitor.ts
@@ -1,4 +1,4 @@
import { Element, hasElementSourceMap } 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,18 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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

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

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

Expand All @@ -16,6 +16,36 @@ describe('refractor', function () {
expect(sexprs(contactElement)).toMatchSnapshot();
});

context('given generic ApiDOM element', function () {
let contactElement: ContactElement;

beforeEach(function () {
contactElement = ContactElement.refract(
new ObjectElement(
{
name: 'API Support',
url: 'https://www.example.com/support',
email: 'support@example.com',
},
{ meta: true },
{ attr: true },
),
) as ContactElement;
});

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

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

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

specify('should support specification extensions', function () {
const contactElement = ContactElement.refract({
name: 'API support',
Expand Down
4 changes: 2 additions & 2 deletions packages/apidom-ns-openapi-3-0/src/refractor/index.ts
Expand Up @@ -24,8 +24,8 @@ const refract = <T extends Element>(
* We don't allow consumers to hook into this translation.
* Though we allow consumers to define their onw plugins on already transformed ApiDOM.
*/
const RootVistorClass = path(specPath, resolvedSpec) as typeof VisitorClass;
const rootVisitor = new RootVistorClass({ specObj: resolvedSpec });
const RootVisitorClass = path(specPath, resolvedSpec) as typeof VisitorClass;
const rootVisitor = new RootVisitorClass({ specObj: resolvedSpec });

visit(element, rootVisitor);

Expand Down

0 comments on commit b243110

Please sign in to comment.