#2825 Fix SubclassMapping stackoverflow exception

This commit is contained in:
Orange Add 2022-08-28 18:41:25 +08:00 committed by Filip Hrisafov
parent 290189652c
commit 473d581528
7 changed files with 158 additions and 3 deletions

View File

@ -196,7 +196,7 @@ public class MappingResolverImpl implements MappingResolver {
this.mappingMethod = mappingMethod;
this.description = description;
this.methods = filterPossibleCandidateMethods( sourceModel );
this.methods = filterPossibleCandidateMethods( sourceModel, mappingMethod );
this.formattingParameters =
formattingParameters == null ? FormattingParameters.EMPTY : formattingParameters;
this.sourceRHS = sourceRHS;
@ -210,10 +210,10 @@ public class MappingResolverImpl implements MappingResolver {
}
// CHECKSTYLE:ON
private <T extends Method> List<T> filterPossibleCandidateMethods(List<T> candidateMethods) {
private <T extends Method> List<T> filterPossibleCandidateMethods(List<T> candidateMethods, T mappingMethod) {
List<T> result = new ArrayList<>( candidateMethods.size() );
for ( T candidate : candidateMethods ) {
if ( isCandidateForMapping( candidate ) ) {
if ( isCandidateForMapping( candidate ) && !candidate.equals( mappingMethod )) {
result.add( candidate );
}
}

View File

@ -0,0 +1,21 @@
/*
* 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._2825;
/**
* @author orange add
*/
public class Animal {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,21 @@
/*
* 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._2825;
/**
* @author orange add
*/
public class Cat extends Animal {
private String race;
public String getRace() {
return race;
}
public void setRace(String race) {
this.race = race;
}
}

View File

@ -0,0 +1,21 @@
/*
* 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._2825;
/**
* @author orange add
*/
public class Dog extends Animal {
private String race;
public String getRace() {
return race;
}
public void setRace(String race) {
this.race = race;
}
}

View File

@ -0,0 +1,24 @@
/*
* 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._2825;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.SubclassMapping;
import org.mapstruct.factory.Mappers;
/**
* @author orange add
*/
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface Issue2825Mapper {
Issue2825Mapper INSTANCE = Mappers.getMapper( Issue2825Mapper.class );
@SubclassMapping(target = TargetAnimal.class, source = Dog.class)
@SubclassMapping(target = TargetAnimal.class, source = Cat.class)
TargetAnimal map(Animal source);
}

View File

@ -0,0 +1,37 @@
/*
* 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._2825;
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 orange add
*/
@IssueKey("2825")
@WithClasses({
Animal.class,
Cat.class,
Dog.class,
Issue2825Mapper.class,
TargetAnimal.class,
})
public class Issue2825Test {
@ProcessorTest
public void mappingMethodShouldNotBeReusedForSubclassMappings() {
Dog dog = new Dog();
dog.setName( "Lucky" );
dog.setRace( "Shepherd" );
TargetAnimal target = Issue2825Mapper.INSTANCE.map( dog );
assertThat( target.getName() ).isEqualTo( "Lucky" );
assertThat( target.getRace() ).isEqualTo( "Shepherd" );
}
}

View File

@ -0,0 +1,31 @@
/*
* 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._2825;
/**
* @author orange add
*/
public class TargetAnimal {
private String name;
private String race;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRace() {
return race;
}
public void setRace(String race) {
this.race = race;
}
}