question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Private mapper do not work when the parent class implement an interface.

See original GitHub issue

Describe the bug

I have the basic setup:

internal sealed class UserWithClaims : IUser
{
   private readonly IdentityUser snapshot;

   public UserWithClaims(IdentityUser user, IReadOnlyList<Claim> claims, IReadOnlySet<string> roles)
   {
       // Clone the user so that we capture the previous values, even when the user is updated.
       snapshot = Mapper.IdentityUserToIdentityUser(user);
   }

   [Mapper(UseDeepCloning = true)]
   private static partial class Mapper
   {
       public static partial IdentityUser IdentityUserToIdentityUser(IdentityUser source);
   }
}

But the generated code does not add the namespace to the IUser interface, which is defined in another namespace. So the follow code is generated:

#nullable enable
namespace Notifo.Identity
{
    internal sealed class UserWithClaims : IUser
    {
        private static partial class Mapper
        {
            public static partial global::Microsoft.AspNetCore.Identity.IdentityUser IdentityUserToIdentityUser(global::Microsoft.AspNetCore.Identity.IdentityUser source)
            {
                var target = new global::Microsoft.AspNetCore.Identity.IdentityUser();
                target.Id = source.Id;
                ...
                return target;
            }
        }
    }
}

Expected behavior The implementing or base classes do not need to be copied to the generated code. if you want to copy that, add the namespaces.

Environment (please complete the following information):

  • Mapperly Version: 2.8.0
  • .NET Version: 7.0

Additional context Add any other context about the problem here.

Issue Analytics

  • State:closed
  • Created 2 months ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
TimothyMakkisoncommented, Aug 1, 2023

Is it valid to map records defined like this?

public record Person(int Id, string Name, string Address); public record PersonDTO(int Id, string Name);

Yes! Afaik Mapperly can map most types.

Example

[Mapper]
public static partial class Mapper
{
    // MapperIgnoreSource isn't required but because `Address` is not mapped to the target Mapperly will generate a warning: "Source member "Address" not mapped".
    // by ignoring this member the warning will not be created
    [MapperIgnoreSource(nameof(Person.Address))]
    public static partial PersonDTO Map(Person src);
}

Generated code

public static partial global::Riok.Mapperly.Sample.PersonDTO Map(global::Riok.Mapperly.Sample.Person src)
{
    var target = new global::Riok.Mapperly.Sample.PersonDTO(src.Id, src.Name);
    return target;
}
1reaction
TimothyMakkisoncommented, Jul 31, 2023

This bug has been fixed in 2.9.0-next.4

See #548 for details.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Compilation failure when mapper interface is a private ...
When a private nested interface is declared as a @Mapper, three compilation warnings are emitted in the generated class. It's not completely ...
Read more >
Mapper not generating correctly the Implementation class
I'm working in spring boot with Mappstruct and Lombok. When I use the Mapper anotation to auto implement the mapper class, the anotation...
Read more >
Should i implement same interface in derived class?
For most intents and purposes you don't need to implement an interface on the derived class. However there is a difference between the...
Read more >
Interfaces and Inheritance in Java
Multiple Inheritance is not supported by class because of ambiguity. In the case of interface, there is no ambiguity because the implementation ...
Read more >
Java Generics Example Tutorial - Generic Method, Class ...
A generic type is a class or interface that is parameterized over types. ... Now the problem with above implementation is that it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found