Instantiate Kotlin data classes using the _primary_ constructor #2933
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.