mapper icon indicating copy to clipboard operation
mapper copied to clipboard

Incorrect mappings when passing invalid types

Open ClassTerr opened this issue 2 years ago • 0 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Describe the issue

When using Automapper I expect that output of map method will transform input into exact type with only known fields with correct types. But it isn't the case now. When class expects one type, but provided another mapping behaves very odd (examples attached).

Models/DTOs/VMs

class NestedClass {
  @AutoMap()
  myProp: string;
}

class Test {
  @AutoMap()
  title: string;

  @AutoMap()
  obj?: NestedClass;

  @AutoMap(() => [NestedClass])
  array?: NestedClass[];
}

Mapping configuration

// Create and export the mapper
export const mapper = createMapper({
  strategyInitializer: classes(),
});
createMap(mapper, Test, Test);
createMap(mapper, NestedClass, NestedClass);

Steps to reproduce

No response

Expected behavior

It's complicated question since for different scenarios I would like to adjust rules for incorrect types, but in general I think the most appropriate solution would be just skip incorrect values or consider them as undefined. Things become more complicated when working with arrays. If some element of array is not valid but others are - I would certainly like to configure the behavior - skip or set as null or undefined. typeConverter is not appropriate here as I understand since you need to predict all of the cases for every single mapping.

Screenshots

No response

Minimum reproduction code

https://stackblitz.com/edit/typescript-1cd3bs?file=index.ts

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.7.7

Additional context

No response

ClassTerr avatar Mar 28 '23 22:03 ClassTerr