Kotlin data class with default values using MapStruct
Asked Answered
W

1

7

I have a kotlin data class with default values and when I try to map it using MapStruct it throws an error at runtime because it will try to assign a null value to a non-nullable type for a property with a default value. I'm aware of assigning default values in @Mapping annotation but is there a way for MapStruct to take data class default value into consideration rather than me having to do it twice?

Here's the example:

data class A(val property1: String = "prop 1", val property2: String)
data class B(val property2: String)

@Mapper
interface SomeMapper {
...
     fun mapBtoA(b: B): A
}

val b = B("prop 2 val")
val a: A = SomeMapper.INSTANCE.mapBtoA(b)

In the above example it will try to assign null to property1 instead of prop 1 default value.

Willin answered 15/3, 2022 at 15:28 Comment(0)
G
1

I had the same issue, you need to use var instead of val

Griseofulvin answered 22/6, 2022 at 15:12 Comment(1)
Thanks for this solution. It still looks like a bug in MapStruct.Tribulation

© 2022 - 2024 — McMap. All rights reserved.