Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check if response exists bevor assigning to it #52

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
250 changes: 250 additions & 0 deletions src/common/strip-definition-presets.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
4 changes: 2 additions & 2 deletions src/common/strip-definition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ describe('stripDefinition', () => {
expect(output).not.toBe(document);
})


describe('opts.replaceInfo', () => {
it('should replace info', () => {
// given
Expand Down Expand Up @@ -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': {
Expand Down
4 changes: 3 additions & 1 deletion src/common/strip-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
}
}
}
}
Expand Down
Loading