nartc / mapper

🔥 An Object-Object AutoMapper for TypeScript 🔥
https://automapperts.netlify.app/
MIT License
985 stars 87 forks source link

Properties with similar names map to undefined #527

Open nrgapple opened 1 year ago

nrgapple commented 1 year ago

Is there an existing issue for this?

Describe the issue

@AutoMap()
  inspection_order_status?: number;

  @AutoMap()
  inspection_order_status_id: number;
 @AutoMap()
  inspectionOrderStatus?: number;

  @AutoMap()
  inspectionOrderStatusId: number;

inspectionOrderStatusId will be undefined in these maps

namingConventions({
          source: new SnakeCaseNamingConvention(),
          destination: new CamelCaseNamingConvention(),
        })

Models/DTOs/VMs

No response

Mapping configuration

No response

Steps to reproduce

No response

Expected behavior

the values should map to their correct values

Screenshots

No response

Minimum reproduction code

No response

Package

Other package and its version

No response

AutoMapper version

8.7.7

Additional context

No response

nartc commented 1 year ago

Thanks for reporting. The naming convention algorithm isn't smart enough to determine whether

when there is inspection_order_status in the object.

The workaround is to use forMember() on inspection_order_status_id. And please feel free to send a PR to fix the algorithm on this (packages/core/src/lib/utils/get-path.ts)

vbrzezina commented 8 months ago

Hello, as this was a blocker on my current project I decided to dig into it a little and found out the issue is in the flattening algorithm, where the shared suffix is being searched in source object, however, and that is my solution to the problem, it's not checked whether it's actually object and thus needs to be flattened. In the reference source object, primitive values are all undefined, but when there's nested objects, they're expanded as objects with their own properties. So this fix verifies, that the property to be flattened is an object.

I'm honestly wasn't sure if I should add it only at the two places I did or to just add it to the hasProperty function. I also didn't find any existing spec for getFlatteningPaths and as I'm not familiar with it in depth and don't know all the requirements, I'm not sure I'm up to writing all the other test cases..