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
6 changes: 4 additions & 2 deletions __tests__/fixtures/controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ export class CreateNestedBody {
users: CreateUserBody[]
}

export class CreatePostBody {
class CreatePostBodyBase {
@IsString({ each: true })
content: string[]
}

export class CreatePostBody extends CreatePostBodyBase {}

export class ListUsersQueryParams {
@IsOptional()
@IsEmail()
Expand Down Expand Up @@ -130,7 +132,7 @@ export class UsersController {

@Post('/:userId/posts')
createUserPost(
@Body({ required: true }) _body: CreatePostBody,
@Body({ required: true, type: CreatePostBody }) _body: CreatePostBody,
@BodyParam('token') _token: string
) {
return
Expand Down
164 changes: 94 additions & 70 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
// tslint:disable:no-implicit-dependencies no-submodule-imports
const { defaultMetadataStorage } = require('class-transformer/cjs/storage')
import { validationMetadatasToSchemas } from 'class-validator-jsonschema'
import * as fs from 'fs'
import { targetConstructorToSchema, validationMetadatasToSchemas } from 'class-validator-jsonschema'
import _merge from 'lodash.merge'
import { getMetadataArgsStorage } from 'routing-controllers'

import {
expressToOpenAPIPath,
getFullPath,
getOperationId,
parseRoutes,
routingControllersToSpec,
} from '../src'
import { expressToOpenAPIPath, getFullPath, getOperationId, parseRoutes, routingControllersToSpec } from '../src'
import { getRequestBody } from '../src/generateSpec'
import {
CreatePostBody,
RootController,
UserPostsController,
UsersController,
UsersController
} from './fixtures/controllers'

const { defaultMetadataStorage } = require('class-transformer/cjs/storage')

// Construct OpenAPI spec:
const storage = getMetadataArgsStorage()
const options = {
controllers: [UsersController, UserPostsController],
routePrefix: '/api',
routePrefix: '/api'
}
const routes = parseRoutes(storage, options)

Expand All @@ -31,7 +28,12 @@ describe('index', () => {
// Include component schemas parsed with class-validator-jsonschema:
const schemas = validationMetadatasToSchemas({
classTransformerMetadataStorage: defaultMetadataStorage,
refPointerPrefix: '#/components/schemas/',
refPointerPrefix: '#/components/schemas/'
})

schemas.CreatePostBody = targetConstructorToSchema(CreatePostBody, {
classTransformerMetadataStorage: defaultMetadataStorage,
refPointerPrefix: '#/components/schemas/'
})

const spec = routingControllersToSpec(storage, options, {
Expand All @@ -40,16 +42,17 @@ describe('index', () => {
securitySchemes: {
basicAuth: {
scheme: 'basic',
type: 'http',
type: 'http'
},
bearerAuth: {
scheme: 'bearer',
type: 'http',
},
},
type: 'http'
}
}
},
info: { title: 'My app', version: '1.2.0' },
info: { title: 'My app', version: '1.2.0' }
})
fs.writeFileSync('spec.test.json', JSON.stringify(spec, null, 2))
expect(spec).toEqual(require('./fixtures/spec.json'))
})

Expand All @@ -60,86 +63,86 @@ describe('index', () => {
method: 'listUsers',
route: '/',
target: UsersController,
type: 'get',
type: 'get'
},
{
method: 'listUsersInRange',
route: '/:from-:to',
target: UsersController,
type: 'get',
type: 'get'
},
{
method: 'getUser',
route: '/:userId?',
target: UsersController,
type: 'get',
type: 'get'
},
{
method: 'createUser',
route: '/',
target: UsersController,
type: 'post',
type: 'post'
},
{
method: 'createUserWithType',
route: '/withType',
target: UsersController,
type: 'post',
type: 'post'
},
{
method: 'createManyUsers',
route: '/',
target: UsersController,
type: 'put',
type: 'put'
},
{
method: 'createNestedUsers',
route: '/nested',
target: UsersController,
type: 'post',
type: 'post'
},
{
method: 'createUserPost',
route: '/:userId/posts',
target: UsersController,
type: 'post',
type: 'post'
},
{
method: 'deleteUsersByVersion',
route: '/:version(v?\\d{1}|all)',
target: UsersController,
type: 'delete',
type: 'delete'
},
{
method: 'putUserDefault',
route: undefined,
target: UsersController,
type: 'put',
type: 'put'
},
{
method: 'getUserPost',
route: '/:postId',
target: UserPostsController,
type: 'get',
type: 'get'
},
{
method: 'patchUserPost',
route: '/:postId',
target: UserPostsController,
type: 'patch',
type: 'patch'
},
{
method: 'getDefaultPath',
route: undefined,
target: RootController,
type: 'get',
type: 'get'
},
{
method: 'getStringPath',
route: '/stringPath',
target: RootController,
type: 'get',
},
type: 'get'
}
])
})

