[MapperIgnore] on properties of classes
See original GitHub issueA common usecase is, that entities have properties that never need to be mapped to any DTO. Some of these metadata properties might even be defined in a base class. For example:
public abstract class BaseEntity
{
public DateTime CreatedDate { get; set; }
public string CreatedBy { get; set; } = default!;
public DateTime? UpdatedDate { get; set; }
public string? UpdatedBy { get; set; }
}
public class MyEntity : BaseEntity
{
public string Foo { get; set; }
}
public class MyDto
{
public string Foo { get; set; }
}
So the mapper configuration is always pretty verbose:
[MapperIgnoreTarget(nameof(BaseEntity.CreatedDate))]
[MapperIgnoreTarget(nameof(BaseEntity.CreatedBy))]
[MapperIgnoreTarget(nameof(BaseEntity.UpdatedDate))]
[MapperIgnoreTarget(nameof(BaseEntity.UpdatedBy))]
public static partial MyEntity ToEntity(this MyDto dto);
[MapperIgnoreSource(nameof(BaseEntity.CreatedDate))]
[MapperIgnoreSource(nameof(BaseEntity.CreatedBy))]
[MapperIgnoreSource(nameof(BaseEntity.UpdatedDate))]
[MapperIgnoreSource(nameof(BaseEntity.UpdatedBy))]
public static partial MyDto ToDto(this MyEntity entity);
A MapperIgnoreAttribute - similar to the well known [JsonIgnore]
- directly on the property would make code way cleaner.
public abstract class BaseEntity
{
[MapperIgnore] public DateTime CreatedDate { get; set; }
[MapperIgnore] public string CreatedBy { get; set; } = default!;
[MapperIgnore] public DateTime? UpdatedDate { get; set; }
[MapperIgnore] public string? UpdatedBy { get; set; }
}
Issue Analytics
- State:
- Created 2 months ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Automapper - Ignore property on base class
You'll need to specify mapping details (like ignoring certain properties) in the mapping of child classes, not parents. For example: Mapper.
Read more >Jackson Ignore Properties on Marshalling
The article discusses Jackson's central ObjectMapper class, basic serialization and deserialization as well as configuring the two processes.
Read more >Can you ignore properties globally with jackson? : r/javahelp
I'm trying to find a way to prevent jackson (@Ignore) from serializing any property named password on any object. I know of ObjectMapper....
Read more >Jackson Ignore Properties on Marshalling
At the class level, we can easily ignore the specific fields by using the @JsonIgnoreProperties and specifying the fields by name. Let's take...
Read more >Ignore Property With Jackson - Java By Examples
In this tutorial, we'll investigate how we can ignore some properties of a class during serialization and deserialization using Jackson.
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
#201 would help, I suppose.
The
[MapperIgnore]
looks way more intuitive to me, though. (at least for the use case I described above - for this kind of metadata, I know upfront, that it’s never gonna be mapped).BTW: Und vielen Dank für diese großartige Idee und Umsetzung von mapperly!
https://github.com/riok/mapperly/pull/611 removes the
MapperIgnoreAttribute
from the API, it may in a later release be reused to ignore properties/fields.