openapi-modifier
Version:
This package allows you to automate the process of modifying OpenAPI specifications by applying a set of predefined rules
504 lines (503 loc) • 21.3 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('remove-unused-components rule', () => {
test('regular', () => {
const fakeLogger = global.createFakeLogger();
const fakeOpenAPIFile = global.createFakeOpenAPIFile({
components: {
schemas: {
Date: {
type: 'string',
},
AttributesDTO: {
type: 'string',
},
Pet: {
type: 'object',
properties: {
date: {
$ref: '#/components/schemas/Date',
},
attributes: {
type: 'object',
additionalProperties: {
$ref: '#/components/schemas/AttributesDTO',
},
},
testWithOf: {
$ref: '#/components/schemas/TestObjectWithOfDTO',
},
testWithDiscriminator: {
$ref: '#/components/schemas/TestObjectWithDiscriminatorDto',
},
},
},
TestSchemaRefOneOfDTO: {
type: 'string',
},
TestSchemaRefAllOfDTO: {
type: 'string',
},
TestSchemaRefAnyOfDTO: {
type: 'string',
},
TestObjectWithOfDTO: {
"type": "object",
"properties": {
"anyOfProperty": {
type: 'object',
anyOf: [
{
$ref: '#/components/schemas/TestSchemaRefAnyOfDTO',
}
]
},
"allOfProperty": {
type: 'object',
allOf: [
{
$ref: '#/components/schemas/TestSchemaRefAllOfDTO',
}
]
},
"oneOfProperty": {
type: 'object',
oneOf: [
{
$ref: '#/components/schemas/TestSchemaRefOneOfDTO',
}
]
},
}
},
"TestDiscriminatorVariantObject": {
"type": "object",
"properties": {
"foo": {
"type": "string"
}
}
},
"TestObjectWithDiscriminatorDto": {
"discriminator": {
"mapping": {
"TEST_DISCRIMINATOR": "#/components/schemas/TestDiscriminatorVariantObject"
},
"propertyName": "action"
},
"type": "object",
"properties": {
"action": {
"description": "Test discriminator property",
"enum": [
"TEST_DISCRIMINATOR"
],
"type": "string"
}
}
},
Notification: {
type: 'object',
properties: {
date: {
$ref: '#/components/schemas/Date',
},
},
},
Notifications: {
type: 'array',
items: {
$ref: '#/components/schemas/Notification',
},
},
},
},
paths: {
'/pets': {
get: {
summary: 'Get all pets',
responses: {
'200': {
description: '',
content: {
'*/*': {
schema: {
type: 'array',
items: {
$ref: '#/components/schemas/Pet',
},
},
},
},
},
},
},
},
},
});
expect(index_1.default.processDocument(fakeOpenAPIFile, {}, fakeLogger, { ruleName: '' })).toEqual(Object.assign(Object.assign({}, fakeOpenAPIFile), { document: Object.assign(Object.assign({}, fakeOpenAPIFile.document), { components: {
schemas: {
Date: {
type: 'string',
},
AttributesDTO: {
type: 'string',
},
Pet: {
type: 'object',
properties: {
date: {
$ref: '#/components/schemas/Date',
},
attributes: {
type: 'object',
additionalProperties: {
$ref: '#/components/schemas/AttributesDTO',
},
},
testWithOf: {
$ref: '#/components/schemas/TestObjectWithOfDTO',
},
testWithDiscriminator: {
$ref: '#/components/schemas/TestObjectWithDiscriminatorDto',
},
},
},
TestSchemaRefOneOfDTO: {
type: 'string',
},
TestSchemaRefAllOfDTO: {
type: 'string',
},
TestSchemaRefAnyOfDTO: {
type: 'string',
},
TestObjectWithOfDTO: {
"type": "object",
"properties": {
"anyOfProperty": {
type: 'object',
anyOf: [
{
$ref: '#/components/schemas/TestSchemaRefAnyOfDTO',
}
]
},
"allOfProperty": {
type: 'object',
allOf: [
{
$ref: '#/components/schemas/TestSchemaRefAllOfDTO',
}
]
},
"oneOfProperty": {
type: 'object',
oneOf: [
{
$ref: '#/components/schemas/TestSchemaRefOneOfDTO',
}
]
},
}
},
"TestDiscriminatorVariantObject": {
"type": "object",
"properties": {
"foo": {
"type": "string"
}
}
},
"TestObjectWithDiscriminatorDto": {
"discriminator": {
"mapping": {
"TEST_DISCRIMINATOR": "#/components/schemas/TestDiscriminatorVariantObject"
},
"propertyName": "action"
},
"type": "object",
"properties": {
"action": {
"description": "Test discriminator property",
"enum": [
"TEST_DISCRIMINATOR"
],
"type": "string"
}
}
},
},
}, paths: {
'/pets': {
get: {
summary: 'Get all pets',
responses: {
'200': {
description: '',
content: {
'*/*': {
schema: {
type: 'array',
items: {
$ref: '#/components/schemas/Pet',
},
},
},
},
},
},
},
},
} }) }));
expect(fakeLogger.warning).toBeCalledTimes(0);
});
test('regular, usage config.ignore', () => {
const fakeLogger = global.createFakeLogger();
const fakeOpenAPIFile = global.createFakeOpenAPIFile({
components: {
schemas: {
Date: {
type: 'string',
},
AttributesDTO: {
type: 'string',
},
Pet: {
type: 'object',
properties: {
date: {
$ref: '#/components/schemas/Date',
},
attributes: {
type: 'object',
additionalProperties: {
$ref: '#/components/schemas/AttributesDTO',
},
},
},
},
TestIgnoreDescriptionDTO: {
type: 'string',
},
Notification: {
type: 'object',
properties: {
date: {
$ref: '#/components/schemas/Date',
},
},
},
Notifications: {
type: 'array',
items: {
$ref: '#/components/schemas/Notification',
},
},
},
},
paths: {
'/pets': {
get: {
summary: 'Get all pets',
responses: {
'200': {
description: '',
content: {
'*/*': {
schema: {
type: 'array',
items: {
$ref: '#/components/schemas/Pet',
},
},
},
},
},
},
},
},
},
});
expect(index_1.default.processDocument(fakeOpenAPIFile, {
ignore: [{
componentName: 'Notifications',
},
'TestIgnoreDescriptionDTO'
],
}, fakeLogger, { ruleName: '' })).toEqual(Object.assign(Object.assign({}, fakeOpenAPIFile), { document: Object.assign(Object.assign({}, fakeOpenAPIFile.document), { paths: Object.assign({}, fakeOpenAPIFile.document.paths) }) }));
expect(fakeLogger.warning).toBeCalledTimes(0);
});
test('regular, show not usaged warning', () => {
const fakeLogger = global.createFakeLogger();
const fakeOpenAPIFile = global.createFakeOpenAPIFile({
components: {
schemas: {
Date: {
type: 'string',
},
AttributesDTO: {
type: 'string',
},
Pet: {
type: 'object',
properties: {
date: {
$ref: '#/components/schemas/Date',
},
attributes: {
type: 'object',
additionalProperties: {
$ref: '#/components/schemas/AttributesDTO',
},
},
},
},
Notification: {
type: 'object',
properties: {
date: {
$ref: '#/components/schemas/Date',
},
},
},
Notifications: {
type: 'array',
items: {
$ref: '#/components/schemas/Notification',
},
},
},
},
paths: {
'/pets': {
get: {
summary: 'Get all pets',
responses: {
'200': {
description: '',
content: {
'*/*': {
schema: {
type: 'array',
items: {
$ref: '#/components/schemas/Pet',
},
},
},
},
},
},
},
},
},
});
expect(index_1.default.processDocument(fakeOpenAPIFile, {
ignore: [{
componentName: 'Notifications',
}, {
componentName: 'TestNotUsagedComponent',
}],
}, fakeLogger, { ruleName: '' })).toEqual(Object.assign(Object.assign({}, fakeOpenAPIFile), { document: Object.assign(Object.assign({}, fakeOpenAPIFile.document), { paths: Object.assign({}, fakeOpenAPIFile.document.paths) }) }));
expect(fakeLogger.warning).toBeCalledLoggerMethod(/Not usaged ignore component/, 1);
});
test('regular, show delete info', () => {
const fakeLogger = global.createFakeLogger();
const fakeOpenAPIFile = global.createFakeOpenAPIFile({
components: {
schemas: {
AttributesDTO: {
type: 'string',
},
Pet: {
type: 'string',
},
},
},
paths: {
'/pets': {
get: {
summary: 'Get all pets',
responses: {
'200': {
description: '',
content: {
'*/*': {
schema: {
type: 'array',
items: {
$ref: '#/components/schemas/Pet',
},
},
},
},
},
},
},
},
},
});
expect(index_1.default.processDocument(fakeOpenAPIFile, {
printDeletedComponents: 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: {
Pet: {
type: 'string',
},
} }) }) }));
expect(fakeLogger.info).toBeCalledLoggerMethod(/Deleted/, 1);
});
test('regular, show delete info', () => {
const fakeLogger = global.createFakeLogger();
const fakeOpenAPIFile = global.createFakeOpenAPIFile({
components: {
schemas: {
AttributesDTO: {
type: 'string',
},
EventNotificationDto: {
type: 'string',
},
Pet: {
type: 'string',
},
},
},
paths: {
'/pets': {
get: {
summary: 'Get all pets',
responses: {
'200': {
description: '',
content: {
'*/*': {
schema: {
type: 'array',
items: {
$ref: '#/components/schemas/Pet',
},
},
},
},
},
},
},
},
},
});
expect(index_1.default.processDocument(fakeOpenAPIFile, {
ignore: [/NotificationDto/]
}, fakeLogger, { ruleName: '' })).toEqual(Object.assign(Object.assign({}, fakeOpenAPIFile), { document: Object.assign(Object.assign({}, fakeOpenAPIFile.document), { components: Object.assign(Object.assign({}, fakeOpenAPIFile.document.components), { schemas: {
EventNotificationDto: {
type: 'string',
},
Pet: {
type: 'string',
},
} }) }) }));
});
});