Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapstruct: kotlin data class with boolean property

two simple data classes

data class InObj(val isActive: Boolean)
data class OutObj(val isActive: Boolean)

@Mapper
interface Mapper {
    fun inToOut(inObj: InObj): OutObj
}

this case will produce the following code, where isActive is not mapped,

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2021-09-29T12:15:55+0200",
    comments = "version: 1.4.2.Final, compiler: IncrementalProcessingEnvironment from kotlin-annotation-processing-gradle-1.5.10.jar, environment: Java 15.0.2 (Oracle Corporation)"
)
public class MapperImpl implements Mapper {

    @Override
    public OutObj inToOut(InObj inObj) {
        if ( inObj == null ) {
            return null;
        }

        boolean isActive = false;

        OutObj outObj = new OutObj( isActive );

        return outObj;
    }
}

when "isActive" property is changed to "active" in OutObj class then mapstruct will correctly mapped this property.

data class InObj(val isActive: Boolean)
data class OutObj(val active: Boolean)

@Mapper
interface Mapper {
    fun inToOut(inObj: InObj): OutObj
}
...
public class MapperImpl implements Mapper {

    @Override
    public OutObj inToOut(InObj inObj) {
        if ( inObj == null ) {
            return null;
        }

        boolean active = false;

        active = inObj.isActive();

        OutObj outObj = new OutObj( active );

        return outObj;
    }
}

so is this a bug in mapstruct or am I missing something?

like image 446
rsobies Avatar asked Oct 20 '25 16:10

rsobies


1 Answers

One way to work around the problem caused by kotlin's clever "design" in this area (see e.g. KT-63097, to see why this was a mistake in my opinion) is to explicitly declare the JVM method names generated for properties as follows:

data class InObj(
    @get:JvmName("getIsActive")
    val isActive: Boolean
)
data class OutObj(
    @get:JvmName("getIsActive")
    val isActive: Boolean
)

If using var, then also specify @set:JvmName("setIsActive")

like image 178
Lukas Eder Avatar answered Oct 23 '25 08:10

Lukas Eder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!