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

extraArgs not inherited by others mapping #509

Closed
2 of 9 tasks
adumat opened this issue Sep 29, 2022 · 1 comment
Closed
2 of 9 tasks

extraArgs not inherited by others mapping #509

adumat opened this issue Sep 29, 2022 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@adumat
Copy link

adumat commented Sep 29, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Describe the issue

I have some params inside extraArgs that can be used during map by other mapping rule.
Only the parent map is able to receive the extraArgs, the child of this map will not receive the params if someone use the mapWithArguments function.

Models/DTOs/VMs

Models

export class SessionWorkoutRun {
  @AutoMap(() => [SessionWorkoutExerciseRun])
  workoutExercisesRun: SessionWorkoutExerciseRun[];
}

export class SessionWorkoutExerciseRun {
  @AutoMap(() => SessionExerciseRun)
  exerciseRun: SessionExerciseRun;
}

export class SessionExerciseRun {}

DTO

export class SummaryWorkoutDataDto {
  @AutoMap(() => [SummaryWorkoutExerciseDataDto])
  exercises: SummaryWorkoutExerciseDataDto[];
}

export class SummaryWorkoutExerciseDataDto {
  @AutoMap(() => SummaryExerciseDataDto)
  exerciseRun: SummaryExerciseDataDto;

  perc_vs_best: number | null;
  perc_vs_last: number | null;
}

export class SummaryExerciseDataDto {}

Mapping configuration

profile:

createMap(
  mapper,
  SessionWorkoutExerciseRun,
  SummaryWorkoutExerciseDataDto,
  forMember(
    d => d.exerciseRun,
    mapWith(
      SummaryExerciseDataDto,
      SessionExerciseRun,
      s => s.exerciseRun,
    ),
  ),
  forMember(
    d => d.perc_vs_best,
    mapWithArguments(
      (
        s,
        { perc_vs_best },
      ) => {
        return perc_vs_best;
      },
    ),
  ),
  forMember(
    d => d.perc_vs_last,
    mapWithArguments((s, { perc_vs_last }) => {
      return perc_vs_last;
    }),
  ),
);
createMap(
  mapper,
  SessionWorkoutRun,
  SummaryWorkoutDataDto,
  forMember(
    d => d.exercises,
    mapWith(
      SummaryWorkoutExerciseDataDto,
      SessionWorkoutExerciseRun,
      s => s.workoutExercisesRun,
    ),
  )
);

Steps to reproduce

with the above models, dto and mapping configuration, the call of the following function can reproduce the issue:

this.mapper.map(
  {
    workoutExercisesRun: [{
      exerciseRun: {}
    }, {
      exerciseRun: {}
    }],
  },
  SessionWorkoutRun,
  SummaryWorkoutDataDto,
  {
    extraArgs: () => ({
      perc_vs_last: 123,
      perc_vs_best: 321,
    }),
  },
);

Expected behavior

extraArgs is inherited by all child that use the function mapWithArguments.
with the steps above the expected result of map is:

{
  exercises: [
    {
      perc_vs_last: 123,
      perc_vs_best: 321,
      exerciseRun: {}
    }, {
      perc_vs_last: 123,
      perc_vs_best: 321,
      exerciseRun: {}
    }
  ]
}

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

node v16
nestjs 9

@adumat adumat added the bug Something isn't working label Sep 29, 2022
@adumat
Copy link
Author

adumat commented Sep 29, 2022

a workaround is to define the mapping of the child that needs to inherit the extraArgs param like this:

createMap(
  mapper,
  SessionWorkoutRun,
  SummaryWorkoutDataDto,
  forMember(
    d => d.exercises,
    mapWithArguments((s, extraArgs) =>
      mapper.mapArray(
        s.workoutExercisesRun,
        SessionWorkoutExerciseRun,
        SummaryWorkoutExerciseDataDto,
        { extraArgs: () => extraArgs },
      ),
    ),
  )
);

@nartc nartc self-assigned this Oct 24, 2022
@nartc nartc closed this as completed in 65c6a74 Oct 25, 2022
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