-
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.
test(reference): add tests for OpenAPI 2.0 JSONReference deref
Refs #3102
- Loading branch information
Showing
54 changed files
with
1,188 additions
and
0 deletions.
There are no files selected for viewing
256 changes: 256 additions & 0 deletions
256
...ference/test/dereference/strategies/openapi-2/json-reference-object/dereference-apidom.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,256 @@ | ||
import path from 'node:path'; | ||
import { assert } from 'chai'; | ||
import { mediaTypes, isSchemaElement, SwaggerElement } from '@swagger-api/apidom-ns-openapi-2'; | ||
import { toValue } from '@swagger-api/apidom-core'; | ||
import { evaluate } from '@swagger-api/apidom-json-pointer'; | ||
|
||
import { parse, dereferenceApiDOM } from '../../../../../src'; | ||
import { ServerTerminable, createHTTPServer } from '../../../../helpers'; | ||
|
||
describe('dereference', function () { | ||
context('strategies', function () { | ||
context('openapi-2', function () { | ||
context('JSONReference Object', function () { | ||
context( | ||
'given single JSONReferenceElement passed to dereferenceApiDOM with internal references', | ||
function () { | ||
context('given dereferencing using local file system', function () { | ||
const fixturePath = path.join(__dirname, 'fixtures', 'internal-only', 'root.json'); | ||
|
||
specify('should dereference', async function () { | ||
const parseResult = await parse(fixturePath, { | ||
parse: { mediaType: mediaTypes.latest('json') }, | ||
}); | ||
const referenceElement = evaluate( | ||
'/definitions/schema1', | ||
parseResult.api as SwaggerElement, | ||
); | ||
const dereferenced = await dereferenceApiDOM(referenceElement, { | ||
parse: { mediaType: mediaTypes.latest('json') }, | ||
resolve: { | ||
baseURI: `${fixturePath}#/definitions/schema1`, | ||
}, | ||
}); | ||
|
||
assert.isTrue(isSchemaElement(dereferenced)); | ||
}); | ||
|
||
specify('should dereference and contain metadata about origin', async function () { | ||
const parseResult = await parse(fixturePath, { | ||
parse: { mediaType: mediaTypes.latest('json') }, | ||
}); | ||
const referenceElement = evaluate( | ||
'/definitions/schema1', | ||
parseResult.api as SwaggerElement, | ||
); | ||
const dereferenced = await dereferenceApiDOM(referenceElement, { | ||
parse: { mediaType: mediaTypes.latest('json') }, | ||
resolve: { baseURI: `${fixturePath}#/definitions/schema1` }, | ||
}); | ||
|
||
assert.match( | ||
toValue(dereferenced.meta.get('ref-origin')), | ||
/internal-only\/root\.json$/, | ||
); | ||
}); | ||
}); | ||
|
||
context('given dereferencing using HTTP protocol', function () { | ||
const fixturePath = path.join(__dirname, 'fixtures', 'internal-only', 'root.json'); | ||
const httpPort = 8123; | ||
let httpServer: ServerTerminable; | ||
|
||
beforeEach(function () { | ||
const cwd = path.join(__dirname, 'fixtures', 'internal-only'); | ||
httpServer = createHTTPServer({ port: httpPort, cwd }); | ||
}); | ||
|
||
afterEach(async function () { | ||
await httpServer.terminate(); | ||
}); | ||
|
||
specify('should dereference', async function () { | ||
const parseResult = await parse(fixturePath, { | ||
parse: { mediaType: mediaTypes.latest('json') }, | ||
}); | ||
const referenceElement = evaluate( | ||
'/definitions/schema1', | ||
parseResult.api as SwaggerElement, | ||
); | ||
const dereferenced = await dereferenceApiDOM(referenceElement, { | ||
parse: { mediaType: mediaTypes.latest('json') }, | ||
resolve: { | ||
baseURI: `http://localhost:${httpPort}/root.json#/definitions/schema1`, | ||
}, | ||
}); | ||
|
||
assert.isTrue(isSchemaElement(dereferenced)); | ||
}); | ||
|
||
specify('should dereference and contain metadata about origin', async function () { | ||
const parseResult = await parse(fixturePath, { | ||
parse: { mediaType: mediaTypes.latest('json') }, | ||
}); | ||
const referenceElement = evaluate( | ||
'/definitions/schema1', | ||
parseResult.api as SwaggerElement, | ||
); | ||
const dereferenced = await dereferenceApiDOM(referenceElement, { | ||
parse: { mediaType: mediaTypes.latest('json') }, | ||
resolve: { | ||
baseURI: `http://localhost:${httpPort}/root.json#/definitions/schema1`, | ||
}, | ||
}); | ||
|
||
assert.match(toValue(dereferenced.meta.get('ref-origin')), /\/root\.json$/); | ||
}); | ||
}); | ||
}, | ||
); | ||
|
||
context( | ||
'given single JSONReferenceElement passed to dereferenceApiDOM with external references', | ||
function () { | ||
context('given dereferencing using local file system', function () { | ||
const fixturePath = path.join(__dirname, 'fixtures', 'external-only', 'root.json'); | ||
|
||
specify('should dereference', async function () { | ||
const parseResult = await parse(fixturePath, { | ||
parse: { mediaType: mediaTypes.latest('json') }, | ||
}); | ||
const referenceElement = evaluate( | ||
'/definitions/schema', | ||
parseResult.api as SwaggerElement, | ||
); | ||
const dereferenced = await dereferenceApiDOM(referenceElement, { | ||
parse: { mediaType: mediaTypes.latest('json') }, | ||
resolve: { baseURI: fixturePath }, | ||
}); | ||
|
||
assert.isTrue(isSchemaElement(dereferenced)); | ||
}); | ||
|
||
specify('should dereference and contain metadata about origin', async function () { | ||
const parseResult = await parse(fixturePath, { | ||
parse: { mediaType: mediaTypes.latest('json') }, | ||
}); | ||
const referenceElement = evaluate( | ||
'/definitions/schema', | ||
parseResult.api as SwaggerElement, | ||
); | ||
const dereferenced = await dereferenceApiDOM(referenceElement, { | ||
parse: { mediaType: mediaTypes.latest('json') }, | ||
resolve: { baseURI: fixturePath }, | ||
}); | ||
|
||
assert.match( | ||
toValue(dereferenced.meta.get('ref-origin')), | ||
/external-only\/ex\.json$/, | ||
); | ||
}); | ||
}); | ||
|
||
context('given dereferencing using HTTP protocol', function () { | ||
const fixturePath = path.join(__dirname, 'fixtures', 'external-only', 'root.json'); | ||
const httpPort = 8123; | ||
let httpServer: ServerTerminable; | ||
|
||
beforeEach(function () { | ||
const cwd = path.join(__dirname, 'fixtures', 'external-only'); | ||
httpServer = createHTTPServer({ port: httpPort, cwd }); | ||
}); | ||
|
||
afterEach(async function () { | ||
await httpServer.terminate(); | ||
}); | ||
|
||
specify('should dereference', async function () { | ||
const parseResult = await parse(fixturePath, { | ||
parse: { mediaType: mediaTypes.latest('json') }, | ||
}); | ||
const referenceElement = evaluate( | ||
'/definitions/schema', | ||
parseResult.api as SwaggerElement, | ||
); | ||
const dereferenced = await dereferenceApiDOM(referenceElement, { | ||
parse: { mediaType: mediaTypes.latest('json') }, | ||
resolve: { baseURI: `http://localhost:${httpPort}/root.json` }, | ||
}); | ||
|
||
assert.isTrue(isSchemaElement(dereferenced)); | ||
}); | ||
|
||
specify('should dereference and contain metadata about origin', async function () { | ||
const parseResult = await parse(fixturePath, { | ||
parse: { mediaType: mediaTypes.latest('json') }, | ||
}); | ||
const referenceElement = evaluate( | ||
'/definitions/schema', | ||
parseResult.api as SwaggerElement, | ||
); | ||
const dereferenced = await dereferenceApiDOM(referenceElement, { | ||
parse: { mediaType: mediaTypes.latest('json') }, | ||
resolve: { baseURI: `http://localhost:${httpPort}/root.json` }, | ||
}); | ||
|
||
assert.match(toValue(dereferenced.meta.get('ref-origin')), /\/ex\.json$/); | ||
}); | ||
}); | ||
|
||
context('given dereferencing using HTTP protocol and absolute URLs', function () { | ||
const fixturePath = path.join( | ||
__dirname, | ||
'fixtures', | ||
'external-only-absolute-url', | ||
'root.json', | ||
); | ||
const httpPort = 8123; | ||
let httpServer: ServerTerminable; | ||
|
||
beforeEach(function () { | ||
const cwd = path.join(__dirname, 'fixtures', 'external-only-absolute-url'); | ||
httpServer = createHTTPServer({ port: httpPort, cwd }); | ||
}); | ||
|
||
afterEach(async function () { | ||
await httpServer.terminate(); | ||
}); | ||
|
||
specify('should dereference', async function () { | ||
const parseResult = await parse(fixturePath, { | ||
parse: { mediaType: mediaTypes.latest('json') }, | ||
}); | ||
const referenceElement = evaluate( | ||
'/definitions/schema', | ||
parseResult.api as SwaggerElement, | ||
); | ||
const dereferenced = await dereferenceApiDOM(referenceElement, { | ||
parse: { mediaType: mediaTypes.latest('json') }, | ||
resolve: { baseURI: `http://localhost:${httpPort}/root.json` }, | ||
}); | ||
|
||
assert.isTrue(isSchemaElement(dereferenced)); | ||
}); | ||
|
||
specify('should dereference and contain metadata about origin', async function () { | ||
const parseResult = await parse(fixturePath, { | ||
parse: { mediaType: mediaTypes.latest('json') }, | ||
}); | ||
const referenceElement = evaluate( | ||
'/definitions/schema', | ||
parseResult.api as SwaggerElement, | ||
); | ||
const dereferenced = await dereferenceApiDOM(referenceElement, { | ||
parse: { mediaType: mediaTypes.latest('json') }, | ||
resolve: { baseURI: `http://localhost:${httpPort}/root.json` }, | ||
}); | ||
|
||
assert.match(toValue(dereferenced.meta.get('ref-origin')), /\/ex\.json$/); | ||
}); | ||
}); | ||
}, | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
27 changes: 27 additions & 0 deletions
27
...gies/openapi-2/json-reference-object/fixtures/additional-ignored-fields/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 @@ | ||
[ | ||
{ | ||
"swagger": "2.0", | ||
"definitions": { | ||
"schema1": { | ||
"type": "object", | ||
"description": "ID of the user" | ||
}, | ||
"schema2": { | ||
"type": "object", | ||
"description": "ID of the user" | ||
}, | ||
"schema3": { | ||
"type": "object", | ||
"description": "ID of the user" | ||
}, | ||
"schema4": { | ||
"type": "object", | ||
"description": "ID of the user" | ||
}, | ||
"schema5": { | ||
"type": "number", | ||
"description": "external schema" | ||
} | ||
} | ||
} | ||
] |
8 changes: 8 additions & 0 deletions
8
...nce/strategies/openapi-2/json-reference-object/fixtures/additional-ignored-fields/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" | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...e/strategies/openapi-2/json-reference-object/fixtures/additional-ignored-fields/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,34 @@ | ||
{ | ||
"swagger": "2.0", | ||
"definitions": { | ||
"schema1": { | ||
"$ref": "#/definitions/schema2", | ||
"description": "description 1", | ||
"prop1": "value1", | ||
"prop2": "value2" | ||
}, | ||
"schema2": { | ||
"$ref": "#/definitions/schema3", | ||
"summary": "indirect summary 1", | ||
"prop1": "value1", | ||
"prop2": "value2" | ||
}, | ||
"schema3": { | ||
"$ref": "#/definitions/schema4", | ||
"description": "indirect description 1", | ||
"summary": "indirect summary 2", | ||
"prop1": "value1", | ||
"prop2": "value2" | ||
}, | ||
"schema4": { | ||
"type": "object", | ||
"description": "ID of the user" | ||
}, | ||
"schema5": { | ||
"$ref": "./ex.json#/definitions/externalSchema", | ||
"description": "pulled from external source", | ||
"prop1": "value1", | ||
"prop2": "value2" | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...ence/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
...ce/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
...ce/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" | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...s/openapi-2/json-reference-object/fixtures/external-circular-dependency/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,20 @@ | ||
[ | ||
{ | ||
"swagger": "2.0", | ||
"definitions": { | ||
"schema1": { | ||
"type": "object", | ||
"description": "schema2 description" | ||
}, | ||
"schema2": { | ||
"type": "object", | ||
"description": "schema2 description" | ||
}, | ||
"schema3": { | ||
"type": "string", | ||
"description": "externalSchema3 description" | ||
} | ||
} | ||
} | ||
|
||
] |
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" | ||
} | ||
} | ||
} |
Oops, something went wrong.