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

Reversed order of key/value pairs in JSON after mapping #497

Closed
4 of 9 tasks
friedScotch opened this issue Jul 26, 2022 · 3 comments
Closed
4 of 9 tasks

Reversed order of key/value pairs in JSON after mapping #497

friedScotch opened this issue Jul 26, 2022 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@friedScotch
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the issue

Hello @nartc,

hats off for being so active in the first place.
I'm experiencing something similar to #181 . The Mapper is configured properly and works as well. Just the JSON key/value pairs after mapping are in reversed order.

Models/DTOs/VMs

UserEntity Class:

export class UserEntity{     

@AutoMap()
id: number;

@AutoMap()
name: string;

@AutoMap()
sharedInfo: string;

password: string;
}

UserResponse Class

export class UserResponse{    

@AutoMap()
id: number;

@AutoMap()
name: string;

@AutoMap()
sharedInfo: string;
}

Mapping configuration

This is the automapper profile:

@Injectable()
export class UserProfile extends AutomapperProfile {
  constructor(@InjectMapper() mapper: Mapper) {
    super(mapper);
  }

  get profile(): MappingProfile {
    return (mapper) => {
      createMap(mapper, UserEntity, UserResponse);
    };
  }
}
@Injectable()
export class UserService {
    users: UserEntity[] = users;

    constructor(@InjectMapper() private readonly mapper: AutoMapper) { }
    
    async getAllUsers(): Promise<UserResponse[]>{
        return toPromise(this.mapper.mapArray(this.users, UserEntity, UserResponse));
    }
    ...
}

Steps to reproduce

No response

Expected behavior

Expected:

{
"users": [
             {
               "id": 456,
               "name": "username",
               "sharedInfo": "This is shared info block"
             }
   ]
}

Actual response from mapper

{
"users": [
             {
               "sharedInfo": "This is shared info block",              
               "name": "username",
                "id": 456
             }
   ]
}

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

Additional context

NestJs v8.0.0
Node v16.16.0

@friedScotch friedScotch added the bug Something isn't working label Jul 26, 2022
@nartc
Copy link
Owner

nartc commented Jul 26, 2022

Thank you for reporting. I'll take a look as soon as I find some time.

@nartc nartc self-assigned this Jul 26, 2022
@nartc nartc closed this as completed in e5b7028 Jul 27, 2022
@nartc
Copy link
Owner

nartc commented Jul 27, 2022

Please give 8.7.0 a try

EDIT: I'll give you some context about the behavior if you're interested.

The "problem" was from: mappingProperties.unshift() in createInitialMapping() function. createInitialMapping() is the function that gets called when you call: createMap(mapper, Source, Destination).

Normally, the Mapper will grab the metadata from AutoMap and process those into MappingProperties. These are so-called "Auto Configuration".

Optionally, you can pass one or many forMember() calls to "customize" the mapping instruction for each property. These "Manual/Custom Configurations" are always run FIRST.

mappingProperties.unshift() makes sure that the "Auto Configurations" are placed BEFORE the "Manual/Custom Configurations" because the "Manual/Custom Configurations" should take precedence over the "Auto Configurations"

export class UserEntity {
   @AutoMap()
   id!: number;
}

export class UserResponse {
   @AutoMap()
   id!: number;
}

createMap(mapper, UserEntity, UserResponse); // "id" is auto-configured
createMap(mapper, UserEntity, UserResponse,
   forMember(d => d.id, fromValue(Math.random())) // "id" is custom-configured and this should take precedence
)

@friedScotch
Copy link
Author

@nartc Whoa thanks for the lightning fast fix AND detailed info. I throughly enjoyed reading it! 👍

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