简体   繁体   English

mapstruct 使用了错误的重载设置器

[英]mapstruct uses the wrong overload setter

I am using mapstruct 1.5.2.我正在使用 mapstruct 1.5.2。 I try to map a SaveAccountDTO object into a AccountUpdateParams.Company object from the Stripe Java library.我尝试将 map 一个 SaveAccountDTO object 放入一个 AccountUpdateParams.Company object 来自 Stripe ZD52387880E15EA2123 库。 It uses the builder pattern.它使用构建器模式。

The generated method uses the wrong overloaded method: the builder have a setter with String type and a second setter with the same name and a EmptyParam enum type:生成的方法使用了错误的重载方法:构建器有一个 String 类型的 setter 和一个具有相同名称和 EmptyParam 枚举类型的第二个 setter:

public Builder setName(String name) {
    this.name = name;
    return this;
}

public Builder setName(EmptyParam name) {
    this.name = name;
    return this;
}

Mapstruct generates the following implementation: Mapstruct 生成以下实现:

AccountUpdateParams.Company.Builder company = com.stripe.param.AccountUpdateParams.Company.builder();

if ( value.getLegalName() != null ) {
    company.setName( Enum.valueOf( EmptyParam.class, value.getLegalName() ) );
}

But it is wrong as I would like to use the setName(String) setter:但这是错误的,因为我想使用 setName(String) 设置器:

company.setName( value.getLegalName() );

My mapping for this field is quite simple:我对该字段的映射非常简单:

@Mapping(target = "name", source = "legalName")

Why does mapstruct choose to convert the input String into the enum to match the second setter?为什么mapstruct会选择将输入的String转换成枚举来匹配第二个setter? Is there any way to go for the String setter?字符串设置器有什么方法可以使用 go 吗? I found no way to disable the implicit type conversion from the doc.我发现无法从文档中禁用隐式类型转换。

I don't know if this is the best solution, but since you ask for any way to solve the issue, you can ignore the property, and map it with a method annotated with @AfterMapping:我不知道这是否是最好的解决方案,但是由于您要求任何解决问题的方法,您可以忽略该属性,并使用带有@AfterMapping 注释的方法 map :

@Mapping(target = "name", ignore = true)
AccountUpdateParams.Company toCompany(SaveAccountDTO saveAccountDTO)

@AfterMapping
default void setNameFromLegalName(SaveAccountDTO saveAccountDTO, @MappingTarget AccountUpdateParams.Company company) {        
    company.setName( saveAccountDTO.getLegalName() );                  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM