mapper icon indicating copy to clipboard operation
mapper copied to clipboard

property is undefined after mapping

Open maxime-aubry opened this issue 2 years ago • 3 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Describe the issue

After is called the mapper, i get a date as a string, as i wanted. But 'id' property is undefined. I must add 'forMember' function for 'id' property too.

Models/DTOs/VMs

import { AutoMap } from '@automapper/classes';

export class TransformationAuditGetAllResponseState {
  @AutoMap()
  id!: string;

  @AutoMap()
  transformationDate!: string;
}

export class TransformationAuditGetAllResponseDto {
  @AutoMap()
  id!: string;

  @AutoMap()
  transformationDate!: Date;
}

Mapping configuration

export const mapper: Mapper = createMapper({
  strategyInitializer: classes(),
});

createMap(
  mapper,
    TransformationAuditGetAllResponseDto,
    TransformationAuditGetAllResponseState,
  forMember(
      (dest: TransformationAuditGetAllResponseState) => dest.transformationDate,
    mapFrom((source: TransformationAuditGetAllResponseDto) => moment(source.transformationDate).format('DD/MM/YYYY hh:mm:ss A'))
  ),
);

Steps to reproduce

No response

Expected behavior

Value of 'id' property of TransformationAuditGetAllResponseDto should be mapped for 'id' property of TransformationAuditGetAllResponseState.

Screenshots

No response

Minimum reproduction code

No response

Package

  • [ ] I don't know.
  • [X] @automapper/core
  • [X] @automapper/classes
  • [ ] @automapper/nestjs
  • [ ] @automapper/pojos
  • [ ] @automapper/mikro
  • [ ] @automapper/sequelize
  • [ ] Other (see below)

Other package and its version

No response

AutoMapper version

8.8.1

Additional context

typescript version : 5.3.3

maxime-aubry avatar Feb 06 '24 15:02 maxime-aubry

Mapper works if i add type on properties.

for example :

import { AutoMap } from '@automapper/classes';

export class TransformationAuditGetAllResponseState {
  @AutoMap(() => String)
  id!: string;

  @AutoMap(() => String)
  transformationDate!: string;
}

export class TransformationAuditGetAllResponseDto {
  @AutoMap(() => String)
  id!: string;

  @AutoMap(() => Date)
  transformationDate!: Date;

Is there a way to do with another way ?

maxime-aubry avatar Feb 07 '24 09:02 maxime-aubry

Yes I'm getting the same result as you. However it only seems to occur in my testing environment (I use vitest). If I run it as normal transpiled javascript, it is fine and I don't need to specify the ()=>String

bradws avatar Feb 28 '24 06:02 bradws