diff --git a/src/common/strip-definition-presets.test.ts b/src/common/strip-definition-presets.test.ts new file mode 100644 index 0000000..74f5d19 --- /dev/null +++ b/src/common/strip-definition-presets.test.ts @@ -0,0 +1,250 @@ +import { PRESETS, stripDefinition } from './strip-definition'; +import * as testFixtures from '../__tests__/test-fixtures'; + +describe('presets', () => { + it.each([ + ['default', PRESETS.default,{ + "components": { + "schemas": { + "Response": { + "type": "object" + } + } + }, + "info": { + "title": "", + "version": "" + }, + "openapi": "3.0.3", + "paths": { + "/path1": { + "post": { + "operationId": "operationId", + "responses": { + "201": { + "content": { + "application/json": { + "schema": { + "type": "object" + } + } + }, + "description": "" + }, + "400": { + "description": "" + } + } + } + }, + "/path2": { + "get": { + "operationId": "operationId", + "responses": { + "200": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Response" + } + } + }, + "description": "" + }, + "400": { + "description": "" + } + } + } + } + }, + "servers": [ + { + "url": "/test1" + }, + { + "url": "/test2" + }, + { + "url": "/test3" + } + ] + }], + ['all', PRESETS.all,{ + "components": {}, + "info": { + "title": "", + "version": "" + }, + "openapi": "3.0.3", + "paths": { + "/path1": { + "post": { + "operationId": "operationId", + "responses": {} + } + }, + "/path2": { + "get": { + "operationId": "operationId", + "responses": {} + } + } + } + }], + ['openapi_client_axios', PRESETS.openapi_client_axios,{ + "components": {}, + "info": { + "title": "", + "version": "" + }, + "openapi": "3.0.3", + "paths": { + "/path1": { + "post": { + "operationId": "operationId", + "responses": {} + } + }, + "/path2": { + "get": { + "operationId": "operationId", + "responses": {} + } + } + }, + "servers": [{ + "url": "/test1" + }] + }], + ['openapi_backend', PRESETS.openapi_backend,{ + "components": { + "schemas": { + "Response": { + "type": "object" + } + } + }, + "info": { + "title": "", + "version": "" + }, + "openapi": "3.0.3", + "paths": { + "/path1": { + "post": { + "operationId": "operationId", + "responses": { + "201": { + "content": { + "application/json": { + "schema": { + "type": "object" + } + } + }, + "description": "" + }, + "400": { + "description": "" + } + } + } + }, + "/path2": { + "get": { + "operationId": "operationId", + "responses": { + "200": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Response" + } + } + }, + "description": "" + }, + "400": { + "description": "" + } + } + } + } + } + }] + ]) ('should strip for %s preset', (label, preset, expected) => { + // given + const document = testFixtures.createDefinition({ + openapi: '3.0.3', + info: { + title: 'title', + description: 'description', + version: '1.0.0', + contact: { + name: 'test', + email: 'test@example.com', + } + }, + servers: [ + { url: '/test1', description: 'description' }, + { url: '/test2', description: 'description' }, + { url: '/test3' } + ], + paths: { + '/path1': { + description: 'description', + post: testFixtures.createOperation({ + responses: { + '201': { + description: 'Created', + content: { + 'application/json': { + schema: { + type: 'object', + }, + }, + }, + }, + '400': { + description: 'Bad Request', + }, + }, + }), + }, + '/path2': { + get: testFixtures.createOperation({ + responses: { + '200': { + description: 'Created', + content: { + 'application/json': { + schema: { + $ref: '#/components/schemas/Response', + }, + }, + }, + }, + '400': { + description: 'Bad Request', + }, + }, + }), + }, + }, + components: { + schemas: { + Response: { + type: 'object', + } + } + } + }) + + // when + const output = stripDefinition(document, preset); + + // then + expect(output).toEqual(expected) + }) +}) diff --git a/src/common/strip-definition.test.ts b/src/common/strip-definition.test.ts index 6bb0825..9c3f4ba 100644 --- a/src/common/strip-definition.test.ts +++ b/src/common/strip-definition.test.ts @@ -14,7 +14,6 @@ describe('stripDefinition', () => { expect(output).not.toBe(document); }) - describe('opts.replaceInfo', () => { it('should replace info', () => { // given @@ -1019,12 +1018,13 @@ describe('stripDefinition', () => { }) }) - describe('replaceResponses', () => { + describe('opts.replaceResponses', () => { it('should replace responses with minimal default response', () => { // given const document = testFixtures.createDefinition({ paths: { '/path1': { + description: 'description', post: testFixtures.createOperation({ responses: { '201': { diff --git a/src/common/strip-definition.ts b/src/common/strip-definition.ts index 9c3d866..3d3fc2c 100644 --- a/src/common/strip-definition.ts +++ b/src/common/strip-definition.ts @@ -259,7 +259,9 @@ export const stripDefinition = (document: Definition, options: StripOptions & { if (output.paths[path]) { for (const method in output.paths[path]) { if (output.paths[path][method]) { - output.paths[path][method].responses = {} + if(output.paths[path][method].responses) { + output.paths[path][method].responses = {} + } } } }