When it comes to using class-transformer and routing-controllers, I seem to be encountering some confusion. I tried to utilize the class-transformer library to convert objects received from a third-party server and adapt them to fit my own model with different names. Initially, this seemed to work fine as I was able to see the converted model when logging it or converting it to JSON. However, when attempting to output the model using routing-controllers, it still showed the original object.
User class:
class User {
@Expose({ name: 'uid' }) // To convert/rename the "uid" property received from the server
id: string
firstName: string
lastName: string
}
users-controller.ts:
@JsonController('/users')
export class UsersController {
@Get('/')
async get() {
const user = plainToClass(User, {
uid: '123',
firstName: 'Matthew',
lastName: 'Michalsky'
})
console.log(user)
return user
}
}
The console displays the expected value:
User {
id: '123',
firstName: 'Matthew',
lastName: 'Michalsky'
}
However, the response from UsersController (using routing-controllers lib) shows:
{
uid: '123',
firstName: 'Matthew',
lastName: 'Michalsky'
}
I'm wondering if there is something missing or incorrectly done on my end. Any insights would be greatly appreciated. Thanks!