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

Add support for multiple @ResponseSchema decorators using oneOf. #58

Merged
merged 6 commits into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
142 changes: 142 additions & 0 deletions __tests__/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,37 @@ describe('decorators', () => {
multipleResponseSchemas() {
return
}

@Get('/twoResponseSchemaSameStatusCode')
@ResponseSchema('SuccessObject1')
@ResponseSchema('SuccessObject2')
twoResponseSchemasSameStatusCode() {
return
}

@Get('/threeResponseSchemaSameStatusCode')
@ResponseSchema('SuccessObject1')
@ResponseSchema('SuccessObject2')
@ResponseSchema('SuccessObject3')
threeResponseSchemasSameStatusCode() {
return
}

@Get('/twoResponseSchemaSameStatusCodeWithOneArraySchema')
@ResponseSchema('SuccessObjects1', { isArray: true })
@ResponseSchema('SuccessObject2')
twoResponseSchemaSameStatusCodeWithOneArraySchema() {
return
}

@Get('/fourResponseSchemasMixedStatusCodeWithTwoArraySchemas')
@ResponseSchema('SuccessObjects1', { isArray: true })
@ResponseSchema('CreatedObject2', { statusCode: 201 })
@ResponseSchema('CreatedObjects3', { statusCode: 201, isArray: true})
@ResponseSchema('BadRequestObject4', { statusCode: 400 })
fourResponseSchemasMixedStatusCodeWithTwoArraySchemas() {
return
}
}

@Controller('/usershtml')
Expand Down Expand Up @@ -436,8 +467,119 @@ describe('decorators', () => {
}
})
})

it('applies two @ResponseSchema with same status code', () => {
const operation = getOperation(routes.twoResponseSchemasSameStatusCode, {})
expect(operation.responses).toEqual({
'200': {
content: {
'application/json': {
schema: {
oneOf: [
{$ref: '#/components/schemas/SuccessObject1'},
{$ref: '#/components/schemas/SuccessObject2'},
]
}
}
},
description: ''
}
})
})

it('applies three @ResponseSchema with same status code', () => {
const operation = getOperation(routes.threeResponseSchemasSameStatusCode, {})
expect(operation.responses).toEqual({
'200': {
content: {
'application/json': {
schema: {
oneOf: [
{$ref: '#/components/schemas/SuccessObject1'},
{$ref: '#/components/schemas/SuccessObject2'},
{$ref: '#/components/schemas/SuccessObject3'},
]
}
}
},
description: ''
}
})
})

it('applies two @ResponseSchema with same status code, where one of them is an array', () => {
const operation = getOperation(routes.twoResponseSchemaSameStatusCodeWithOneArraySchema, {})
expect(operation.responses).toEqual({
'200': {
content: {
'application/json': {
schema: {
oneOf: [
{
items: {
$ref: '#/components/schemas/SuccessObjects1'
},
type: 'array'
},
{$ref: '#/components/schemas/SuccessObject2'},
]
}
}
},
description: ''
}
})
})

it('applies four @ResponseSchema with mixed status code, where two of them are arrays', () => {
const operation = getOperation(routes.fourResponseSchemasMixedStatusCodeWithTwoArraySchemas, {})
expect(operation.responses).toEqual({
'200': {
content: {
'application/json': {
schema: {
items: {
$ref: '#/components/schemas/SuccessObjects1'
},
type: 'array'
}
}
},
description: ''
},
'201': {
content: {
'application/json': {
schema: {
oneOf: [
{$ref: '#/components/schemas/CreatedObject2'},
{
items: {
$ref: '#/components/schemas/CreatedObjects3'
},
type: 'array'
}
]
}
}
},
description: ''
},
'400': {
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/BadRequestObject4'
}
}
},
description: ''
}
})
})
})


describe('@OpenAPI-decorated class', () => {
let routes: { [method: string]: IRoute }

Expand Down
24 changes: 24 additions & 0 deletions src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,30 @@ export function ResponseSchema(
},
}

const oldSchema =
source.responses[statusCode]?.content[contentType].schema
if (oldSchema?.$ref || oldSchema?.items?.$ref) {
// case where we're adding multiple schemas under single statuscode/contentType
// with single $ref
const isOldSchemaArray = oldSchema?.items?.$ref

// delete old schema and integrate into current schema under oneOf
const schemaObj = { oneOf: [{ ...oldSchema }, schema] }
responses[statusCode].content[contentType].schema = schemaObj

if (isOldSchemaArray) {
delete oldSchema.items
delete oldSchema.type
} else {
delete oldSchema.$ref
}
} else if (oldSchema?.oneOf) {
// case where there's already multiple existing schemas
const oneOf = _.concat([...oldSchema.oneOf], schema)
responses[statusCode].content[contentType].schema = { oneOf }
delete oldSchema.oneOf
}

return _.merge({}, source, { responses })
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking all the deletes makes this a bit hard to read. How about splitting into two branches, something like

if (oldSchema) {
  const newSchema = {};

  // ...construct newSchema

  source.responses[statusCode].content[contentType].schema = newSchema
  return source
} else {
  return _.merge({}, source, { responses }) // just as before
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed here

}

Expand Down