Pozo / mapstruct-kotlin

Using mapstruct with kotlin data classes.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool


⚠️ Since 1.4 MapStruct has support for using constructor arguments when instantiating mapping targets. This also works with Kotlin data classes.

Since mapstruct 1.3.0.Beta2 it's possible to use builders for immutable classes. According to the documentation you can implement your custom builder provider logic. This project take advantage of this and provide a custom BuilderProvider for kotlin data classes.

So instead of this (source)

data class PersonDto(var firstName: String?, var lastName: String?, var phone: String?, var birthdate: LocalDate?) {
    // Necessary for MapStruct
    constructor() : this(null, null, null, null)

We can do this

data class PersonDto(val firstName: String, val lastName: String, val phone: String, val birthdate: LocalDate)

With a mapper

interface PersonMapper {
    fun map(person: Person): PersonDto


First apply kapt plugin

apply plugin: 'kotlin-kapt'

Then add these to your project as dependency


Check out the directory example for a basic usage example.


For example in case of the first part 1.3.1 is the mapstruct version number and the last digit 1 reserved for future patches.

Project structure

  • mapstruct-kotlin-builder contains only the KotlinBuilder annotation
  • mapstruct-kotlin-processor responsible for generating the builders for the kotlin data classes with the help of a custom DefaultBuilderProvider
  • example responsible for demonstrating this library usage

Build and run the example application

./gradlew -p example clean build


  • Map with custom types are not working
  • Look over kotlin-builder-annotation project and replace with class generating module (builder-processor)
  • Writing tests
  • Versioning and release process


Please see LICENSE file


Zoltan Polgar - pozo@gmx.com

Please do not hesitate to contact me if you have any further questions.


Using mapstruct with kotlin data classes.

License:MIT License


Language:Kotlin 66.5%Language:Java 33.5%