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.

Compilation fails when defining mapper as inner/nested class and the outer class has an attribute

See original GitHub issue

Describe the bug When defining the mapper as an inner class, the generated mapper code is copying the existing class attributes and the parent class but not copying their respective namespace defined at the bottom. This is creating 2 problems:

  1. The compilation fails because the attribute or the parent are not found (since it missing the using namespace). There is an open issue for this already (https://github.com/riok/mapperly/issues/507)
  2. By copying the attribute/tag to the generated class, it generates a compilation error CS0579 Duplicate 'xyz' attribute since now both the original and the generated class are applying the same attribute/tag.

To Reproduce Steps to reproduce the behavior:

  1. Create a class with an attribute
  2. Declare a mapper as an inner class
  3. Try to compile the code.

Expected behavior Code compiles without error

Code snippets

using Riok.Mapperly.Abstractions;
using System.Diagnostics.CodeAnalysis;
namespace Bug_Mapperly;

internal class Program { static void Main(string[] args) { Console.WriteLine("Hello, World!"); } }

[ExcludeFromCodeCoverage] // --The generated code will also have this attribute set and will show the 2 compilations errors mentioned above (Undefined ExcludeFromCodeCoverage because of lack of namespace import. If you use the fqn for ExcludeFromCodeCoverage as in the line below, you will get the duplicate attribute error
//[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public partial class CarDto
{
    public string? Maker { get; set; }
    public string? Model { get; set; }
    [Mapper]
    private partial class CarMapper
    {
        public partial CarDto From(Car car);
    }
}
public class Car
{
    public string? Maker { get; set; }
    public string? Model { get; set; }
}

Fix suggestion

  • Mapper should copy the using namespace code
  • Mapper should not copy outer class attributes when generating the code

Environment (please complete the following information):

  • Mapperly Version: 2.9.0-next.2
  • .NET Version: .NET 6
  • Target Framework: [e.g. .net6.0]
  • IDE: Visual Studio Pro 2022
  • OS: Windows 10

Additional context Related issues:

Issue Analytics

  • State:closed
  • Created 2 months ago
  • Reactions:1
  • Comments:7

github_iconTop GitHub Comments

1reaction
renatosccommented, Jul 8, 2023

Created new issue for the parent class issue: https://github.com/riok/mapperly/issues/548

1reaction
TimothyMakkisoncommented, Jul 7, 2023

@TimothyMakkison , I didn’t mention on the issue but I see same similar behavior with inheritance, where the parent class is being copied when it I think it should not needed to be copied. Do you want me to create a new issue about that?

If its not too much effort then please do. Do you have a simple example you could share?

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.
Read more >
Java compiler fails to recognise static inner class
An inner class need not necessarily be static. I think a static class does need to be nested inside another class, though. But...
Read more >
Nested Classes - Learning the Java Language
This beginner Java tutorial describes fundamentals of programming in the Java programming language.
Read more >
Inner Class in Java
Static Nested Class: It is a static class that is defined inside another class. It does not have access to the non-static members...
Read more >
Nested Classes in C# - GeeksforGeeks
A nested class can be declared as a private, public, protected, internal, protected internal, or private protected. Outer class is not allowed ...
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