Skip to content

Commit

Permalink
Demonstrate nested classes in sample project
Browse files Browse the repository at this point in the history
  • Loading branch information
epiphone committed Nov 28, 2020
1 parent bb9e99e commit bea6f3d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
32 changes: 26 additions & 6 deletions sample/01-basic/UsersController.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,56 @@
import { IsOptional, IsString, MaxLength, IsNumber, IsPositive } from 'class-validator'
import { Type } from 'class-transformer'
import {
IsOptional,
IsString,
MaxLength,
IsNumber,
IsPositive,
ValidateNested,
} from 'class-validator'
import {
Body,
Get,
JsonController,
Param,
Post,
Put,
QueryParams
QueryParams,
} from 'routing-controllers'
import { OpenAPI } from 'routing-controllers-openapi'

class Child {
@IsString()
name: string
}

class CreateUserBody {
@IsString()
name: string

@IsOptional()
@MaxLength(20, { each: true })
hobbies: string[]

@ValidateNested()
child: Child

@ValidateNested({ each: true })
@Type(() => Child)
children: Child[]
}

class PaginationQuery {
@IsNumber()
@IsPositive()
public limit: number;
public limit: number

@IsNumber()
@IsOptional()
public offset?: number;
public offset?: number
}

@OpenAPI({
security: [{ basicAuth: [] }]
security: [{ basicAuth: [] }],
})
@JsonController('/users')
export class UsersController {
Expand All @@ -39,7 +59,7 @@ export class UsersController {
getAll(@QueryParams() query: PaginationQuery) {
return [
{ id: 1, name: 'First user!', hobbies: [] },
{ id: 2, name: 'Second user!', hobbies: ['fishing', 'cycling'] }
{ id: 2, name: 'Second user!', hobbies: ['fishing', 'cycling'] },
]
}

Expand Down
24 changes: 13 additions & 11 deletions sample/01-basic/app.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import 'reflect-metadata'
import { defaultMetadataStorage } from 'class-transformer/storage' // tslint:disable-line:no-submodule-imports
import { validationMetadatasToSchemas } from 'class-validator-jsonschema'
import { Express } from 'express'
import {
createExpressServer,
getMetadataArgsStorage
getMetadataArgsStorage,
} from 'routing-controllers'
import { routingControllersToSpec } from 'routing-controllers-openapi'
import * as swaggerUiExpress from 'swagger-ui-express';
import * as swaggerUiExpress from 'swagger-ui-express'

import { UsersController } from './UsersController'

const routingControllersOptions = {
controllers: [UsersController],
routePrefix: '/api'
routePrefix: '/api',
}
const app: Express = createExpressServer(routingControllersOptions)

// Parse class-validator classes into JSON Schema:
const schemas = validationMetadatasToSchemas({
refPointerPrefix: '#/components/schemas/'
classTransformerMetadataStorage: defaultMetadataStorage,
refPointerPrefix: '#/components/schemas/',
})

// Parse routing-controllers classes into OpenAPI spec:
Expand All @@ -29,18 +31,18 @@ const spec = routingControllersToSpec(storage, routingControllersOptions, {
securitySchemes: {
basicAuth: {
scheme: 'basic',
type: 'http'
}
}
type: 'http',
},
},
},
info: {
description: 'Generated with `routing-controllers-openapi`',
title: 'A sample API',
version: '1.0.0'
}
version: '1.0.0',
},
})

app.use('/docs', swaggerUiExpress.serve, swaggerUiExpress.setup(spec));
app.use('/docs', swaggerUiExpress.serve, swaggerUiExpress.setup(spec))

// Render spec on root:
app.get('/', (_req, res) => {
Expand All @@ -49,5 +51,5 @@ app.get('/', (_req, res) => {

app.listen(3001)
console.log(
'Express server is running on port 3001. Open http://localhost:3001/users/'
'Express server is running on port 3001. Open http://localhost:3001/docs/'
)
3 changes: 2 additions & 1 deletion sample/01-basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
"main": "app.js",
"scripts": {
"start": "npm run build && node ./build/app.js",
"start:watch": "build:watch && nodemon ./build/app.js",
"start:watch": "npm run build:watch && nodemon ./build/app.js",
"build": "node_modules/.bin/tsc",
"build:watch": "npm run build -- -w"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.3",
"class-transformer": "^0.3.1",
"class-validator": "^0.12.2",
"class-validator-jsonschema": "^2.0.0-rc1",
"express": "^4.17.1",
Expand Down
7 changes: 6 additions & 1 deletion sample/01-basic/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,11 @@ ci-info@^1.5.0:
resolved "https://registry.yarnpkg.com/ci-info/-/ci-info-1.6.0.tgz#2ca20dbb9ceb32d4524a683303313f0304b1e497"
integrity sha512-vsGdkwSCDpWmP80ncATX7iea5DWQemg1UgCW5J8tqjU3lYw4FBYuj89J0CTVomA7BEfvSZd84GmHko+MxFQU2A==

class-transformer@^0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/class-transformer/-/class-transformer-0.3.1.tgz#ee681a5439ff2230fc57f5056412d3befa70d597"
integrity sha512-cKFwohpJbuMovS8xVLmn8N2AUbAuc8pVo4zEfsUVo8qgECOogns1WVk/FkOZoxhOPTyTYFckuoH+13FO+MQ8GA==

class-utils@^0.3.5:
version "0.3.6"
resolved "https://registry.yarnpkg.com/class-utils/-/class-utils-0.3.6.tgz#f93369ae8b9a7ce02fd41faad0ca83033190c463"
Expand Down Expand Up @@ -1503,7 +1508,7 @@ ret@~0.1.10:
integrity sha512-TTlYpa+OL+vMMNG24xSlQGEJ3B/RzEfUlLct7b5G/ytav+wPrplCpVMFuwzXbkecJrb6IYo1iFb0S9v37754mg==

routing-controllers-openapi@../..:
version "2.0.0"
version "2.1.0"
dependencies:
lodash "^4.17.15"
openapi3-ts "^1.3.0"
Expand Down

0 comments on commit bea6f3d

Please sign in to comment.