Skip to content

Commit

Permalink
fix(ns-openapi-2): retain meta & attributes during refracting
Browse files Browse the repository at this point in the history
Refs #3842
  • Loading branch information
char0n committed Feb 21, 2024
1 parent a7aac32 commit b1d369c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 9 deletions.
5 changes: 3 additions & 2 deletions packages/apidom-ns-openapi-2/src/refractor/index.ts
Original file line number Diff line number Diff line change
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
13 changes: 9 additions & 4 deletions packages/apidom-ns-openapi-2/src/refractor/visitors/Visitor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Element, hasElementSourceMap } from '@swagger-api/apidom-core';
import { Element, cloneDeep } from '@swagger-api/apidom-core';

export interface VisitorOptions {}

Expand All @@ -11,9 +11,14 @@ class Visitor {

// eslint-disable-next-line class-methods-use-this
public copyMetaAndAttributes(from: Element, to: Element) {
// copy sourcemaps
if (hasElementSourceMap(from)) {
to.meta.set('sourceMap', from.meta.get('sourceMap'));
// copy meta
if (from.meta.length > 0) {
to.meta = cloneDeep(from.meta); // eslint-disable-line no-param-reassign
}

// copy attributes
if (from.attributes.length > 0) {
to.attributes = cloneDeep(from.attributes); // eslint-disable-line no-param-reassign
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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 b1d369c

Please sign in to comment.