MapsterMapper / MapsterMapper/Mapster
Unable to map classes with generics
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 5.2k
- Forks
- 410
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 6
Description
I would want to map a class structure for logs. The main class is a generic class `Audit` where `T ` is class `LogData `or a subclass it.
DTO would have a simple structure with `AuditDTO `and property of type `LogDataDTO`, that object has all the possible subclass properties united in it.
- My initial issues occurred at compile time as `CompileException` .
If my configuration has
`config.NewConfig()` <-- with all base class properties mapped plus
` .IgnoreNonMapped(true)`
` .Include()` <-- for all subclass of LogData
and
`config.NewConfig()` <-- with all the specific subclass properties mapped plus
`.IgnoreNonMapped(true)`
then it would complain about LogDataDTO not having a corresponding source member mapped or ignored: SOME properties of LogDataSubclasses' - but weird enough not all of them. If I added to the LogData baseclass config explicitly the `.Ignore(dest => dest.ProblematicSubclassProp)` then that would solve the issue.
- Second issue occurred afterward at runtime. The `AuditDTO ` had the same exception that its properties, other than the LogData, are not mapped or ignored. Adding an explicit config for mapping seems trickier as it does not seem to work with invariance:
if there's `config.NewConfig(), AuditDTO>()`, or any other type of `, AuditDTO>` then the source type causing the error is referred to as `>`. (Conventional interfaces)
I have the impression that i heavily overcomplicate the config and not using (misusing) some methods here... IgnoreNonMapped is definetely not working as expected and makes this whole declarartion redundant.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No files, tests, or entry points are named. Start by reproducing the reported compile-time and runtime failures with the Audit, LogData subclasses, DTOs, Include, and IgnoreNonMapped configurations described here; done means the expected behavior and a focused fix are established for both generic mapping cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100