-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reference): add OpenAPI 2.0 resolve strategy
Refs #3101
- Loading branch information
Showing
89 changed files
with
1,510 additions
and
2 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
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
55 changes: 55 additions & 0 deletions
55
packages/apidom-reference/src/resolve/strategies/openapi-2/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,55 @@ | ||
import stampit from 'stampit'; | ||
import { createNamespace, visit } from '@swagger-api/apidom-core'; | ||
import openapi2Namespace, { | ||
getNodeType, | ||
isSwaggerElement, | ||
keyMap, | ||
mediaTypes, | ||
} from '@swagger-api/apidom-ns-openapi-2'; | ||
|
||
import ResolveStrategy from '../ResolveStrategy'; | ||
import { | ||
File as IFile, | ||
ReferenceOptions as IReferenceOptions, | ||
ResolveStrategy as IResolveStrategy, | ||
} from '../../../types'; | ||
import ReferenceSet from '../../../ReferenceSet'; | ||
import Reference from '../../../Reference'; | ||
import OpenApi2ResolveVisitor from './visitor'; | ||
|
||
// @ts-ignore | ||
const visitAsync = visit[Symbol.for('nodejs.util.promisify.custom')]; | ||
|
||
const OpenApi2ResolveStrategy: stampit.Stamp<IResolveStrategy> = stampit(ResolveStrategy, { | ||
init() { | ||
this.name = 'openapi-2'; | ||
}, | ||
methods: { | ||
canResolve(file: IFile) { | ||
// assert by media type | ||
if (file.mediaType !== 'text/plain') { | ||
return mediaTypes.includes(file.mediaType); | ||
} | ||
|
||
// assert by inspecting ApiDOM | ||
return isSwaggerElement(file.parseResult?.api); | ||
}, | ||
|
||
async resolve(file: IFile, options: IReferenceOptions) { | ||
const namespace = createNamespace(openapi2Namespace); | ||
const reference = Reference({ uri: file.uri, value: file.parseResult }); | ||
const visitor = OpenApi2ResolveVisitor({ reference, namespace, options }); | ||
const refSet = ReferenceSet(); | ||
refSet.add(reference); | ||
|
||
await visitAsync(refSet.rootRef.value, visitor, { | ||
keyMap, | ||
nodeTypeGetter: getNodeType, | ||
}); | ||
|
||
return refSet; | ||
}, | ||
}, | ||
}); | ||
|
||
export default OpenApi2ResolveStrategy; |
7 changes: 7 additions & 0 deletions
7
packages/apidom-reference/src/resolve/strategies/openapi-2/visitor.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,7 @@ | ||
import stampit from 'stampit'; | ||
|
||
import OpenApi2DereferenceVisitor from '../../../dereference/strategies/openapi-2/visitor'; | ||
|
||
const OpenApi2ResolveVisitor = stampit(OpenApi2DereferenceVisitor); | ||
|
||
export default OpenApi2ResolveVisitor; |
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
8 changes: 8 additions & 0 deletions
8
...olve/strategies/openapi-2/json-reference-object/fixtures/direct-external-circular/ex.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,8 @@ | ||
{ | ||
"definitions": { | ||
"externalSchema": { | ||
"$ref": "./root.json#/definitions/schema" | ||
} | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
...ve/strategies/openapi-2/json-reference-object/fixtures/direct-external-circular/root.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,8 @@ | ||
{ | ||
"swagger": "2.0", | ||
"definitions": { | ||
"schema": { | ||
"$ref": "./ex.json#/definitions/externalSchema" | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...ve/strategies/openapi-2/json-reference-object/fixtures/direct-internal-circular/root.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,8 @@ | ||
{ | ||
"swagger": "2.0", | ||
"definitions": { | ||
"schema": { | ||
"$ref": "#/definitions/schema" | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
.../strategies/openapi-2/json-reference-object/fixtures/external-circular-dependency/ex.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,14 @@ | ||
{ | ||
"definitions": { | ||
"externalSchema1": { | ||
"$ref": "./root.json#/definitions/schema2" | ||
}, | ||
"externalSchema2": { | ||
"$ref": "./root.json#/definitions/schema2" | ||
}, | ||
"externalSchema3": { | ||
"type": "string", | ||
"description": "externalSchema3 description" | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...trategies/openapi-2/json-reference-object/fixtures/external-circular-dependency/root.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,15 @@ | ||
{ | ||
"swagger": "2.0", | ||
"definitions": { | ||
"schema1": { | ||
"$ref": "./ex.json#/definitions/externalSchema1" | ||
}, | ||
"schema2": { | ||
"type": "object", | ||
"description": "schema2 description" | ||
}, | ||
"schema3": { | ||
"$ref": "./ex.json#/definitions/externalSchema3" | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...esolve/strategies/openapi-2/json-reference-object/fixtures/external-indirections/ex1.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,7 @@ | ||
{ | ||
"definitions": { | ||
"indirection": { | ||
"$ref": "./ex2.json#/definitions/indirection" | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...esolve/strategies/openapi-2/json-reference-object/fixtures/external-indirections/ex2.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,7 @@ | ||
{ | ||
"definitions": { | ||
"indirection": { | ||
"$ref": "./ex3.json#/definitions/externalSchema" | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...esolve/strategies/openapi-2/json-reference-object/fixtures/external-indirections/ex3.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,8 @@ | ||
{ | ||
"definitions": { | ||
"externalSchema": { | ||
"type": "object", | ||
"description": "external schema" | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...solve/strategies/openapi-2/json-reference-object/fixtures/external-indirections/root.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,8 @@ | ||
{ | ||
"swagger": "2.0", | ||
"definitions": { | ||
"schema": { | ||
"$ref": "./ex1.json#/definitions/indirection" | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...ce/test/resolve/strategies/openapi-2/json-reference-object/fixtures/external-only/ex.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,8 @@ | ||
{ | ||
"definitions": { | ||
"externalSchema": { | ||
"type": "object", | ||
"description": "external schema" | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
.../test/resolve/strategies/openapi-2/json-reference-object/fixtures/external-only/root.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,8 @@ | ||
{ | ||
"swagger": "2.0", | ||
"definitions": { | ||
"schema": { | ||
"$ref": "./ex.json#/definitions/externalSchema" | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...esolve/strategies/openapi-2/json-reference-object/fixtures/ignore-arbitrary-$refs/ex.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,15 @@ | ||
{ | ||
"one": { | ||
"$ref": "#/two" | ||
}, | ||
"two": { | ||
"a": { | ||
"$ref": "#/three" | ||
} | ||
}, | ||
"three": { | ||
"b": { | ||
"$ref": "#/two" | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...olve/strategies/openapi-2/json-reference-object/fixtures/ignore-arbitrary-$refs/root.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,8 @@ | ||
{ | ||
"swagger": "2.0", | ||
"definitions": { | ||
"schema": { | ||
"$ref": "./ex.json#/one" | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
.../test/resolve/strategies/openapi-2/json-reference-object/fixtures/ignore-external/ex.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,8 @@ | ||
{ | ||
"definitions": { | ||
"externalSchema": { | ||
"type": "number", | ||
"description": "external schema" | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...est/resolve/strategies/openapi-2/json-reference-object/fixtures/ignore-external/root.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,21 @@ | ||
{ | ||
"swagger": "2.0", | ||
"definitions": { | ||
"schema1": { | ||
"$ref": "#/definitions/schema2" | ||
}, | ||
"schema2": { | ||
"$ref": "#/definitions/schema3" | ||
}, | ||
"schema3": { | ||
"$ref": "#/definitions/schema4" | ||
}, | ||
"schema4": { | ||
"type": "number", | ||
"description": "schema4 description" | ||
}, | ||
"schema5": { | ||
"$ref": "./ex.json#/definitions/externalSchema" | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...e/strategies/openapi-2/json-reference-object/fixtures/indirect-external-circular/ex1.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,7 @@ | ||
{ | ||
"definitions": { | ||
"indirection": { | ||
"$ref": "./ex2.json#/definitions/indirection" | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...e/strategies/openapi-2/json-reference-object/fixtures/indirect-external-circular/ex2.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,7 @@ | ||
{ | ||
"definitions": { | ||
"indirection": { | ||
"$ref": "./ex3.json#/definitions/indirection" | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...e/strategies/openapi-2/json-reference-object/fixtures/indirect-external-circular/ex3.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,7 @@ | ||
{ | ||
"definitions": { | ||
"indirection": { | ||
"$ref": "./root.json#/definitions/schema" | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
.../strategies/openapi-2/json-reference-object/fixtures/indirect-external-circular/root.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,8 @@ | ||
{ | ||
"swagger": "2.0", | ||
"definitions": { | ||
"schema": { | ||
"$ref": "./ex1.json#/definitions/indirection" | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
.../strategies/openapi-2/json-reference-object/fixtures/indirect-internal-circular/root.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,17 @@ | ||
{ | ||
"swagger": "2.0", | ||
"definitions": { | ||
"schema1": { | ||
"$ref": "#/definitions/schema2" | ||
}, | ||
"schema2": { | ||
"$ref": "#/definitions/schema3" | ||
}, | ||
"schema3": { | ||
"$ref": "#/definitions/schema4" | ||
}, | ||
"schema4": { | ||
"$ref": "#/definitions/schema1" | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...est/resolve/strategies/openapi-2/json-reference-object/fixtures/internal-external/ex.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,8 @@ | ||
{ | ||
"definitions": { | ||
"externalSchema": { | ||
"type": "string", | ||
"description": "external schema" | ||
} | ||
} | ||
} |
Oops, something went wrong.