7

I'm trying to map some POJOs from Java to Protobuf (proto3). Some of them contain Lists. While mapping lists with POJOs (for example List) is no problem, I'm getting a UnsupportedOperationException.

Example with List<Product> (this works corrctly):

ProductProtobuf.Builder map(Product product);

@Mapping(target = "productsList", source = "products")
ResponseProtobuf.Builder map(Response response);

Example with List<String> (this doesn't work):

@Mapping(target = "usersList", source = "users")
ResponseProtobuf.Builder map(Response response);

Additionally, I have some Mapper for builder:

public ResponseProtobuf.Builder responseBuilder() {
    return ResponseProtobuf.newBuilder();
}

public ProductProtobuf build(ProductProtobuf.Builder builder) {
    return builder.build();
}
Liso
  • 195
  • 5
  • 15

1 Answers1

12

The problem is that MapStruct will use getProductsList().addAll(). In order to avoid this you should use CollectionMappingStrategy.ADDER_PREFERRED collectionMappingStrategy. Have a look at the UserMapper from the mapstruct-protobuf3 in the mapstruct examples repo.

In a nutshell you need to add:

@Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED,
    nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)

to your mapper.

One information regarding your builder, in case you are using 1.3.0.Beta1 you won't need it as there is out of the box support for that in MapStruct now.

NB: There was a bug in MapStruct that was not working correctly with ProtocolStringList. This bug has been fixed in 1.3.0.Beta1. If you try with this version it should work (in case your productList is a String)

Filip
  • 19,269
  • 7
  • 51
  • 60
  • 3
    I forgot to write that I already used: `@Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED, nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)` The problem occures ONLY for List because MapStruct would create an object of type ProtocolStringList (instead of String), which is an interface and therefore can't be used. – Liso Jul 23 '18 at 12:01
  • Can you update your issue with this information, please. I will update the answer with the fix – Filip Jul 23 '18 at 17:57
  • 1
    How about from Protobuf to Java POJO? – 0xTomato Apr 10 '21 at 07:05
  • @0xTomato for that, please see: https://stackoverflow.com/a/72244381/3667003 – Oliver May 14 '22 at 22:53