Skip to content

Commit 0cc84b5

Browse files
author
Aleksi Pekkala
committed
Annotate API request and response bodies
1 parent b078964 commit 0cc84b5

File tree

2 files changed

+92
-4
lines changed

2 files changed

+92
-4
lines changed

src/api/controllers/PetController.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
1+
import { IsNotEmpty, IsNumber, IsUUID, ValidateNested } from 'class-validator';
12
import {
23
Authorized, Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put
34
} from 'routing-controllers';
5+
import { ResponseSchema } from 'routing-controllers-openapi';
46

57
import { PetNotFoundError } from '../errors/PetNotFoundError';
68
import { Pet } from '../models/Pet';
79
import { PetService } from '../services/PetService';
10+
import { UserResponse } from './UserController';
11+
12+
class BasePet {
13+
@IsNotEmpty()
14+
public name: string;
15+
16+
@IsNumber()
17+
public age: number;
18+
}
19+
20+
export class PetResponse extends BasePet {
21+
@IsUUID()
22+
public id: string;
23+
24+
@ValidateNested()
25+
public user: UserResponse;
26+
}
27+
28+
class CreatePetBody extends BasePet {
29+
@IsUUID()
30+
public userId: string;
31+
}
832

933
@Authorized()
1034
@JsonController('/pets')
@@ -15,23 +39,36 @@ export class PetController {
1539
) { }
1640

1741
@Get()
42+
@ResponseSchema(PetResponse, { isArray: true })
1843
public find(): Promise<Pet[]> {
1944
return this.petService.find();
2045
}
2146

2247
@Get('/:id')
2348
@OnUndefined(PetNotFoundError)
49+
@ResponseSchema(PetResponse)
2450
public one(@Param('id') id: string): Promise<Pet | undefined> {
2551
return this.petService.findOne(id);
2652
}
2753

2854
@Post()
29-
public create(@Body() pet: Pet): Promise<Pet> {
55+
@ResponseSchema(PetResponse)
56+
public create(@Body({ required: true }) body: CreatePetBody): Promise<Pet> {
57+
const pet = new Pet();
58+
pet.age = body.age;
59+
pet.name = body.name;
60+
pet.userId = body.userId;
61+
3062
return this.petService.create(pet);
3163
}
3264

3365
@Put('/:id')
34-
public update(@Param('id') id: string, @Body() pet: Pet): Promise<Pet> {
66+
@ResponseSchema(PetResponse)
67+
public update(@Param('id') id: string, @Body() body: BasePet): Promise<Pet> {
68+
const pet = new Pet();
69+
pet.age = body.age;
70+
pet.name = body.name;
71+
3572
return this.petService.update(id, pet);
3673
}
3774

src/api/controllers/UserController.ts

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,43 @@
1+
import { Type } from 'class-transformer';
2+
import { IsEmail, IsNotEmpty, IsUUID, ValidateNested } from 'class-validator';
13
import {
24
Authorized, Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, Req
35
} from 'routing-controllers';
6+
import { ResponseSchema } from 'routing-controllers-openapi';
47

58
import { UserNotFoundError } from '../errors/UserNotFoundError';
69
import { User } from '../models/User';
710
import { UserService } from '../services/UserService';
11+
import { PetResponse } from './PetController';
12+
13+
class BaseUser {
14+
@IsNotEmpty()
15+
public firstName: string;
16+
17+
@IsNotEmpty()
18+
public lastName: string;
19+
20+
@IsEmail()
21+
@IsNotEmpty()
22+
public email: string;
23+
24+
@IsNotEmpty()
25+
public username: string;
26+
}
27+
28+
export class UserResponse extends BaseUser {
29+
@IsUUID()
30+
public id: string;
31+
32+
@ValidateNested({ each: true })
33+
@Type(() => PetResponse)
34+
public pets: PetResponse[];
35+
}
36+
37+
class CreateUserBody extends BaseUser {
38+
@IsNotEmpty()
39+
public password: string;
40+
}
841

942
@Authorized()
1043
@JsonController('/users')
@@ -15,28 +48,46 @@ export class UserController {
1548
) { }
1649

1750
@Get()
51+
@ResponseSchema(UserResponse, { isArray: true })
1852
public find(): Promise<User[]> {
1953
return this.userService.find();
2054
}
2155

2256
@Get('/me')
57+
@ResponseSchema(UserResponse, { isArray: true })
2358
public findMe(@Req() req: any): Promise<User[]> {
2459
return req.user;
2560
}
2661

2762
@Get('/:id')
2863
@OnUndefined(UserNotFoundError)
64+
@ResponseSchema(UserResponse)
2965
public one(@Param('id') id: string): Promise<User | undefined> {
3066
return this.userService.findOne(id);
3167
}
3268

3369
@Post()
34-
public create(@Body() user: User): Promise<User> {
70+
@ResponseSchema(UserResponse)
71+
public create(@Body() body: CreateUserBody): Promise<User> {
72+
const user = new User();
73+
user.email = body.email;
74+
user.firstName = body.firstName;
75+
user.lastName = body.lastName;
76+
user.password = body.password;
77+
user.username = body.username;
78+
3579
return this.userService.create(user);
3680
}
3781

3882
@Put('/:id')
39-
public update(@Param('id') id: string, @Body() user: User): Promise<User> {
83+
@ResponseSchema(UserResponse)
84+
public update(@Param('id') id: string, @Body() body: BaseUser): Promise<User> {
85+
const user = new User();
86+
user.email = body.email;
87+
user.firstName = body.firstName;
88+
user.lastName = body.lastName;
89+
user.username = body.username;
90+
4091
return this.userService.update(id, user);
4192
}
4293

0 commit comments

Comments
 (0)