Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapping not found error when using nullSubstitution on array property #480

Closed
3 of 9 tasks
LennartH opened this issue Jun 13, 2022 · 0 comments
Closed
3 of 9 tasks
Labels
bug Something isn't working

Comments

@LennartH
Copy link
Contributor

Is there an existing issue for this?

  • I have searched the existing issues

Describe the issue

If you have to explicitly provide a properties type (e.g. an enum array) and you have to configure a nullSubstitution in the mapping, the following error is thrown when trying to map an object:

[AutoMapper] Mapping is not found for function String() { [native code] } and function String() { [native code] }
[AutoMapper] 
Error at "values" on Bar ({"name":"foo","id":"id"})
---------------------------------------------------------------------
Original error: Error: Mapping is not found for function String() { [native code] } and function String() { [native code] }
Error: 
Error at "values" on Bar ({"name":"foo","id":"id"})
---------------------------------------------------------------------
Original error: Error: Mapping is not found for function String() { [native code] } and function String() { [native code] }

The error only occurs if the property has a value or if the nullSubstitution resolves to a non-empty array and the specified type is built-in (e.g. String or Object). I think the problem is that nullSubstitution (and undefinedSubstitution and condition) call mapper.mapArray which calls getMapping without applying the 'identifier checks' used by map.

Models/DTOs/VMs

enum Enum {
  Value1 = 'Value1',
  Value2 = 'Value2',
}

class Foo {
  @AutoMap(() => [String])
  values: Enum[];
}

class Bar {
  @AutoMap(() => [String])
  values: Enum[];
}

Mapping configuration

export const mapper: Mapper = createMapper({
  strategyInitializer: classes(),
  namingConventions: new CamelCaseNamingConvention(),
});
createMap(mapper, Foo, Bar,
  forMember((bar) => bar.values, nullSubstitution([])),
);

Steps to reproduce

const foo = new Foo();
foo.values = [Enum.Value1];
const bar = mapper.map(foo, Foo, Bar);
console.log(bar);

Expected behavior

No error should occur, since built-in types should have built-in mappings.

Screenshots

No response

Minimum reproduction code

No response

Package

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

Other package and its version

No response

AutoMapper version

8.3.6

Additional context

No response

@LennartH LennartH added the bug Something isn't working label Jun 13, 2022
@nartc nartc closed this as completed in 262f86b Jul 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant