Instantiate Kotlin data classes using the _primary_ constructor#2933
Instantiate Kotlin data classes using the _primary_ constructor#2933jawo-a wants to merge 1 commit intomapstruct:mainfrom
Conversation
Recognizes data classes and the primary constructor by the presence of the annotation `kotlin.Metadata` and `componentN()` methods, although there might be false positives.
|
You could also maybe use the generated copy method as a form of detection? |
|
@sskrla Yes, I've taken a look at decompiled data classes and the generated |
|
This is an interesting approach @jawo-a. However, I am not sure that I like the taken approach with using the component method count. I would prefer in using the kotlinx-metadata-jvm library as an optional dependency instead. Perhaps with a |
|
Hi @filiphr , how would you suggest that |
|
@DavidRomao I've already prepared a change with a Constructor Provider SPI. Going to open a PR in the next few weeks. |
|
Any change with this? |
|
Closing this in favor of #3978 |
A lot of Kotlin projects define DTOs using data classes with optional parameters like
Unfortunately these data classes do not work with MapStruct because the Kotlin compiler generates multiple constructors under the hood.
Issues #2281, #2378 describe this problem.
This PR is an attempt to make MapStruct use the primary constructor of the data class to instantiate it, and ignore all other generated constructors.
How it works?
Recognizes data classes and the primary constructor by
kotlin.Metadata, that is present on any class file produced by the Kotlin compilercomponentN()methods, as described in the documentation about data classes.Note: There might be false positives.
How to review?
BeanMappingMethod.KotlinType.TODO
Improvement ideas
componentN()methods, use their exact return types to more accurately determine the primary constructor.