Expand Down Expand Up @@ -181,120 +184,141 @@ describe('index', () => {
const route = _merge({}, routes[0])
expect(getOperationId(route)).toEqual('UsersController.listUsers')

route.action.target = class AnotherController {}
route.action.target = class AnotherController {
}
route.action.method = 'anotherMethod'
expect(getOperationId(route)).toEqual('AnotherController.anotherMethod')
})
})

describe('getRequestBody', () => {
it('parse a single `body` metadata item into a single `object` schema', () => {
const schemas = validationMetadatasToSchemas({
classTransformerMetadataStorage: defaultMetadataStorage,
refPointerPrefix: '#/components/schemas/'
})
const route = routes.find((d) => d.action.method === 'createUser')!
expect(route).toBeDefined()
expect(getRequestBody(route)).toEqual({
expect(getRequestBody(route, schemas)).toEqual({
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/CreateUserBody',
},
},
$ref: '#/components/schemas/CreateUserBody'
}
}
},
description: 'CreateUserBody',
required: false,
required: false
})
})

it('parse a single `body` metadata item of array type into a single `object` schema', () => {
const route = routes.find((d) => d.action.method === 'createManyUsers')!
const schemas = validationMetadatasToSchemas({
classTransformerMetadataStorage: defaultMetadataStorage,
refPointerPrefix: '#/components/schemas/'
})
expect(route).toBeDefined()
expect(getRequestBody(route)).toEqual({
expect(getRequestBody(route, schemas)).toEqual({
content: {
'application/json': {
schema: {
items: {
$ref: '#/components/schemas/CreateUserBody',
$ref: '#/components/schemas/CreateUserBody'
},
type: 'array',
},
},
type: 'array'
}
}
},
description: 'CreateUserBody',
required: true,
required: true
})
})

it('parse a single `body-param` metadata item into a single `object` schema', () => {
const schemas = validationMetadatasToSchemas({
classTransformerMetadataStorage: defaultMetadataStorage,
refPointerPrefix: '#/components/schemas/'
})
const route = routes.find((d) => d.action.method === 'patchUserPost')!
expect(route).toBeDefined()
expect(getRequestBody(route)).toEqual({
expect(getRequestBody(route, schemas)).toEqual({
content: {
'application/json': {
schema: {
properties: {
token: {
type: 'string',
},
type: 'string'
}
},
required: [],
type: 'object',
},
},
},
type: 'object'
}
}
}
})
})

it('combine multiple `body-param` metadata items into a single `object` schema', () => {
const schemas = validationMetadatasToSchemas({
classTransformerMetadataStorage: defaultMetadataStorage,
refPointerPrefix: '#/components/schemas/'
})
const route = routes.find((d) => d.action.method === 'putUserDefault')!
expect(route).toBeDefined()
expect(getRequestBody(route)).toEqual({
expect(getRequestBody(route, schemas)).toEqual({
content: {
'application/json': {
schema: {
properties: {
limit: {
type: 'number',
type: 'number'
},
query: {
$ref: '#/components/schemas/UserQuery',
$ref: '#/components/schemas/UserQuery'
},
token: {
type: 'string',
},
type: 'string'
}
},
required: ['token'],
type: 'object',
},
},
},
type: 'object'
}
}
}
})
})

it('wrap `body` and `body-param` metadata items under a single `allOf` schema', () => {
const schemas = validationMetadatasToSchemas({
classTransformerMetadataStorage: defaultMetadataStorage,
refPointerPrefix: '#/components/schemas/'
})
const route = routes.find((d) => d.action.method === 'createUserPost')!
expect(route).toBeDefined()
expect(getRequestBody(route)).toEqual({
expect(getRequestBody(route, schemas)).toEqual({
content: {
'application/json': {
schema: {
allOf: [
{
$ref: '#/components/schemas/CreatePostBody',
$ref: '#/components/schemas/CreatePostBody'
},
{
properties: {
token: {
type: 'string',
},
type: 'string'
}
},
required: [],
type: 'object',
},
],
},
},
type: 'object'
}
]
}
}
},
description: 'CreatePostBody',
required: true,
required: true
})
})
})
Loading