Skip to content

Commit 337c68c

Browse files
Merge branch 'austinwoon-docs/add-serialize-options-transform-docs'
2 parents 3b1ddaa + 8c0a1d5 commit 337c68c

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

content/techniques/serialization.md

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ Now consider a controller with a method handler that returns an instance of this
3737
findOne(): UserEntity {
3838
return new UserEntity({
3939
id: 1,
40-
firstName: 'Kamil',
41-
lastName: 'Mysliwiec',
40+
firstName: 'John',
41+
lastName: 'Doe',
4242
password: 'password',
4343
});
4444
}
@@ -53,13 +53,44 @@ When this endpoint is requested, the client receives the following response:
5353
```json
5454
{
5555
"id": 1,
56-
"firstName": "Kamil",
57-
"lastName": "Mysliwiec"
56+
"firstName": "John",
57+
"lastName": "Doe"
5858
}
5959
```
6060

6161
Note that the interceptor can be applied application-wide (as covered [here](https://docs.nestjs.com/interceptors#binding-interceptors)). The combination of the interceptor and the entity class declaration ensures that **any** method that returns a `UserEntity` will be sure to remove the `password` property. This gives you a measure of centralized enforcement of this business rule.
6262

63+
#### Transform plain objects
64+
65+
You can enforce transformations at the controller level by using the `@SerializeOptions({ type: <CLASS> })` decorator. This ensures that all responses are transformed into instances of the specified class, applying any decorators from class-validator or class-transformer, even when plain objects are returned. This approach leads to cleaner code without the need to repeatedly instantiate the class or call `plainToInstance`.
66+
67+
In the example below, despite returning plain JavaScript objects in both conditional branches, they will be automatically converted into `UserEntity` instances, with the relevant decorators applied:
68+
69+
```typescript
70+
@UseInterceptors(ClassSerializerInterceptor)
71+
@SerializeOptions({ type: UserEntity })
72+
@Get()
73+
findOne(@Query() { id }: { id: number }): UserEntity {
74+
if (id === 1) {
75+
return {
76+
id: 1,
77+
firstName: 'John',
78+
lastName: 'Doe',
79+
password: 'password',
80+
};
81+
}
82+
83+
return {
84+
id: 2,
85+
firstName: 'Kamil',
86+
lastName: 'Mysliwiec',
87+
password: 'password2',
88+
};
89+
}
90+
```
91+
92+
> info **Hint** By specifying the expected return type for the controller, you can leverage TypeScript's type-checking capabilities to ensure that the returned plain object adheres to the shape of the DTO or entity. The `plainToInstance` function doesn't provide this level of type hinting, which can lead to potential bugs if the plain object doesn't match the expected DTO or entity structure.
93+
6394
#### Expose properties
6495

6596
You can use the `@Expose()` decorator to provide alias names for properties, or to execute a function to calculate a property value (analogous to **getter** functions), as shown below.

0 commit comments

Comments
 (0)