Mapper is not working with Profiles
anrolmar opened this issue · 7 comments
Hi, I'm using the automapper in this way:
app.module.ts
@module({
imports: [
AutomapperModule.withMapper()
],
controllers: [],
providers: []
})
export class AppModule {}
queue.profile.ts
@Profile()
export class QueueProfile extends ProfileBase {
constructor(mapper: AutoMapper) {
super();
mapper.createMap(Queue, CreateMessageDto).reverseMap();
}
}
queue.controller.ts
@controller()
export class QueueController {
constructor(
@InjectMapper() private readonly mapper: AutoMapper
) {}
@post()
@httpcode(HttpStatus.CREATED)
createMessage(@Body() message: CreateMessageDto): void {
try {
const queue = this.mapper.map(message, Queue);
} catch (error) {
throw new InternalServerErrorException(error);
}
}
}
queue.ts
export class Queue {
filePath: string;
pageNumber: number;
containerName: string;
resultPath: string;
language: string;
}
create-message.dto.ts
export class CreateMessageDto {
@IsString()
readonly filePath: string;
@IsNumber()
readonly pageNumber?: number = 1;
@IsString()
readonly containerName: string;
@IsString()
readonly resultPath?: string;
@IsString()
readonly language?: string = 'fr-FR';
}
My problem is that when I call to the controller queue contains only a proto field
It seems that the mapping is not working quite well.
Thanks
Hi,
There are couple of things I’d like to verify:
- I guess there is a QueueModule where you have QueueController in its controllers array? If yes, have you imported “path/to/queue.profile”? This is for TypeScript to include QueueProfile in the bundle and actually execute the Profile code.
- CreateMessageDto as a Body. Are you sure this “message” is an instance of CreateMessageDto or just an Object? Regardless, please use: this.mapper.map(message, Queue, CreateMessageDto) to ensure AutoMapper knows the source model is CreateMessageDto and will instantiate CreateMessageDto for you.
- No, there's no
QueueModule
. And intoAppModule
, I've imported './mappers/queue.profile'; - Yes, I'm sure. I've used
this.mapper.map(message, Queue, CreateMessageDto)
and the results has been the same. Would it be possible thatQueue
class doesn't have any constructor?