Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ns-openapi-3-0): retain meta & attributes during refracting #3859

Merged
merged 4 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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));
});
});
});
});
});
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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