mapstruct-spi-protobuf icon indicating copy to clipboard operation
mapstruct-spi-protobuf copied to clipboard

176 mappers should never return null

Open zartc opened this issue 3 years ago • 2 comments

Here's the code that implements the ban of null return values from mappers.

  • null return values from domain->proto mapper will end up with a NPE thrown by Protobuff.
  • null return values from proto->domain is less dangerous but it is best to let the application decide what to do in a post-mapper step.

The changes consist of systematically return a "default' value: empty string or arrays, EPOCH, zero, or protobuff's getDefaultInstance.

Close #176

zartc avatar Mar 20 '22 19:03 zartc

@zartc Sorry for the late response,

I agree with your reasoning. On the proto->domain side I would like to see 2 different implementations, yours (map to empty domain) as well as the existing one (map to null).

If we split the existing mapper class into 3 variants

  • DomainToProto
  • ProtoToEmptyDomain
  • ProtoToNullDomain

the developer can select the appropriate implementation based on his/her needs. It also avoid breaking (runtime) any existing use of this library (mapper uses attribute must be updated, but this is detected compile time)

seime avatar Sep 08 '22 12:09 seime

Any news?

wirekang avatar Apr 26 '23 09:04 wirekang