mapstruct protobuf3 映射映射

Ant*_*nyC 1 protocol-buffers mapstruct

我正在尝试使用 mapstruct 将我的 DTO 映射到 protobuf 生成的类。此类包含地图,但这会导致异常:

java.lang.UnsupportedOperationException
    at java.util.Collections$UnmodifiableMap.putAll(Collections.java:1463)
Run Code Online (Sandbox Code Playgroud)

我不知道如何绕过这个。

这是我的映射器功能

@Mappings({
      @Mapping(target = "profiles", source = "profiles"),
  MyClassMessage.Builder convertToBuilder(MyClassDto myClassDto);
Run Code Online (Sandbox Code Playgroud)

它生成此代码

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

但我认为它应该是这样的

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

我应该如何告诉 mapstruct 这样做?

Ant*_*nyC 6

所以我发现的解决方案是类似

@Mapping(target = "mutableProfiles", source = "profiles"),
@Mapping(target = "profiles", ignore = true),
Run Code Online (Sandbox Code Playgroud)

第一行将使用可变映射并添加集合,第二行将告诉 mapstruct 不要尝试映射配置文件,因为它已经完成。