Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map List<String> with Mapstruct from Java POJO to Protobuf (proto3)

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();
}
like image 556
Liso Avatar asked Jul 19 '18 07:07

Liso


1 Answers

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)

like image 58
Filip Avatar answered Oct 19 '22 15:10

Filip