Skip to content

Commit

Permalink
fix(ns-openapi-3-0): retain meta & attributes during refracting (#3859)
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 7e40336 commit abefa88
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 7 deletions.
Expand Up @@ -10,6 +10,10 @@ exports[`given OpenAPI 3.1 definition should decorate with API Design Systems St
{
"element": "string",
"content": "api"
},
{
"element": "string",
"content": "result"
}
]
}
Expand Down
17 changes: 12 additions & 5 deletions packages/apidom-ns-openapi-3-0/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,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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

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

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

Expand All @@ -18,6 +18,30 @@ describe('refractor', function () {

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

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

beforeEach(function () {
infoElement = InfoElement.refract(
new ObjectElement({}, { classes: ['example'] }, { attr: true }),
) as InfoElement;
});

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

specify('should deepmerge meta', function () {
assert.deepEqual(toValue(infoElement.meta), {
classes: ['info', 'example'],
});
});

specify('should deepmerge attributes', function () {
assert.isTrue(infoElement.attributes.get('attr').equals(true));
});
});
});
});
});
Expand Up @@ -10,6 +10,10 @@ exports[`refractor plugins normalize-operation-ids given Operation Object with m
{
"element": "string",
"content": "api"
},
{
"element": "string",
"content": "result"
}
]
}
Expand Down Expand Up @@ -183,6 +187,10 @@ exports[`refractor plugins normalize-operation-ids given Operation Object with n
{
"element": "string",
"content": "api"
},
{
"element": "string",
"content": "result"
}
]
}
Expand Down Expand Up @@ -382,6 +390,10 @@ exports[`refractor plugins normalize-operation-ids given Operation Object with o
{
"element": "string",
"content": "api"
},
{
"element": "string",
"content": "result"
}
]
}
Expand Down Expand Up @@ -598,6 +610,10 @@ exports[`refractor plugins normalize-operation-ids given Operation Object with o
{
"element": "string",
"content": "api"
},
{
"element": "string",
"content": "result"
}
]
}
Expand Down Expand Up @@ -814,6 +830,10 @@ exports[`refractor plugins normalize-operation-ids given Operation Object with u
{
"element": "string",
"content": "api"
},
{
"element": "string",
"content": "result"
}
]
}
Expand Down Expand Up @@ -1168,6 +1188,10 @@ exports[`refractor plugins normalize-operation-ids given Operation Object with u
{
"element": "string",
"content": "api"
},
{
"element": "string",
"content": "result"
}
]
}
Expand Down Expand Up @@ -1384,6 +1408,10 @@ exports[`refractor plugins normalize-operation-ids given Operation Objects with
{
"element": "string",
"content": "api"
},
{
"element": "string",
"content": "result"
}
]
}
Expand Down
Expand Up @@ -99,6 +99,10 @@ exports[`refractor plugins normalize-parameters given parameters are defined in
{
"element": "string",
"content": "api"
},
{
"element": "string",
"content": "result"
}
]
}
Expand Down
Expand Up @@ -117,6 +117,10 @@ exports[`refractor plugins normalize-parameters given parameters are defined in
{
"element": "string",
"content": "api"
},
{
"element": "string",
"content": "result"
}
]
}
Expand Down Expand Up @@ -608,6 +612,10 @@ exports[`refractor plugins normalize-parameters given parameters are defined in
{
"element": "string",
"content": "api"
},
{
"element": "string",
"content": "result"
}
]
}
Expand Down
Expand Up @@ -67,6 +67,10 @@ exports[`refractor plugins normalize-parameters given parameters are defined in
{
"element": "string",
"content": "api"
},
{
"element": "string",
"content": "result"
}
]
}
Expand Down
Expand Up @@ -56,6 +56,10 @@ exports[`refractor plugins normalize-servers given OpenAPI.servers defined and P
{
"element": "string",
"content": "api"
},
{
"element": "string",
"content": "result"
}
]
}
Expand Down

0 comments on commit abefa88

Please sign in to comment.