You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MapStruct映射Kotlin布尔类型数据类属性未正确赋值:是Bug还是使用误区?

Why MapStruct Fails to Map isActive Boolean Property Between Kotlin Data Classes?

This is not a MapStruct bug—it’s a common pitfall caused by the interaction between Kotlin’s property naming conventions and JavaBean standards, which MapStruct relies on for property matching.

Let’s Break Down the Root Cause

Kotlin handles boolean properties starting with is differently when compiling to Java bytecode, following strict JavaBean rules:

  • For a Kotlin property val isActive: Boolean, the compiler generates a getter method isActive() (instead of getIsActive() as you might expect for non-boolean properties).
  • Crucially, MapStruct interprets the underlying JavaBean property name as active (stripping the is prefix for boolean types), even though the Kotlin data class constructor uses the full isActive parameter name.

When both your source (InObj) and target (OutObj) classes use isActive as the property name:

  • MapStruct tries to map the source’s inferred JavaBean property active to the target’s JavaBean property active.
  • But the target’s constructor expects a parameter named isActive, not active. MapStruct can’t connect these two, so it falls back to initializing the target property with the default false value instead of pulling it from the source.

When you rename the target property to active:

  • The Kotlin compiler generates a JavaBean property named active with a getter getActive(), and the constructor parameter is also active.
  • MapStruct can now directly match the source’s inferred active property to the target’s active property, so the mapping works correctly.

Fixes to Resolve the Issue

You have a few straightforward solutions to fix this:

1. Explicitly Define the Mapping with @Mapping

Tell MapStruct exactly which properties to map, bypassing JavaBean naming inference:

@Mapper
interface Mapper {
    @Mapping(source = "isActive", target = "isActive")
    fun inToOut(inObj: InObj): OutObj
}

This forces MapStruct to use the literal property names from your Kotlin data classes, resulting in correct assignment of isActive from source to target.

2. Use @JvmField to Expose Raw Fields

Annotate your Kotlin properties with @JvmField to make them visible as direct Java fields, instead of relying on JavaBean getters/setters. MapStruct will then use the field names directly:

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

With this, MapStruct recognizes isActive as a field name in both classes and maps them correctly without extra configuration.

3. Align Naming with JavaBean Conventions

If you prefer to avoid annotations, rename your properties to drop the is prefix (e.g., val active: Boolean in both classes). Kotlin will still let you use isActive() as a getter via convention, and MapStruct will handle the mapping seamlessly.

内容的提问来源于stack exchange,提问作者rsobies

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 15:37:46