nestjsx/automapper

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

nartc commented

Hi,

There are couple of things I’d like to verify:

  1. 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.
  2. 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.
  1. No, there's no QueueModule. And into AppModule, I've imported './mappers/queue.profile';
  2. Yes, I'm sure. I've used this.mapper.map(message, Queue, CreateMessageDto) and the results has been the same. Would it be possible that Queue class doesn't have any constructor?
nartc commented

@anrolmar Can you provide a sample repro? That’d be super helpful.

nartc commented

@anrolmar I just looked over your repo. You’re not using the plugin (automapper-transform-plugin) and you don’t annotate the properties with @AutoMap. Can you confirm that you have the @AutoMap decorators on the properties and it still doesn’t work?

Hi, @nartc. I've add the plugin and the @AutoMap, and it woks now.

Thanks.

Only a suggestion for you. Maybe you should add this part to the documentation of the NestJS library

nartc commented

@anrolmar Thanks for the suggestion. I’ll add it to clear up the confusion. Also, if you use the Plugin, you don’t need @AutoMap, although the plugin might work funky (TypeScript Transform is still an experimental feature of TypeScript).