openapi-modifier
Version:
This package allows you to automate the process of modifying OpenAPI specifications by applying a set of predefined rules
203 lines (202 loc) • 8.1 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('patch-endpoint-schema rule', () => {
test('regular, use merge', () => {
const fakeLogger = global.createFakeLogger();
const fakeOpenAPIFile = global.createFakeOpenAPIFile({
paths: {
'/all-pets': {
get: {
summary: '',
responses: {
'401': {
description: 'Test 401',
content: {
'*/*': {
schema: {
type: 'object',
},
},
},
},
},
},
},
},
});
expect(index_1.default.processDocument(fakeOpenAPIFile, {
patchMethod: 'merge',
endpointDescriptor: {
path: '/all-pets',
method: 'GET',
},
schemaDiff: {
responses: {
'200': {
description: 'Test 200',
content: {
'*/*': {
schema: {
type: 'object',
},
},
},
},
},
},
}, fakeLogger, { ruleName: '' })).toEqual(Object.assign(Object.assign({}, fakeOpenAPIFile), { document: Object.assign(Object.assign({}, fakeOpenAPIFile.document), { paths: {
'/all-pets': {
get: {
summary: '',
responses: {
'200': {
description: 'Test 200',
content: {
'*/*': {
schema: {
type: 'object',
},
},
},
},
},
},
},
} }) }));
expect(fakeLogger.warning).toBeCalledTimes(0);
});
test('regular, use deepmerge', () => {
const fakeLogger = global.createFakeLogger();
const fakeOpenAPIFile = global.createFakeOpenAPIFile({
paths: {
'/pets': {
get: {
summary: '',
responses: {
'401': {
description: 'Test 401',
content: {
'*/*': {
schema: {
type: 'object',
},
},
},
},
},
},
},
},
});
expect(index_1.default.processDocument(fakeOpenAPIFile, {
patchMethod: 'deepmerge',
endpointDescriptor: {
path: '/pets',
method: 'GET',
},
schemaDiff: {
responses: {
'200': {
description: 'Test 200',
content: {
'*/*': {
schema: {
type: 'object',
},
},
},
},
},
},
}, fakeLogger, { ruleName: '' })).toEqual(Object.assign(Object.assign({}, fakeOpenAPIFile), { document: Object.assign(Object.assign({}, fakeOpenAPIFile.document), { paths: {
'/pets': {
get: {
summary: '',
responses: {
'200': {
description: 'Test 200',
content: {
'*/*': {
schema: {
type: 'object',
},
},
},
},
'401': {
description: 'Test 401',
content: {
'*/*': {
schema: {
type: 'object',
},
},
},
},
},
},
},
} }) }));
expect(fakeLogger.warning).toBeCalledTimes(0);
});
test('regular, use merge with correction', () => {
const fakeLogger = global.createFakeLogger();
const fakeOpenAPIFile = global.createFakeOpenAPIFile({
paths: {
'/pets': {
get: {
summary: '',
responses: {
'200': {
description: 'Test 200',
content: {
'*/*': {
schema: {
enum: ['1', '2'],
type: 'string',
},
},
},
},
},
},
},
},
});
expect(index_1.default.processDocument(fakeOpenAPIFile, {
patchMethod: 'merge',
endpointDescriptor: {
path: '/pets',
method: 'GET',
},
endpointDescriptorCorrection: 'responses.200.content.*/*.schema',
schemaDiff: {
enum: ['3', '4'],
},
}, fakeLogger, { ruleName: '' })).toEqual(Object.assign(Object.assign({}, fakeOpenAPIFile), { document: Object.assign(Object.assign({}, fakeOpenAPIFile.document), { paths: {
'/pets': {
get: {
summary: '',
responses: {
'200': {
description: 'Test 200',
content: {
'*/*': {
schema: {
enum: ['3', '4'],
type: 'string',
},
},
},
},
},
},
},
} }) }));
expect(fakeLogger.warning).toBeCalledTimes(0);
});
});