openapi-modifier
Version:
This package allows you to automate the process of modifying OpenAPI specifications by applying a set of predefined rules
213 lines (212 loc) • 9.7 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = __importDefault(require("./index"));
describe('merge-openapi-spec rule', () => {
test('regular', () => {
const fakeLogger = global.createFakeLogger();
const fakeOpenAPIFile = global.createFakeOpenAPIFile({
paths: {
'/pets': {
get: {
summary: 'List all pets',
responses: {
'200': {
description: '',
content: {
'*/*': {
schema: {
type: 'object',
},
},
},
},
},
},
},
},
});
expect(index_1.default.processDocument(fakeOpenAPIFile, {
path: __dirname + '/__mocks__/new-api-for-new-feature.yaml',
}, fakeLogger, { ruleName: '' })).toEqual(Object.assign(Object.assign({}, fakeOpenAPIFile), { document: Object.assign(Object.assign({}, fakeOpenAPIFile.document), { components: {
schemas: {
Pet: {
type: 'object',
required: ['id', 'name'],
properties: {
id: {
type: 'integer',
format: 'int64',
},
name: {
type: 'string',
},
},
},
},
}, paths: {
'/notifications': {
get: {
summary: 'Get all notifications',
responses: {
'200': {
content: {
'*/*': {
schema: {
type: 'array',
items: {
$ref: '#/components/schemas/Pet',
},
},
},
},
},
},
},
},
'/pets': {
get: {
summary: 'List all pets',
responses: {
'200': {
description: '',
content: {
'*/*': {
schema: {
type: 'object',
},
},
},
},
},
},
},
} }) }));
expect(fakeLogger.warning).toBeCalledTimes(0);
});
test('collision operations/paths', () => {
const fakeLogger = global.createFakeLogger();
const fakeOpenAPIFile = global.createFakeOpenAPIFile({
paths: {
'/notifications': {
get: {
summary: 'List all pets',
responses: {
'200': {
description: '',
content: {
'*/*': {
schema: {
type: 'object',
},
},
},
},
},
},
},
},
});
expect(index_1.default.processDocument(fakeOpenAPIFile, {
path: __dirname + '/__mocks__/collision/paths.yaml',
}, fakeLogger, { ruleName: '' })).toEqual(Object.assign(Object.assign({}, fakeOpenAPIFile), { document: Object.assign({}, fakeOpenAPIFile.document) }));
expect(fakeLogger.warning).toBeCalledTimes(0);
expect(fakeLogger.errorMessage).toBeCalledLoggerMethod(/operaion conflicts/, 1);
});
test('collision operations/paths, usage config.ignoreOperationCollisions', () => {
const fakeLogger = global.createFakeLogger();
const fakeOpenAPIFile = global.createFakeOpenAPIFile({
paths: {
'/notifications': {
get: {
summary: 'List all pets',
responses: {
'200': {
description: '',
content: {
'*/*': {
schema: {
type: 'object',
},
},
},
},
},
},
},
},
});
expect(index_1.default.processDocument(fakeOpenAPIFile, {
path: __dirname + '/__mocks__/collision/paths.yaml',
ignoreOperationCollisions: true,
}, fakeLogger, { ruleName: '' })).toEqual(Object.assign(Object.assign({}, fakeOpenAPIFile), { document: Object.assign(Object.assign({}, fakeOpenAPIFile.document), { paths: Object.assign(Object.assign({}, fakeOpenAPIFile.document.paths), { '/notifications': {
get: {
summary: 'Get all notifications',
responses: {
'200': {
content: {
'*/*': {
schema: {
type: 'array',
items: {
$ref: '#/components/schemas/Pet',
},
},
},
},
description: '',
},
},
},
} }) }) }));
expect(fakeLogger.warning).toBeCalledTimes(0);
expect(fakeLogger.error).toBeCalledTimes(0);
});
test('collision components', () => {
const fakeLogger = global.createFakeLogger();
const fakeOpenAPIFile = global.createFakeOpenAPIFile({
components: {
schemas: {
Pet: {
type: 'string',
},
},
},
});
expect(index_1.default.processDocument(fakeOpenAPIFile, {
path: __dirname + '/__mocks__/collision/components.yaml',
}, fakeLogger, { ruleName: '' })).toEqual(Object.assign(Object.assign({}, fakeOpenAPIFile), { document: Object.assign({}, fakeOpenAPIFile.document) }));
expect(fakeLogger.warning).toBeCalledTimes(0);
expect(fakeLogger.errorMessage).toBeCalledLoggerMethod(/component conflicts/, 1);
});
test('collision components, usage config.ignoreComponentCollisions', () => {
var _a;
const fakeLogger = global.createFakeLogger();
const fakeOpenAPIFile = global.createFakeOpenAPIFile({
components: {
schemas: {
Pet: {
type: 'string',
},
},
},
});
expect(index_1.default.processDocument(fakeOpenAPIFile, {
path: __dirname + '/__mocks__/collision/components.yaml',
ignoreComponentCollisions: true,
}, fakeLogger, { ruleName: '' })).toEqual(Object.assign(Object.assign({}, fakeOpenAPIFile), { document: Object.assign(Object.assign({}, fakeOpenAPIFile.document), { components: Object.assign(Object.assign({}, fakeOpenAPIFile.document.components), { schemas: Object.assign(Object.assign({}, (_a = fakeOpenAPIFile.document.components) === null || _a === void 0 ? void 0 : _a.schemas), { Pet: {
type: 'object',
required: ['id'],
properties: {
id: {
type: 'integer',
format: 'int64',
},
},
} }) }) }) }));
expect(fakeLogger.warning).toBeCalledTimes(0);
expect(fakeLogger.error).toBeCalledTimes(0);
});
});