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

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

Closed
3 of 9 tasks
eyalpost opened this issue Aug 7, 2022 · 0 comments
Closed
3 of 9 tasks
Labels
bug Something isn't working

Comments

@eyalpost
Copy link

eyalpost commented Aug 7, 2022

Is there an existing issue for this?

  • 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:


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.
  • @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.5

Additional context

No response

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

2 participants