entur / mapstruct-spi-protobuf

Protobuf accessor naming strategy for Mapstruct
European Union Public License 1.2
70 stars 19 forks source link

Enum mapping from protobuf class to protobuf class is wrong #136

Open seime opened 3 years ago

seime commented 3 years ago

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

See branch proto_to_proto_mapping

S1artie commented 2 months ago

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.