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.

[MapperIgnore] on properties of classes

See original GitHub issue

A 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:open
  • Created 2 months ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mutzlcommented, Jul 21, 2023

Would #513 and #201 help?

#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!

0reactions
latonzcommented, Aug 2, 2023

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.

Read more comments on GitHub >

github_iconTop 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 >

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