Skip to content
Open
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
152 changes: 152 additions & 0 deletions __tests__/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,69 @@ describe('decorators', () => {
fourResponseSchemasMixedStatusCodeWithTwoArraySchemas() {
return
}

@Get('/responseSchemaExamples')
@ResponseSchema('BadRequestErrorObject', {
statusCode: 400,
examples: {
TENANT_NOT_FOUND: {
value: {
error: {
reason: 'TENANT_NOT_FOUND',
message: 'We could not find the requested tenant id.',
},
},
},
USER_NOT_FOUND: {
value: {
error: {
reason: 'USER_NOT_FOUND',
message: 'We could not find the requested user id.',
},
},
},
},
})
responseSchemaExamples() {
return
}

@Get('/responseSchemaMultipleExamples')
@ResponseSchema('BadRequestErrorObject1', {
statusCode: 400,
examples: {
TENANT_NOT_FOUND: {
value: {
error: {
reason: 'TENANT_NOT_FOUND',
message: 'We could not find the requested tenant id.',
},
},
},
USER_NOT_FOUND: {
value: {
error: {
reason: 'USER_NOT_FOUND',
message: 'We could not find the requested user id.',
},
},
},
},
})
@ResponseSchema('BadRequestErrorObject2', {
statusCode: 400,
examples: {
BANK_NOT_FOUND: {
value: {
error: 'Bad request',
message: 'We could not find the requested bank id.',
},
},
},
})
responseSchemaMultipleExamples() {
return
}
}

@Controller('/usershtml')
Expand Down Expand Up @@ -594,6 +657,95 @@ describe('decorators', () => {
},
})
})

it('applies @ResponseSchema using examples from options object', () => {
const operation = getOperation(routes.responseSchemaExamples, {})
expect(operation.responses).toEqual({
'200': {
content: {
'application/json': {},
},
description: 'Successful response',
},
'400': {
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/BadRequestErrorObject',
},
examples: {
TENANT_NOT_FOUND: {
value: {
error: {
message: 'We could not find the requested tenant id.',
reason: 'TENANT_NOT_FOUND',
},
},
},
USER_NOT_FOUND: {
value: {
error: {
message: 'We could not find the requested user id.',
reason: 'USER_NOT_FOUND',
},
},
},
},
},
},
description: '',
},
})
})

it('applies multiple @ResponseSchema with same status code using examples from options objects', () => {
const operation = getOperation(routes.responseSchemaMultipleExamples, {})
expect(operation.responses).toEqual({
'200': {
content: {
'application/json': {},
},
description: 'Successful response',
},
'400': {
content: {
'application/json': {
schema: {
oneOf: [
{ $ref: '#/components/schemas/BadRequestErrorObject1' },
{ $ref: '#/components/schemas/BadRequestErrorObject2' },
],
},
examples: {
TENANT_NOT_FOUND: {
value: {
error: {
message: 'We could not find the requested tenant id.',
reason: 'TENANT_NOT_FOUND',
},
},
},
USER_NOT_FOUND: {
value: {
error: {
message: 'We could not find the requested user id.',
reason: 'USER_NOT_FOUND',
},
},
},
BANK_NOT_FOUND: {
value: {
error: 'Bad request',
message: 'We could not find the requested bank id.',
},
},
},
},
},
description: '',
},
})
})
})

describe('@OpenAPI-decorated class', () => {
Expand Down
19 changes: 18 additions & 1 deletion src/decorators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import _merge from 'lodash.merge'
import { OperationObject, ReferenceObject, ResponsesObject } from 'openapi3-ts'
import {
ExamplesObject,
OperationObject,
ReferenceObject,
ResponsesObject,
} from 'openapi3-ts'
import 'reflect-metadata'

import { getContentType, getStatusCode, IRoute } from './index'
Expand Down Expand Up @@ -91,13 +96,15 @@ export function ResponseSchema(
description?: string
statusCode?: string | number
isArray?: boolean
examples?: ExamplesObject
} = {}
) {
const setResponseSchema = (source: OperationObject, route: IRoute) => {
const contentType = options.contentType || getContentType(route)
const description = options.description || ''
const isArray = options.isArray || false
const statusCode = (options.statusCode || getStatusCode(route)) + ''
const examples = options.examples || undefined

let responseSchemaName = ''
if (typeof responseClass === 'function' && responseClass.name) {
Expand All @@ -110,12 +117,22 @@ export function ResponseSchema(
const reference: ReferenceObject = {
$ref: `#/components/schemas/${responseSchemaName}`,
}

const schema = isArray ? { items: reference, type: 'array' } : reference

const oldExamples =
source.responses[statusCode]?.content[contentType]?.examples

const newExamples = oldExamples
? { ...oldExamples, ...examples }
: examples

const responses: ResponsesObject = {
[statusCode]: {
content: {
[contentType]: {
schema,
examples: newExamples,
},
},
description,
Expand Down