Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(apidom-converter): add reference summary and description plugins (…
- Loading branch information
Showing
9 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...er/src/strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/reference-description.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { ReferenceElement } from '@swagger-api/apidom-ns-openapi-3-1'; | ||
import { AnnotationElement } from '@swagger-api/apidom-core'; | ||
|
||
type ReferenceDescriptionPluginOptions = { | ||
annotations: AnnotationElement[]; | ||
}; | ||
|
||
const referenceDescriptionRefractorPlugin = | ||
({ annotations }: ReferenceDescriptionPluginOptions) => | ||
() => { | ||
const annotation = new AnnotationElement( | ||
'The "description" field of Reference Object is not supported in OpenAPI 3.0.3. It has been removed from the converted document.', | ||
{ classes: ['warning'] }, | ||
{ code: 'reference-description' }, | ||
); | ||
|
||
return { | ||
visitor: { | ||
ReferenceElement(element: ReferenceElement) { | ||
if (element.hasKey('description')) { | ||
annotations.push(annotation); | ||
element.remove('description'); | ||
} | ||
}, | ||
}, | ||
}; | ||
}; | ||
|
||
export default referenceDescriptionRefractorPlugin; |
29 changes: 29 additions & 0 deletions
29
...verter/src/strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/reference-summary.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { ReferenceElement } from '@swagger-api/apidom-ns-openapi-3-1'; | ||
import { AnnotationElement } from '@swagger-api/apidom-core'; | ||
|
||
type ReferenceSummaryPluginOptions = { | ||
annotations: AnnotationElement[]; | ||
}; | ||
|
||
const referenceSummaryRefractorPlugin = | ||
({ annotations }: ReferenceSummaryPluginOptions) => | ||
() => { | ||
const annotation = new AnnotationElement( | ||
'The "summary" field of Reference Object is not supported in OpenAPI 3.0.3. It has been removed from the converted document.', | ||
{ classes: ['warning'] }, | ||
{ code: 'reference-summary' }, | ||
); | ||
|
||
return { | ||
visitor: { | ||
ReferenceElement(element: ReferenceElement) { | ||
if (element.hasKey('summary')) { | ||
annotations.push(annotation); | ||
element.remove('summary'); | ||
} | ||
}, | ||
}, | ||
}; | ||
}; | ||
|
||
export default referenceSummaryRefractorPlugin; |
25 changes: 25 additions & 0 deletions
25
...-3-1-to-openapi-3-0-3/refractor-plugins/reference-description/__snapshots__/index.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`converter strategies openapi-3-1-to-openapi-3-0-3 reference-description should remove Reference.description field 1`] = ` | ||
{ | ||
"openapi": "3.0.3", | ||
"paths": { | ||
"/path": { | ||
"get": { | ||
"responses": { | ||
"default": { | ||
"$ref": "#/components/responses/default" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"components": { | ||
"responses": { | ||
"default": { | ||
"description": "first response object" | ||
} | ||
} | ||
} | ||
} | ||
`; |
22 changes: 22 additions & 0 deletions
22
...openapi-3-0-3/refractor-plugins/reference-description/fixtures/reference-description.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"openapi": "3.1.0", | ||
"paths": { | ||
"/path": { | ||
"get": { | ||
"responses": { | ||
"default": { | ||
"$ref": "#/components/responses/default", | ||
"description": "example description" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"components": { | ||
"responses": { | ||
"default": { | ||
"description": "first response object" | ||
} | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
.../strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/reference-description/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import path from 'node:path'; | ||
import { expect, assert } from 'chai'; | ||
import { mediaTypes as openAPI31MediaTypes } from '@swagger-api/apidom-parser-adapter-openapi-json-3-1'; | ||
import { mediaTypes as openAPI30MediaTypes } from '@swagger-api/apidom-parser-adapter-openapi-json-3-0'; | ||
import { | ||
AnnotationElement, | ||
ParseResultElement, | ||
toJSON, | ||
includesClasses, | ||
} from '@swagger-api/apidom-core'; | ||
|
||
import convert from '../../../../../src'; | ||
|
||
describe('converter', function () { | ||
context('strategies', function () { | ||
context('openapi-3-1-to-openapi-3-0-3', function () { | ||
context('reference-description', function () { | ||
const fixturePath = path.join(__dirname, 'fixtures', 'reference-description.json'); | ||
let convertedParseResult: ParseResultElement; | ||
|
||
beforeEach(async function () { | ||
convertedParseResult = await convert(fixturePath, { | ||
convert: { | ||
sourceMediaType: openAPI31MediaTypes.findBy('3.1.0', 'json'), | ||
targetMediaType: openAPI30MediaTypes.findBy('3.0.3', 'json'), | ||
}, | ||
}); | ||
}); | ||
|
||
specify('should remove Reference.description field', async function () { | ||
expect(toJSON(convertedParseResult.api!, undefined, 2)).toMatchSnapshot(); | ||
}); | ||
|
||
specify('should create WARNING annotation for description field', async function () { | ||
const annotations = Array.from(convertedParseResult.annotations); | ||
const annotation = annotations.find((a: AnnotationElement) => | ||
a.code?.equals('reference-description'), | ||
); | ||
|
||
assert.isDefined(annotation); | ||
assert.isTrue(includesClasses(['warning'], annotation)); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
25 changes: 25 additions & 0 deletions
25
...napi-3-1-to-openapi-3-0-3/refractor-plugins/reference-summary/__snapshots__/index.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`converter strategies openapi-3-1-to-openapi-3-0-3 reference-summary should remove Reference.summary field 1`] = ` | ||
{ | ||
"openapi": "3.0.3", | ||
"paths": { | ||
"/path": { | ||
"get": { | ||
"responses": { | ||
"default": { | ||
"$ref": "#/components/responses/default" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"components": { | ||
"responses": { | ||
"default": { | ||
"description": "first response object" | ||
} | ||
} | ||
} | ||
} | ||
`; |
22 changes: 22 additions & 0 deletions
22
...-3-1-to-openapi-3-0-3/refractor-plugins/reference-summary/fixtures/reference-summary.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"openapi": "3.1.0", | ||
"paths": { | ||
"/path": { | ||
"get": { | ||
"responses": { | ||
"default": { | ||
"$ref": "#/components/responses/default", | ||
"summary": "example summary" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"components": { | ||
"responses": { | ||
"default": { | ||
"description": "first response object" | ||
} | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...test/strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/reference-summary/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import path from 'node:path'; | ||
import { expect, assert } from 'chai'; | ||
import { mediaTypes as openAPI31MediaTypes } from '@swagger-api/apidom-parser-adapter-openapi-json-3-1'; | ||
import { mediaTypes as openAPI30MediaTypes } from '@swagger-api/apidom-parser-adapter-openapi-json-3-0'; | ||
import { | ||
AnnotationElement, | ||
ParseResultElement, | ||
toJSON, | ||
includesClasses, | ||
} from '@swagger-api/apidom-core'; | ||
|
||
import convert from '../../../../../src'; | ||
|
||
describe('converter', function () { | ||
context('strategies', function () { | ||
context('openapi-3-1-to-openapi-3-0-3', function () { | ||
context('reference-summary', function () { | ||
const fixturePath = path.join(__dirname, 'fixtures', 'reference-summary.json'); | ||
let convertedParseResult: ParseResultElement; | ||
|
||
beforeEach(async function () { | ||
convertedParseResult = await convert(fixturePath, { | ||
convert: { | ||
sourceMediaType: openAPI31MediaTypes.findBy('3.1.0', 'json'), | ||
targetMediaType: openAPI30MediaTypes.findBy('3.0.3', 'json'), | ||
}, | ||
}); | ||
}); | ||
|
||
specify('should remove Reference.summary field', async function () { | ||
expect(toJSON(convertedParseResult.api!, undefined, 2)).toMatchSnapshot(); | ||
}); | ||
|
||
specify('should create WARNING annotation for summary field', async function () { | ||
const annotations = Array.from(convertedParseResult.annotations); | ||
const annotation = annotations.find((a: AnnotationElement) => | ||
a.code?.equals('reference-summary'), | ||
); | ||
|
||
assert.isDefined(annotation); | ||
assert.isTrue(includesClasses(['warning'], annotation)); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |