-
Notifications
You must be signed in to change notification settings - Fork 761
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add initial OpenAPI 3.1.0 dereference strategy
Refs #2717
- Loading branch information
Showing
19 changed files
with
246 additions
and
12 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
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
64 changes: 64 additions & 0 deletions
64
src/helpers/apidom/reference/dereference/strategies/openapi-3-1-swagger-client/index.js
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,64 @@ | ||
/* eslint-disable camelcase */ | ||
import { createNamespace, visit } from '@swagger-api/apidom-core'; | ||
import { ReferenceSet, Reference } from '@swagger-api/apidom-reference/configuration/empty'; | ||
import OpenApi3_1DereferenceStrategy from '@swagger-api/apidom-reference/dereference/strategies/openapi-3-1'; | ||
import openApi3_1Namespace, { getNodeType, keyMap } from '@swagger-api/apidom-ns-openapi-3-1'; | ||
|
||
import OpenApi3_1SwaggerClientDereferenceVisitor from './visitor.js'; | ||
|
||
const visitAsync = visit[Symbol.for('nodejs.util.promisify.custom')]; | ||
|
||
const OpenApi3_1SwaggerClientDereferenceStrategy = OpenApi3_1DereferenceStrategy.compose({ | ||
props: { | ||
useCircularStructures: true, | ||
allowMetaPatches: false, | ||
}, | ||
init({ | ||
useCircularStructures = this.useCircularStructures, | ||
allowMetaPatches = this.allowMetaPatches, | ||
} = {}) { | ||
this.name = 'openapi-3-1-swagger-client'; | ||
this.useCircularStructures = useCircularStructures; | ||
this.allowMetaPatches = allowMetaPatches; | ||
}, | ||
methods: { | ||
async dereference(file, options) { | ||
const namespace = createNamespace(openApi3_1Namespace); | ||
const refSet = options.dereference.refSet ?? ReferenceSet(); | ||
let reference; | ||
|
||
if (!refSet.has(file.uri)) { | ||
reference = Reference({ uri: file.uri, value: file.parseResult }); | ||
refSet.add(reference); | ||
} else { | ||
// pre-computed refSet was provided as configuration option | ||
reference = refSet.find((ref) => ref.uri === file.uri); | ||
} | ||
|
||
const visitor = OpenApi3_1SwaggerClientDereferenceVisitor({ | ||
reference, | ||
namespace, | ||
options, | ||
useCircularStructures: this.useCircularStructures, | ||
allowMetaPatches: this.allowMetaPatches, | ||
}); | ||
const dereferencedElement = await visitAsync(refSet.rootRef.value, visitor, { | ||
keyMap, | ||
nodeTypeGetter: getNodeType, | ||
}); | ||
|
||
/** | ||
* Release all memory if this refSet was not provided as a configuration option. | ||
* If provided as configuration option, then provider is responsible for cleanup. | ||
*/ | ||
if (options.dereference.refSet === null) { | ||
refSet.clean(); | ||
} | ||
|
||
return dereferencedElement; | ||
}, | ||
}, | ||
}); | ||
|
||
export default OpenApi3_1SwaggerClientDereferenceStrategy; | ||
/* eslint-enable camelcase */ |
7 changes: 7 additions & 0 deletions
7
src/helpers/apidom/reference/dereference/strategies/openapi-3-1-swagger-client/visitor.js
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,7 @@ | ||
/* eslint-disable camelcase */ | ||
import { OpenApi3_1DereferenceVisitor } from '@swagger-api/apidom-reference/dereference/strategies/openapi-3-1'; | ||
|
||
const OpenApi3_1SwaggerClientDereferenceVisitor = OpenApi3_1DereferenceVisitor.compose({}); | ||
|
||
export default OpenApi3_1SwaggerClientDereferenceVisitor; | ||
/* eslint-enable camelcase */ |
33 changes: 33 additions & 0 deletions
33
...reference/dereference/strategies/openapi-3-1-swagger-client/__utils__/jest.local.setup.js
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,33 @@ | ||
/* eslint-disable camelcase */ | ||
import { options } from '@swagger-api/apidom-reference/configuration/empty'; | ||
import FileResolver from '@swagger-api/apidom-reference/resolve/resolvers/file'; | ||
|
||
import JsonParser from '../../../../../../../../src/helpers/apidom/reference/parse/parsers/json/index.js'; | ||
import YamlParser from '../../../../../../../../src/helpers/apidom/reference/parse/parsers/yaml-1-2/index.js'; | ||
import OpenApiJson3_1Parser from '../../../../../../../../src/helpers/apidom/reference/parse/parsers/openapi-json-3-1/index.js'; | ||
import OpenApiYaml3_1Parser from '../../../../../../../../src/helpers/apidom/reference/parse/parsers/openapi-yaml-3-1/index.js'; | ||
import HttpResolverSwaggerClient from '../../../../../../../../src/helpers/apidom/reference/resolve/resolvers/http-swagger-client/index.js'; | ||
import OpenApi3_1SwaggerClientDereferenceStrategy from '../../../../../../../../src/helpers/apidom/reference/dereference/strategies/openapi-3-1-swagger-client/index.js'; | ||
|
||
export const beforeAll = () => { | ||
// configure custom parser plugins globally | ||
options.parse.parsers = [ | ||
OpenApiJson3_1Parser({ allowEmpty: false, sourceMap: false }), | ||
OpenApiYaml3_1Parser({ allowEmpty: false, sourceMap: false }), | ||
JsonParser({ allowEmpty: false, sourceMap: false }), | ||
YamlParser({ allowEmpty: false, sourceMap: false }), | ||
]; | ||
|
||
// configure custom resolver plugins globally | ||
options.resolve.resolvers = [FileResolver({ fileAllowList: ['*'] }), HttpResolverSwaggerClient()]; | ||
|
||
// configure custom dereference strategy globally | ||
options.dereference.strategies = [OpenApi3_1SwaggerClientDereferenceStrategy()]; | ||
}; | ||
|
||
export const afterAll = () => { | ||
options.parse.parsers = []; | ||
options.resolve.resolvers = []; | ||
options.dereference.strategies = []; | ||
}; | ||
/* eslint-enable camelcase */ |
19 changes: 19 additions & 0 deletions
19
...pi-3-1-swagger-client/callback-object/__fixtures__/components-callbacks/dereferenced.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,19 @@ | ||
[ | ||
{ | ||
"openapi": "3.1.0", | ||
"components": { | ||
"callbacks": { | ||
"callback1": { | ||
"{$method}": { | ||
"description": "description of callback2" | ||
} | ||
}, | ||
"callback2": { | ||
"{$method}": { | ||
"description": "description of callback2" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
] |
9 changes: 9 additions & 0 deletions
9
...es/openapi-3-1-swagger-client/callback-object/__fixtures__/components-callbacks/root.yaml
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,9 @@ | ||
--- | ||
openapi: 3.1.0 | ||
components: | ||
callbacks: | ||
callback1: | ||
"$ref": "#/components/callbacks/callback2" | ||
callback2: | ||
"{$method}": | ||
description: description of callback2 |
27 changes: 27 additions & 0 deletions
27
...penapi-3-1-swagger-client/callback-object/__fixtures__/operation-object/dereferenced.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,27 @@ | ||
[ | ||
{ | ||
"openapi": "3.1.0", | ||
"paths": { | ||
"/path": { | ||
"get": { | ||
"callbacks": { | ||
"callback": { | ||
"{$method}": { | ||
"description": "description of callback2" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"components": { | ||
"callbacks": { | ||
"callback": { | ||
"{$method}": { | ||
"description": "description of callback2" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
] |
13 changes: 13 additions & 0 deletions
13
...tegies/openapi-3-1-swagger-client/callback-object/__fixtures__/operation-object/root.yaml
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,13 @@ | ||
--- | ||
openapi: 3.1.0 | ||
paths: | ||
"/path": | ||
get: | ||
callbacks: | ||
callback: | ||
"$ref": "#/components/callbacks/callback" | ||
components: | ||
callbacks: | ||
callback: | ||
"{$method}": | ||
description: description of callback2 |
52 changes: 52 additions & 0 deletions
52
...idom/reference/dereference/strategies/openapi-3-1-swagger-client/callback-object/index.js
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,52 @@ | ||
import path from 'node:path'; | ||
import { toValue } from '@swagger-api/apidom-core'; | ||
import { mediaTypes } from '@swagger-api/apidom-ns-openapi-3-1'; | ||
import { dereference } from '@swagger-api/apidom-reference/configuration/empty'; | ||
|
||
import * as jestSetup from '../__utils__/jest.local.setup.js'; | ||
|
||
const rootFixturePath = path.join(__dirname, '__fixtures__'); | ||
|
||
describe('dereference', () => { | ||
beforeAll(() => { | ||
jestSetup.beforeAll(); | ||
}); | ||
|
||
afterAll(() => { | ||
jestSetup.afterAll(); | ||
}); | ||
|
||
describe('strategies', () => { | ||
describe('openapi-3-1swagger-client', () => { | ||
describe('Callback Object', () => { | ||
describe('given in components/callbacks field', () => { | ||
const fixturePath = path.join(rootFixturePath, 'components-callbacks'); | ||
|
||
test('should dereference', async () => { | ||
const rootFilePath = path.join(fixturePath, 'root.yaml'); | ||
const actual = await dereference(rootFilePath, { | ||
parse: { mediaType: mediaTypes.latest('yaml') }, | ||
}); | ||
const expected = globalThis.loadJsonFile(path.join(fixturePath, 'dereferenced.json')); | ||
|
||
expect(toValue(actual)).toEqual(expected); | ||
}); | ||
}); | ||
|
||
describe('given in Operation Object', () => { | ||
const fixturePath = path.join(rootFixturePath, 'operation-object'); | ||
|
||
test('should dereference', async () => { | ||
const rootFilePath = path.join(fixturePath, 'root.yaml'); | ||
const actual = await dereference(rootFilePath, { | ||
parse: { mediaType: mediaTypes.latest('yaml') }, | ||
}); | ||
const expected = globalThis.loadJsonFile(path.join(fixturePath, 'dereferenced.json')); | ||
|
||
expect(toValue(actual)).toEqual(expected); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
File renamed without changes.
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
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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