1
votes

I am trying to use mapstruct to map my DTO to protobuf generated class. This class contains a map but this cause an Exception:

java.lang.UnsupportedOperationException
    at java.util.Collections$UnmodifiableMap.putAll(Collections.java:1463)

I am not sure how to bypass this.

this is my mapper function

@Mappings({
      @Mapping(target = "profiles", source = "profiles"),
  MyClassMessage.Builder convertToBuilder(MyClassDto myClassDto);

it generate this code

if ( builder.getProfiles() != null ) {
  Map<String, Object> map = stringInterpreterProfileMapToStringInterpreterProfileMessageMap( interpreterSettingDto.getProfiles() );
  if ( map != null ) {
    builder.getProfiles().putAll( map ); // <-- this cause the exception
  }
 }

But i think it should be something like this instread

if ( builder.getProfiles() != null ) {
            Map<String, Object> map = stringInterpreterProfileMapToStringInterpreterProfileMessageMap( interpreterSettingDto.getProfiles() );
            if ( map != null ) {
                builder.putAllProfiles( map );
            }
        }

How should i tell mapstruct to do that way?

3

3 Answers

6
votes

So the solution i found was to something like

@Mapping(target = "mutableProfiles", source = "profiles"),
@Mapping(target = "profiles", ignore = true),

The first line will use the mutable map and add the collection and the 2nd will tell mapstruct to not try to map profile since its already done.

1
votes

This is currently not supported within MapStruct. However, there is an open feature request for it see issue #1343

0
votes

you can do like:

@Mappings({
      @Mapping(target = "putAllProfiles", source = "profiles"),
  MyClassMessage.Builder convertToBuilder(MyClassDto myClassDto);