Skip to content

Commit 90b7e4f

Browse files
fix: update serialization docs to avoid ng compiler error
1 parent 337c68c commit 90b7e4f

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

content/techniques/serialization.md

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -60,37 +60,6 @@ When this endpoint is requested, the client receives the following response:
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-
9463
#### Expose properties
9564

9665
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.
@@ -129,6 +98,37 @@ findOne(): UserEntity {
12998
13099
Options passed via `@SerializeOptions()` are passed as the second argument of the underlying `instanceToPlain()` function. In this example, we are automatically excluding all properties that begin with the `_` prefix.
131100

101+
#### Transform plain objects
102+
103+
You can enforce transformations at the controller level by using the `@SerializeOptions` 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`.
104+
105+
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:
106+
107+
```typescript
108+
@UseInterceptors(ClassSerializerInterceptor)
109+
@SerializeOptions({ type: UserEntity })
110+
@Get()
111+
findOne(@Query() { id }: { id: number }): UserEntity {
112+
if (id === 1) {
113+
return {
114+
id: 1,
115+
firstName: 'John',
116+
lastName: 'Doe',
117+
password: 'password',
118+
};
119+
}
120+
121+
return {
122+
id: 2,
123+
firstName: 'Kamil',
124+
lastName: 'Mysliwiec',
125+
password: 'password2',
126+
};
127+
}
128+
```
129+
130+
> 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.
131+
132132
#### Example
133133

134134
A working example is available [here](https://github.com/nestjs/nest/tree/master/sample/21-serializer).

0 commit comments

Comments
 (0)