Skip to content

Commit 8c0a1d5

Browse files
chore: improve wording
1 parent 544ac5f commit 8c0a1d5

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

content/techniques/serialization.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ 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
}
4545
```
4646

47-
> **Warning** Note that we must return an instance of the class. If you return a plain JavaScript object, for example, `{{ '{' }} user: new UserEntity() {{ '}' }}`, the object won't be properly serialized.
47+
> **Warning** Note that we must return an instance of the class. If you return a plain JavaScript object, for example, `{{ '{' }} user: new UserEntity() {{ '}' }}`, the object won't be properly serialized.
4848
4949
> info **Hint** The `ClassSerializerInterceptor` is imported from `@nestjs/common`.
5050
@@ -53,44 +53,43 @@ 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
6364

64-
#### Using the SerializeOptions to transform plain objects to the class instance.
65-
66-
You could enforce transformations at the controller level by stating `@SerializeOptions({{ '{' }} type {{ ':' }} <CLASS> {{ '}' }})` to transform all responses to the instance. Doing so will ensure that decorators on the class will always be applied, even if plain objects are returned. This allows for terser code, without the added verbosity of instantiating the class or calling `plainToInstance` repeatedly.
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`.
6766

68-
In this example, even though a plain js object was returned in both conditional statements, they will all be converted into `UserEntity` with annotated class-validator or class-transformer decorators applied.
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:
6968

7069
```typescript
7170
@UseInterceptors(ClassSerializerInterceptor)
71+
@SerializeOptions({ type: UserEntity })
7272
@Get()
73-
@SerializeOptions({ type : UserEntity })
7473
findOne(@Query() { id }: { id: number }): UserEntity {
7574
if (id === 1) {
7675
return {
7776
id: 1,
78-
firstName: 'Kamil',
79-
lastName: 'Mysliwiec',
77+
firstName: 'John',
78+
lastName: 'Doe',
8079
password: 'password',
8180
};
8281
}
8382

8483
return {
8584
id: 2,
86-
firstName: 'Kamil2',
87-
lastName: 'Mysliwiec2',
85+
firstName: 'Kamil',
86+
lastName: 'Mysliwiec',
8887
password: 'password2',
8988
};
9089
}
9190
```
9291

93-
> info **Hint** By stating the expecting controller return type, we can take advantage of Typescript to check if the returned plain object conforms to the DTO/entity shape. `plainToInstance`' does not hint if the plain object conforms to the the DTO/entity class instance you are transforming it into (somewhat equivalent to typecasting). This could lead to bugs in your application.
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.
9493
9594
#### Expose properties
9695

0 commit comments

Comments
 (0)