MapStruct 属性在元数据中没有目标名称的写入访问器

问题描述 投票:0回答:1

我有一个

FlatDTO
需要映射到包含
Response
InfoData
 的嵌套 
MetaData

响应代码由

OpenAPI
生成。所以下面的定义不能改变。

package org.mapstruct.example.kotlin.openapi

import com.fasterxml.jackson.annotation.JsonProperty
import javax.validation.Valid

data class Response(

    @field:Valid @field:JsonProperty("infoData", required = true) val infoData: InfoData,

    @field:Valid @field:JsonProperty("metaData", required = true) val metaData: MetaData
)


data class InfoData(

    @field:JsonProperty("id", required = true) val id: kotlin.String,

    )


data class MetaData(
    @field:JsonProperty("firstProperty") val firstProperty: String? = null,
)

我已将

FlatDTO
定义如下。

package org.mapstruct.example.kotlin.models

data class FlatDTO(
    var id: String? = null,
    var firstProperty: String,
)

这是我的映射器类,它将

FlatDTO
映射到
Response

package org.mapstruct.example.kotlin.mapper

import org.mapstruct.Mapper
import org.mapstruct.Mapping
import org.mapstruct.Mappings
import org.mapstruct.example.kotlin.models.FlatDTO
import org.mapstruct.example.kotlin.openapi.Response

@Mapper
interface DataMapper {

    @Mappings(
        Mapping(target = "infoData.id", source = "id"),
        Mapping(target = "metaData.firstProperty", source = "firstProperty")
    )
    fun flatToResponse(flatDTO: FlatDTO): Response
}

当我尝试使用

mvn clean install
构建代码时 我收到以下错误。

error: Property "firstProperty" has no write accessor in MetaData for target name "metaData.firstProperty".
[ERROR]     @org.mapstruct.Mappings(value = {@org.mapstruct.Mapping(target = "infoData.id", source = "id"), @org.mapstruct.Mapping(target = "metaData.firstProperty", source = "firstProperty")})

我理解这条消息试图说明

firstProperty
没有 setter 函数,因为它被定义为
val
但该代码无法编辑。我可以编写自己的自定义映射器,效果很好。

我想了解在这种情况下是否有办法使用

MapStruct

kotlin openapi mapstruct openapi-generator
1个回答
0
投票

我对此了解不多。但请为字段添加设置器。

© www.soinside.com 2019 - 2024. All rights reserved.