Skip to content

Compilation fails if property name is start (substring) of another property #525

@MasterPG5

Description

@MasterPG5

Is there an existing issue for this?

  • I have searched the existing issues

Describe the issue

I faced a very strange bug (I think). I have two classes, OrderDto and Order. These classes have 2 properties (among others), of which one is a starting substring of the other: shippingFee and shippingFeePerPallet, both nullable numbers.
I have a mapping profile to map from the former to the latter. First, I didn't specify these in the mapping profile explicitely, but when I tried to build the project, it failed with the following error (at the addProfile function):

.../server/node_modules/@automapper/core/index.cjs:1092
  return Object.prototype.hasOwnProperty.call(obj, property);
                                         ^
[2022.12.19 10:44:47] [error] -  TypeError: Cannot convert undefined or null to object

[...]

at addProfile (.../server/node_modules/@automapper/core/index.cjs:1373:11)

I narrowed down the problem to the shippingFee property. I tried adding both properties to the profile explicitely, as follows:

createMap(
    mapper,
    OrderDto,
    Order,
    forMember(
      (dest) => dest.shippingFee,
      mapFrom((source) => source.shippingFee)
    ),
    forMember(
      (dest) => dest.shippingFeePerPallet,
      mapFrom((source) => source.shippingFeePerPallet)
    )
  );

But it still doesn't work. The way I got it working is that I deleted shippingFee from both the profile and the OrderDto class (not a good solution, but it confirms the source of the issue). Another workaround was that I renamed shippingFee to shipiingFee (yes, with a typo) in both classes, in which case it magically worked.

The strange thing is, that other properties also have this kind of same-starting-substring, but they work without errors (like status and statusEn) - even the workaround has the same substring: shipiingFee and shippingFeePerPallet also start with "ship".

Models/DTOs/VMs

export class OrderDto {
...
  @AutoMap() @Expose() shippingFee?: number;
  @AutoMap() @Expose() shippingFeePerPallet?: number;
...
}

export class Order {
...
  @AutoMap() @Expose() shippingFee?: number;
  @AutoMap() @Expose() shippingFeePerPallet?: number;
...
}

Mapping configuration

createMap(
    mapper,
    OrderDto,
    Order,
    forMember(
      (dest) => dest.shippingFee,
      mapFrom((source) => source.shippingFee)
    ),
    forMember(
      (dest) => dest.shippingFeePerPallet,
      mapFrom((source) => source.shippingFeePerPallet)
    )
  );

Steps to reproduce

  1. Implement the following classes and mapping
  2. Add the profile
  3. Try to build

Expected behavior

Should map the properties to their counterparts as expected.

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

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions