Skip to content

Commit 3246c9f

Browse files
authored
feat(apidom-converter): add reference summary and description plugins (#3885)
This plugin removes the summary and description fields of Reference Object. Refs #3697
1 parent 274abfb commit 3246c9f

File tree

9 files changed

+248
-0
lines changed

9 files changed

+248
-0
lines changed

packages/apidom-converter/src/strategies/openapi-3-1-to-openapi-3-0-3/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import type { ConverterOptions } from '../../options';
2424
import createToolbox from './toolbox';
2525
import infoSummaryRefractorPlugin from './refractor-plugins/info-summary';
2626
import licenseIdentifierRefractorPlugin from './refractor-plugins/license-identifier';
27+
import referenceDescriptionRefractorPlugin from './refractor-plugins/reference-description';
28+
import referenceSummaryRefractorPlugin from './refractor-plugins/reference-summary';
2729

2830
// eslint-disable-next-line @typescript-eslint/naming-convention
2931
const openAPI3_0_3MediaTypes = [
@@ -67,6 +69,8 @@ class OpenAPI31ToOpenAPI30ConvertStrategy extends ConvertStrategy {
6769
securityRequirementsEmptyRolesRefractorPlugin({ annotations }),
6870
infoSummaryRefractorPlugin({ annotations }),
6971
licenseIdentifierRefractorPlugin({ annotations }),
72+
referenceDescriptionRefractorPlugin({ annotations }),
73+
referenceSummaryRefractorPlugin({ annotations }),
7074
],
7175
{
7276
toolboxCreator: createToolbox,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { ReferenceElement } from '@swagger-api/apidom-ns-openapi-3-1';
2+
import { AnnotationElement } from '@swagger-api/apidom-core';
3+
4+
type ReferenceDescriptionPluginOptions = {
5+
annotations: AnnotationElement[];
6+
};
7+
8+
const referenceDescriptionRefractorPlugin =
9+
({ annotations }: ReferenceDescriptionPluginOptions) =>
10+
() => {
11+
const annotation = new AnnotationElement(
12+
'The "description" field of Reference Object is not supported in OpenAPI 3.0.3. It has been removed from the converted document.',
13+
{ classes: ['warning'] },
14+
{ code: 'reference-description' },
15+
);
16+
17+
return {
18+
visitor: {
19+
ReferenceElement(element: ReferenceElement) {
20+
if (element.hasKey('description')) {
21+
annotations.push(annotation);
22+
element.remove('description');
23+
}
24+
},
25+
},
26+
};
27+
};
28+
29+
export default referenceDescriptionRefractorPlugin;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { ReferenceElement } from '@swagger-api/apidom-ns-openapi-3-1';
2+
import { AnnotationElement } from '@swagger-api/apidom-core';
3+
4+
type ReferenceSummaryPluginOptions = {
5+
annotations: AnnotationElement[];
6+
};
7+
8+
const referenceSummaryRefractorPlugin =
9+
({ annotations }: ReferenceSummaryPluginOptions) =>
10+
() => {
11+
const annotation = new AnnotationElement(
12+
'The "summary" field of Reference Object is not supported in OpenAPI 3.0.3. It has been removed from the converted document.',
13+
{ classes: ['warning'] },
14+
{ code: 'reference-summary' },
15+
);
16+
17+
return {
18+
visitor: {
19+
ReferenceElement(element: ReferenceElement) {
20+
if (element.hasKey('summary')) {
21+
annotations.push(annotation);
22+
element.remove('summary');
23+
}
24+
},
25+
},
26+
};
27+
};
28+
29+
export default referenceSummaryRefractorPlugin;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`converter strategies openapi-3-1-to-openapi-3-0-3 reference-description should remove Reference.description field 1`] = `
4+
{
5+
"openapi": "3.0.3",
6+
"paths": {
7+
"/path": {
8+
"get": {
9+
"responses": {
10+
"default": {
11+
"$ref": "#/components/responses/default"
12+
}
13+
}
14+
}
15+
}
16+
},
17+
"components": {
18+
"responses": {
19+
"default": {
20+
"description": "first response object"
21+
}
22+
}
23+
}
24+
}
25+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"openapi": "3.1.0",
3+
"paths": {
4+
"/path": {
5+
"get": {
6+
"responses": {
7+
"default": {
8+
"$ref": "#/components/responses/default",
9+
"description": "example description"
10+
}
11+
}
12+
}
13+
}
14+
},
15+
"components": {
16+
"responses": {
17+
"default": {
18+
"description": "first response object"
19+
}
20+
}
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import path from 'node:path';
2+
import { expect, assert } from 'chai';
3+
import { mediaTypes as openAPI31MediaTypes } from '@swagger-api/apidom-parser-adapter-openapi-json-3-1';
4+
import { mediaTypes as openAPI30MediaTypes } from '@swagger-api/apidom-parser-adapter-openapi-json-3-0';
5+
import {
6+
AnnotationElement,
7+
ParseResultElement,
8+
toJSON,
9+
includesClasses,
10+
} from '@swagger-api/apidom-core';
11+
12+
import convert from '../../../../../src';
13+
14+
describe('converter', function () {
15+
context('strategies', function () {
16+
context('openapi-3-1-to-openapi-3-0-3', function () {
17+
context('reference-description', function () {
18+
const fixturePath = path.join(__dirname, 'fixtures', 'reference-description.json');
19+
let convertedParseResult: ParseResultElement;
20+
21+
beforeEach(async function () {
22+
convertedParseResult = await convert(fixturePath, {
23+
convert: {
24+
sourceMediaType: openAPI31MediaTypes.findBy('3.1.0', 'json'),
25+
targetMediaType: openAPI30MediaTypes.findBy('3.0.3', 'json'),
26+
},
27+
});
28+
});
29+
30+
specify('should remove Reference.description field', async function () {
31+
expect(toJSON(convertedParseResult.api!, undefined, 2)).toMatchSnapshot();
32+
});
33+
34+
specify('should create WARNING annotation for description field', async function () {
35+
const annotations = Array.from(convertedParseResult.annotations);
36+
const annotation = annotations.find((a: AnnotationElement) =>
37+
a.code?.equals('reference-description'),
38+
);
39+
40+
assert.isDefined(annotation);
41+
assert.isTrue(includesClasses(['warning'], annotation));
42+
});
43+
});
44+
});
45+
});
46+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`converter strategies openapi-3-1-to-openapi-3-0-3 reference-summary should remove Reference.summary field 1`] = `
4+
{
5+
"openapi": "3.0.3",
6+
"paths": {
7+
"/path": {
8+
"get": {
9+
"responses": {
10+
"default": {
11+
"$ref": "#/components/responses/default"
12+
}
13+
}
14+
}
15+
}
16+
},
17+
"components": {
18+
"responses": {
19+
"default": {
20+
"description": "first response object"
21+
}
22+
}
23+
}
24+
}
25+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"openapi": "3.1.0",
3+
"paths": {
4+
"/path": {
5+
"get": {
6+
"responses": {
7+
"default": {
8+
"$ref": "#/components/responses/default",
9+
"summary": "example summary"
10+
}
11+
}
12+
}
13+
}
14+
},
15+
"components": {
16+
"responses": {
17+
"default": {
18+
"description": "first response object"
19+
}
20+
}
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import path from 'node:path';
2+
import { expect, assert } from 'chai';
3+
import { mediaTypes as openAPI31MediaTypes } from '@swagger-api/apidom-parser-adapter-openapi-json-3-1';
4+
import { mediaTypes as openAPI30MediaTypes } from '@swagger-api/apidom-parser-adapter-openapi-json-3-0';
5+
import {
6+
AnnotationElement,
7+
ParseResultElement,
8+
toJSON,
9+
includesClasses,
10+
} from '@swagger-api/apidom-core';
11+
12+
import convert from '../../../../../src';
13+
14+
describe('converter', function () {
15+
context('strategies', function () {
16+
context('openapi-3-1-to-openapi-3-0-3', function () {
17+
context('reference-summary', function () {
18+
const fixturePath = path.join(__dirname, 'fixtures', 'reference-summary.json');
19+
let convertedParseResult: ParseResultElement;
20+
21+
beforeEach(async function () {
22+
convertedParseResult = await convert(fixturePath, {
23+
convert: {
24+
sourceMediaType: openAPI31MediaTypes.findBy('3.1.0', 'json'),
25+
targetMediaType: openAPI30MediaTypes.findBy('3.0.3', 'json'),
26+
},
27+
});
28+
});
29+
30+
specify('should remove Reference.summary field', async function () {
31+
expect(toJSON(convertedParseResult.api!, undefined, 2)).toMatchSnapshot();
32+
});
33+
34+
specify('should create WARNING annotation for summary field', async function () {
35+
const annotations = Array.from(convertedParseResult.annotations);
36+
const annotation = annotations.find((a: AnnotationElement) =>
37+
a.code?.equals('reference-summary'),
38+
);
39+
40+
assert.isDefined(annotation);
41+
assert.isTrue(includesClasses(['warning'], annotation));
42+
});
43+
});
44+
});
45+
});
46+
});

0 commit comments

Comments
 (0)