Compilation fails when defining mapper as inner/nested class and the outer class has an attribute
See original GitHub issueDescribe 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:
- 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)
- 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:
- Create a class with an attribute
- Declare a mapper as an inner class
- 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:
- Added support to inner classes: https://github.com/riok/mapperly/issues/350
- Lack of namespace import: https://github.com/riok/mapperly/issues/507
Issue Analytics
- State:
- Created 2 months ago
- Reactions:1
- Comments:7
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Created new issue for the parent class issue: https://github.com/riok/mapperly/issues/548
If its not too much effort then please do. Do you have a simple example you could share?