#3565 Presence check methods should not be considered as valid mapping candidates

This commit is contained in:
Filip Hrisafov 2024-04-28 19:04:57 +02:00
parent 5fbd36c443
commit 0a935c67a7
3 changed files with 93 additions and 0 deletions

View File

@ -470,6 +470,9 @@ public class MappingResolverImpl implements MappingResolver {
}
private boolean isCandidateForMapping(Method methodCandidate) {
if ( methodCandidate.isPresenceCheck() ) {
return false;
}
return isCreateMethodForMapping( methodCandidate ) || isUpdateMethodForMapping( methodCandidate );
}

View File

@ -0,0 +1,57 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.bugs._3565;
import java.util.Optional;
import org.mapstruct.Condition;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
/**
* @author Filip Hrisafov
*/
@Mapper
public interface Issue3565Mapper {
Issue3565Mapper INSTANCE = Mappers.getMapper( Issue3565Mapper.class );
default <T> T mapFromOptional(Optional<T> value) {
return value.orElse( (T) null );
}
@Condition
default boolean isOptionalPresent(Optional<?> value) {
return value.isPresent();
}
Target map(Source source);
class Source {
private final Boolean condition;
public Source(Boolean condition) {
this.condition = condition;
}
public Optional<Boolean> getCondition() {
return Optional.ofNullable( this.condition );
}
}
class Target {
private String condition;
public Optional<String> getCondition() {
return Optional.ofNullable( this.condition );
}
public void setCondition(String condition) {
this.condition = condition;
}
}
}

View File

@ -0,0 +1,33 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.bugs._3565;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.ProcessorTest;
import org.mapstruct.ap.testutil.WithClasses;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Filip Hrisafov
*/
@WithClasses(Issue3565Mapper.class)
@IssueKey("3565")
class Issue3565Test {
@ProcessorTest
void shouldGenerateValidCode() {
Issue3565Mapper.Target target = Issue3565Mapper.INSTANCE.map( new Issue3565Mapper.Source( null ) );
assertThat( target ).isNotNull();
assertThat( target.getCondition() ).isEmpty();
target = Issue3565Mapper.INSTANCE.map( new Issue3565Mapper.Source( false ) );
assertThat( target ).isNotNull();
assertThat( target.getCondition() ).hasValue( "false" );
}
}