mapper icon indicating copy to clipboard operation
mapper copied to clipboard

When using pojos strategy, there's an internal error while creating an error message

Open eyalpost opened this issue 3 years ago • 0 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Describe the issue

When trying to convert from one type to another and the configuration is not complete, instead of throwing a meaningful error, the following error is thrown:

Cannot convert a Symbol value to a string
TypeError: Cannot convert a Symbol value to a string
    at assertUnmappedProperties (/Users/eyalp/sources/nsr-automapper/node_modules/@automapper/core/index.cjs:74:92)
    at map (/Users/eyalp/sources/nsr-automapper/node_modules/@automapper/core/index.cjs:494:3)
    at mapReturn (/Users/eyalp/sources/nsr-automapper/node_modules/@automapper/core/index.cjs:291:10)
    at Proxy.<anonymous> (/Users/eyalp/sources/nsr-automapper/node_modules/@automapper/core/index.cjs:691:31)

Models/DTOs/VMs

    interface User {
      firstName: string;
      lastName: string;
    }

    interface UserDto {
      firstName: string;
      lastName: string;
      fullName: string;
    }

Mapping configuration

    function createUserMetadata() {
      PojosMetadataMap.create<User>("User", {
        firstName: String,
        lastName: String,
      });

      PojosMetadataMap.create<UserDto>("UserDto", {
        firstName: String,
        lastName: String,
        fullName: String,
      });
    }

    createUserMetadata();

    const mapper = createMapper({
      strategyInitializer: pojos(),
      errorHandler: {
        handle(error: unknown) {
          throw error;
        },
      },
    });

    createMap<User, UserDto>(
      mapper,
      "User", 
      "UserDto",
    );

Steps to reproduce

The following code can help reproduce the problem. Note this is copied from your examples, I intentionally removed the mapping for fullName and used a custom error handler hoping I will get the following message:

Unmapped properties for User -> UserDto:
-------------------
fullName
    interface User {
      firstName: string;
      lastName: string;
    }

    interface UserDto {
      firstName: string;
      lastName: string;
      fullName: string;
    }

    function createUserMetadata() {
      PojosMetadataMap.create<User>("User", {
        firstName: String,
        lastName: String,
      });

      PojosMetadataMap.create<UserDto>("UserDto", {
        firstName: String,
        lastName: String,
        fullName: String,
      });
    }

    createUserMetadata();

    const mapper = createMapper({
      strategyInitializer: pojos(),
      errorHandler: {
        handle(error: unknown) {
          throw error;
        },
      },
    });

    createMap<User, UserDto>(
      mapper,
      "User", // this needs to match what we passed in PojosMetadataMap.create()
      "UserDto", // this needs to match what we passed in PojosMetadataMap.create()
    );

    const dto = mapper.map<User, UserDto>(
      { firstName: "Chau", lastName: "Tran" },
      "User", // this needs to match what we passed in PojosMetadataMap.create()
      "UserDto" // this needs to match what we passed in PojosMetadataMap.create()
    );

The root cause seems to lie here: https://github.com/nartc/mapper/blob/85060cd8143eddfd8bf08ea8c1ae67fbd7108344/packages/core/src/lib/utils/assert-unmapped-properties.ts#L45 Basically it tries to do

//convert a symbol to string
`${identifier}`

where identifier is a Symbol and this fails with TypeError: Cannot convert a Symbol value to a string

Expected behavior

A proper error message should be passed to my error handler. Note that by default if an error handler is not configured mapping errors are "swallowed" but in this case, the error is thrown before it gets to the error handler

Screenshots

No response

Minimum reproduction code

No response

Package

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

Other package and its version

No response

AutoMapper version

8.7.5

Additional context

No response

eyalpost avatar Aug 07 '22 10:08 eyalpost