#4060 Report compilation error for SET_TO_DEFAULT without accessible no-args constructor#4061
Merged
hduelme merged 2 commits intoJun 7, 2026
Conversation
…cessible no-args constructor When NullValuePropertyMappingStrategy.SET_TO_DEFAULT resets a null source property, MapStruct generates new Target() to create the default instance. For target types without an accessible parameterless constructor (e.g. LocalDate, BigDecimal, Comparable) this produced uncompilable code such as target.setLocalDate( new LocalDate() ). Detect this during annotation processing and raise GENERAL_NO_SUITABLE_CONSTRUCTOR instead.
hduelme
requested changes
Jun 4, 2026
hduelme
left a comment
Contributor
There was a problem hiding this comment.
@seonwooj0810 nice implementation.
I left two comments: one regarding the error message and one suggesting a small test improvement.
Address review feedback: - Introduce PROPERTYMAPPING_NO_ACCESSIBLE_PARAMETERLESS_CONSTRUCTOR with a message that names the actual problem (no accessible parameterless constructor) and suggests fixes (change the nullValuePropertyMappingStrategy or define a defaultValue / defaultExpression), instead of reusing GENERAL_NO_SUITABLE_CONSTRUCTOR. - Cover abstract class and enum target property types in ErroneousSetToDefaultMapper.
hduelme
approved these changes
Jun 7, 2026
Contributor
|
@seonwooj0810 Thanks for making the adjustments, and thanks again for the contribution! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #4060
Problem
When updating an existing bean with
NullValuePropertyMappingStrategy.SET_TO_DEFAULT, MapStruct resets anullsource property by creating a fresh default instance via the parameterless constructor (new Target()). For target property types that do not expose an accessible no-args constructor (e.g.LocalDate,LocalDateTime,Instant,BigDecimal,BigInteger,Comparable), this generated uncompilable code such as:As requested in the issue, MapStruct should report a compilation error during annotation processing rather than emitting code that fails to compile.
Change
Type#hasAccessibleParameterlessConstructor()— new helper that returnstrueonly whennew Type()would actually compile (an implicit default constructor, or a declared non-private no-args constructor). Unlike the existinghasAccessibleConstructor(), it returnsfalsefor types that only expose parameterized constructors (e.g.BigDecimal).PropertyMapping#assignToPlainViaSetter(...)— for theSET_TO_DEFAULTcase, when the default instance would be built through a parameterless constructor (no factory/builder) and the target type lacks an accessible one, reportGENERAL_NO_SUITABLE_CONSTRUCTOR(the message referenced in the issue). Types that are initialized differently — those with an implementation type (collections/maps), arrays,Optional, or a sensible default — are intentionally excluded.Test evidence
Added
org.mapstruct.ap.test.bugs._4060.Issue4060Test:setToDefaultWithoutParameterlessConstructorFails— asserts the expected compilation errors forLocalDate,BigDecimalandComparable<String>.setToDefaultWithParameterlessConstructorResetsToNewInstance— verifies the existing reset-to-new-instance behaviour still works for constructible types.Local run (JDK toolchain,
processormodule):Issue4060Test— 4 tests, 0 failures.Issue1790Test,Issue3884Test,JSpecifySafetyGuardTest,NullValuePropertyMappingTest,NullValuePropertyMappingClearTest.checkstyle:checkon theprocessormodule passes.Verification done
gh pr list --searchfor 4060 / SET_TO_DEFAULT constructor) and no self-claim comments on the issue..javafiles only.main(453602e): without the change the erroneous mapper compiles and emitsnew LocalDate(); with the change the documented compilation error is raised.bug,hack.commit.push.