MapsterMapper / MapsterMapper/Mapster

Does Mapster has a way to explicitly map the same object while false ShallowCopyForSameType?

Open
#938 19 comments 0 reactions 0 assignees View on GitHub

A pull request for this has already been merged.

  • #940 by @DocSvartz — closed without merging
  • #941 by @DocSvartz — merged
Dominant language
C#
Stars
5.2k
Forks
410
Avg merge
2d 12h
Merged PRs (30d)
6

Description

## Is this possible?
```cs
cfg.Default.ShallowCopyForSameType(true);

// Attempted code:
cfg.NewConfig().ShallowCopyForSameType(false);
```

## Trying to replicate similar behavior in the following:

> // Source - https://stackoverflow.com/a/31677062
// Posted by Jimmy Bogard
// Retrieved 2026-04-28, License - CC BY-SA 3.0
> You're reusing a type on both the source and destination objects, "MyStuff". When AutoMapper sees two assignable types, it assigns them rather than copying them. You can override this behavior by creating an explicit map:
```cs
Mapper.CreateMap();
```
> AutoMapper defaults to assigning, as AutoMapper is not a copying/cloning library.

## Concerns
- While I'm trying out attempted code, some passes and some fails the unit test wherein the failed attempted code always had an error `System.NullReferenceException : Object reference not set to an instance of an object.`.
- These are only occurring for objects that has their own parameterless constructor that instantiates their uninitialized object properties if it's still null.
```cs
public FailingMyStuff() { CreateEmptyEntities(); }
public void CreateEmptyEntities() { Item1 ??= new(); Item2 ??= new(); }
```
- Although it can be fixed using `MapWith` and defining a function, let's say `MapToSelf`, where it's general structure is `return src == null ? null : new MyStuff(){ ... };` and I've verified that this passed the failing unit tests.

### However, I wanted to know if there's a better way in automating this instead of using `MapToSelf`.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the issue examples and the merged pull request #941, then compare its changes with the reported same-type mapping and constructor-related null-reference cases. Done means confirming whether the merged work addresses explicit mapping when ShallowCopyForSameType is enabled, or documenting what remains unresolved.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.