Skip to content

Commit

Permalink
test(reference): add tests for OpenAPI 2.0 JSONReference deref
Browse files Browse the repository at this point in the history
Refs #3102
  • Loading branch information
char0n committed Nov 23, 2023
1 parent aa0aad1 commit c0910d1
Show file tree
Hide file tree
Showing 54 changed files with 1,188 additions and 0 deletions.
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$/);
});
});
},
);
});
});
});
});
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"
}
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"definitions": {
"externalSchema": {
"type": "number",
"description": "external schema"
}
}
}
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"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"definitions": {
"externalSchema": {
"$ref": "./root.json#/definitions/schema"
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"swagger": "2.0",
"definitions": {
"schema": {
"$ref": "./ex.json#/definitions/externalSchema"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"swagger": "2.0",
"definitions": {
"schema": {
"$ref": "#/definitions/schema"
}
}
}
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"
}
}
}

]
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"
}
}
}
Loading

0 comments on commit c0910d1

Please sign in to comment.