0

In my mapping, I often have the requirement that 0 values (e.g. BigDecimal.ZERO) should be treated as NULL values. If it were JavaScript number values, then the requirement would be that all falsy values should be mapped to NULL.

Example:

BigDecimal => String

If NULL then map to NULL

If BigDecimal.signum() == 0 then map to NULL

All other values: .toString()

Is there a build-in configuration to achieve this, or do I need to implement a custom mapper?

MatterOfFact
  • 1,253
  • 2
  • 18
  • 49
  • I don't think there's a built-in configuration as such, but you can create a mapper that does it and reuse it, cfg CMyker's answer to [this question](https://stackoverflow.com/questions/38807415/mapstruct-how-can-i-inject-a-spring-dependency-in-the-generated-mapper-class) – daniu Feb 02 '23 at 06:51
  • There's a nullvaluemappingstrategy, but I don't think that's what you want. You could solve this, by using an @Named("bar") / @AfterMapping annotation on a custom method, paired with an @Mapping(target = "foo", qualifiedByName = "bar") or @Mapping(target = "foo", ignore = true) on the mapper declaration itself. – Z-100 Feb 02 '23 at 08:17
  • Thank you! Yes, I know that there's an option to write a custum mapper. My question is, if there is a build-in-config which already solves my requirement – MatterOfFact Feb 02 '23 at 09:45

0 Answers0