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

Enum mapping from protobuf class to protobuf class is wrong

Open seime opened this issue 4 years ago • 1 comments

Default 0 constant (_UNSPECIFIED) maps to UNRECOGNIZED in generated code.

See branch proto_to_proto_mapping

seime avatar Nov 08 '21 15:11 seime

This is AFAIK technically un-fixable, because the SPI interface does not communicate the mapping target type, so one cannot distinguish between mapping a Protobuf enum to a Java enum (in which case there is no UNRECOGNIZED, so mapping UNSPECIFIED and UNRECOGNIZED both to null is the correct thing to do) and mapping to another Protobuf enum, where there is an UNRECOGNIZED type on both sides and thus mapping UNRECOGNIZED to null (which is what causes UNSPECIFIED to be mapped to UNRECOGNIZED) is counterproductive.

I have implemented some kind of work-around in another mapstruct-protobuf SPI implementation I'm maintaining over here: https://github.com/S1artie/mapstruct-spi-protobuf - this solution uses a new feature of Mapstruct 1.6, custom configuration values, to allow the user to control whether UNRECOGNIZED is mapped to null. By default it's done, but when generating protobuf-to-protobuf mapping code, it can be deactivated (because in that case you can rely on there being the UNRECOGNIZED magic value to be present in the target enum) and thus the mapping of _UNSPECIFIED is fixed.

S1artie avatar Aug 19 '24 11:08 S1artie