I have a FlatDTO
that needs to be mapped to a nested Response
containing InfoData
and MetaData
The code for the response is generated by OpenAPI
. So the below definitions can't be changed.
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,
)
I have defined FlatDTO
as follows.
package org.mapstruct.example.kotlin.models
data class FlatDTO(
var id: String? = null,
var firstProperty: String,
)
Here is my mapper class which maps FlatDTO
to 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
}
When I try to build the code using mvn clean install
I get the following error.
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")})
I understand that this message is trying to say that there is no setter function for firstProperty
because it's defined as val
but that code cannot be edited. I can write my own custom mapper that works just fine.
I wanted to understand if there is a way to use MapStruct
in this scenario.