Skip to content

Commit

Permalink
Merge pull request #52 from sebastian-otto-axa/fix-response-assigne
Browse files Browse the repository at this point in the history
check if response exists bevor assigning to it
  • Loading branch information
anttiviljami authored Aug 19, 2024
2 parents fa5ad40 + b707969 commit 7694bef
Show file tree
Hide file tree
Showing 3 changed files with 255 additions and 3 deletions.
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

0 comments on commit 7694bef

Please sign in to comment.