Skip to content

#232: Do not resolve public setters and public fields for records #233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/main/java/org/mapstruct/intellij/util/TargetUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public static PsiType getRelevantType(@NotNull PsiMethod mappingMethod) {
}

/**
* Extract all public write accessors (public setters and fields)
* with their psi substitutors from the given {@code psiType}
* Extract all public write accessors with their psi substitutors from the given {@code psiType}.
* These accessors are constructor parameters and, if it is not a record, also public fields and setters.
*
* @param psiType to use to extract the accessors
* @param mapStructVersion the MapStruct project version
Expand All @@ -101,11 +101,8 @@ public static PsiType getRelevantType(@NotNull PsiMethod mappingMethod) {
*/
public static Map<String, Pair<? extends PsiElement, PsiSubstitutor>> publicWriteAccessors(@NotNull PsiType psiType,
MapStructVersion mapStructVersion, MapstructUtil mapstructUtil, PsiMethod mappingMethod) {
boolean builderSupportPresent = mapStructVersion.isBuilderSupported();
Pair<PsiClass, TargetType> classAndType = resolveBuilderOrSelfClass(
psiType,
builderSupportPresent && isBuilderEnabled( mappingMethod )
);
boolean builderPresent = mapStructVersion.isBuilderSupported() && isBuilderEnabled( mappingMethod );
Pair<PsiClass, TargetType> classAndType = resolveBuilderOrSelfClass( psiType, builderPresent );
if ( classAndType == null ) {
return Collections.emptyMap();
}
Expand All @@ -116,9 +113,10 @@ builderSupportPresent && isBuilderEnabled( mappingMethod )
TargetType targetType = classAndType.getSecond();
PsiType typeToUse = targetType.type();

publicWriteAccessors.putAll( publicSetters( psiClass, typeToUse, mapstructUtil,
builderSupportPresent && isBuilderEnabled( mappingMethod ) ) );
publicWriteAccessors.putAll( publicFields( psiClass ) );
if ( !psiClass.isRecord() ) {
publicWriteAccessors.putAll( publicSetters( psiClass, typeToUse, mapstructUtil, builderPresent ) );
publicWriteAccessors.putAll( publicFields( psiClass ) );
}

if ( mapStructVersion.isConstructorSupported() && !targetType.builder() ) {
publicWriteAccessors.putAll( constructorParameters( psiClass ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private void assertCarDtoWithConstructorAutoComplete() {
assertThat( myItems )
.extracting( LookupElementPresentation::renderElement )
.usingRecursiveFieldByFieldElementComparator()
.usingElementComparatorIgnoringFields( "myIcon", "myTail" )
.usingRecursiveFieldByFieldElementComparatorIgnoringFields( "myIcon", "myTail" )
.containsExactlyInAnyOrder(
createParameter( "make", "String" ),
createParameter( "seatCount", "int" ),
Expand Down Expand Up @@ -234,7 +234,7 @@ private void assertCarDtoWithConstructorAndSettersAutoComplete() {
assertThat( myItems )
.extracting( LookupElementPresentation::renderElement )
.usingRecursiveFieldByFieldElementComparator()
.usingElementComparatorIgnoringFields( "myIcon", "myTail" )
.usingRecursiveFieldByFieldElementComparatorIgnoringFields( "myIcon", "myTail" )
.containsExactlyInAnyOrder(
createParameter( "make", "String" ),
createParameter( "seatCount", "int" ),
Expand Down Expand Up @@ -327,7 +327,7 @@ public void testCarMapperReturnTargetCarDtoWithMultipleConstructorsAndAnnotatedW
assertThat( myItems )
.extracting( LookupElementPresentation::renderElement )
.usingRecursiveFieldByFieldElementComparator()
.usingElementComparatorIgnoringFields( "myIcon", "myTail" )
.usingRecursiveFieldByFieldElementComparatorIgnoringFields( "myIcon", "myTail" )
.containsExactlyInAnyOrder(
createParameter( "make", "String" ),
createParameter( "seatCount", "int" ),
Expand Down Expand Up @@ -401,7 +401,7 @@ public void testNestedSecondLevelAutoCompleteConstructorTargetProperty() {
assertThat( myItems )
.extracting( LookupElementPresentation::renderElement )
.usingRecursiveFieldByFieldElementComparator()
.usingElementComparatorIgnoringFields( "myIcon", "myTail" )
.usingRecursiveFieldByFieldElementComparatorIgnoringFields( "myIcon", "myTail" )
.containsExactlyInAnyOrder(
createParameter( "name", "String" )
);
Expand Down Expand Up @@ -451,7 +451,7 @@ public void testMultipleSourceParametersUpdateMapping() {
assertThat( myItems )
.extracting( LookupElementPresentation::renderElement )
//For some reason the icon is empty in the returned items. However, in actual completion it is OK
.usingElementComparatorIgnoringFields( "myIcon" )
.usingRecursiveFieldByFieldElementComparatorIgnoringFields( "myIcon" )
.containsExactlyInAnyOrder(
createParameter( "source1", "Car" ),
createParameter( "source2", "Car" ),
Expand Down
8 changes: 7 additions & 1 deletion testData/inspection/UnmappedRecordTargetProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@

record Source(String name, String matching, String moreSource, String onlyInSource) { }

record Target(String testName, String matching, String moreTarget) { }
record Target(String testName, String matching, String moreTarget) {

public Target restrict(Target target) {
return this;
}

}

interface NotMapStructMapper {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@

record Source(String name, String matching, String moreSource, String onlyInSource) { }

record Target(String testName, String matching, String moreTarget) { }
record Target(String testName, String matching, String moreTarget) {

public Target restrict(Target target) {
return this;
}

}

interface NotMapStructMapper {

Expand Down