Skip to content

Commit

Permalink
mapstruct#131, mapstruct#2438: Brought the situation from issue 2438 …
Browse files Browse the repository at this point in the history
…under test and fixed inheritence issues.
  • Loading branch information
Zegveld committed Jul 9, 2021
1 parent a1c5800 commit b10bafb
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 22 deletions.
Expand Up @@ -200,26 +200,7 @@ public void applyInheritedOptions(SourceMethod templateMethod, boolean isInverse
}
}

if ( getSubClassMappings() == null ) {
if ( templateOptions.getSubClassMappings() != null ) {
// there were no mappings, so the inherited mappings are the new ones
setSubClassMappings( templateOptions.getSubClassMappings() );
}
else {
setSubClassMappings( Collections.emptyList() );
}
}
else {
if ( templateOptions.getSubClassMappings() != null ) {
// iff there are also inherited mappings, we inverse and add them.
for ( SubClassMappingOptions inheritedSubClassMapping : templateOptions.getSubClassMappings() ) {
if ( inheritedSubClassMapping != null
&& !getSubClassMappings().contains( inheritedSubClassMapping ) ) {
getSubClassMappings().add( inheritedSubClassMapping );
}
}
}
}
// Do NOT inherit subclass mapping options!!!

Set<MappingOptions> newMappings = new LinkedHashSet<>();
for ( MappingOptions mapping : templateOptions.getMappings() ) {
Expand Down
Expand Up @@ -9,6 +9,7 @@
import static org.mapstruct.ap.internal.util.Message.SUBCLASSMAPPING_METHOD_SIGNATURE_NOT_SUPPORTED;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -44,13 +45,22 @@ public static List<SubClassMappingOptions> getInstanceOn(ExecutableElement metho
FormattingMessager messager, TypeUtils typeUtils) {

SubClassMappingsGem subClassMappings = SubClassMappingsGem.instanceOn( method );
List<SubClassMappingGem> subClassMappingAnnotations;
if ( subClassMappings == null ) {
return Collections.emptyList();
if ( SubClassMappingGem.instanceOn( method ) != null ) {
subClassMappingAnnotations = Arrays.asList( SubClassMappingGem.instanceOn( method ) );
}
else {
return Collections.emptyList();
}
}
else {
subClassMappingAnnotations = subClassMappings.value().get();
}

List<SubClassMappingOptions> subClassMappingOptions = new ArrayList<>();

for ( SubClassMappingGem subClassMapping : subClassMappings.value().get() ) {
for ( SubClassMappingGem subClassMapping : subClassMappingAnnotations ) {

if ( !isConsistent( subClassMapping, method, messager, typeUtils ) ) {
continue;
Expand Down
@@ -0,0 +1,45 @@
/*
* 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._2438;

import org.assertj.core.api.Assertions;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.ProcessorTest;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.factory.Mappers;

@IssueKey( "2438" )
@WithClasses( { Source.class, SourceSubclass.class, SubclassMapper.class, Target.class } )
class Issue2438Test {

@ProcessorTest
void inheretenceTest() {
SubclassMapper mapper = Mappers.getMapper( SubclassMapper.class );
SourceSubclass sourceSubclass = new SourceSubclass( "f1", "f2", "f3", "f4", "f5" );

Target result = mapper.mapSuperclass( sourceSubclass );

Assertions.assertThat( result.getTarget1() ).isEqualTo( "f1" );
Assertions.assertThat( result.getTarget2() ).isEqualTo( "f2" );
Assertions.assertThat( result.getTarget3() ).isEqualTo( "f3" );
Assertions.assertThat( result.getTarget4() ).isEqualTo( "f4" );
Assertions.assertThat( result.getTarget5() ).isEqualTo( "f5" );
}

@ProcessorTest
void superclassTest() {
SubclassMapper mapper = Mappers.getMapper( SubclassMapper.class );
Source source = new Source( "f1", "f2", "f3" );

Target result = mapper.mapSuperclass( source );

Assertions.assertThat( result.getTarget1() ).isEqualTo( "f1" );
Assertions.assertThat( result.getTarget2() ).isEqualTo( "f2" );
Assertions.assertThat( result.getTarget3() ).isEqualTo( "f3" );
Assertions.assertThat( result.getTarget4() ).isNull();
Assertions.assertThat( result.getTarget5() ).isNull();
}
}
@@ -0,0 +1,42 @@
/*
* 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._2438;

public class Source {
private String property1;
private String property2;
private String property3;

public Source(String prop1, String prop2, String prop3) {
setProperty1( prop1 );
setProperty2( prop2 );
setProperty3( prop3 );
}

public String getProperty1() {
return property1;
}

public String getProperty2() {
return property2;
}

public String getProperty3() {
return property3;
}

public void setProperty1(String property1) {
this.property1 = property1;
}

public void setProperty2(String property2) {
this.property2 = property2;
}

public void setProperty3(String property3) {
this.property3 = property3;
}
}
@@ -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._2438;

public class SourceSubclass extends Source {
private String subclassProperty;
private String target5;

public SourceSubclass(String prop1, String prop2, String prop3, String subProp, String target5) {
super( prop1, prop2, prop3 );
setSubclassProperty( subProp );
setTarget5( target5 );
}

public String getSubclassProperty() {
return subclassProperty;
}

public String getTarget5() {
return target5;
}

public void setSubclassProperty(String subclassProperty) {
this.subclassProperty = subclassProperty;
}

public void setTarget5(String target5) {
this.target5 = target5;
}
}
@@ -0,0 +1,28 @@
/*
* 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._2438;

import org.mapstruct.InheritConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.SubClassMapping;

@Mapper
public interface SubclassMapper {

@SubClassMapping( sourceClass = SourceSubclass.class, targetClass = Target.class )
@Mapping( target = "target1", source = "property1" )
@Mapping( target = "target2", source = "property2" )
@Mapping( target = "target3", source = "property3" )
@Mapping( target = "target4", expression = "java(null)" )
@Mapping( target = "target5", expression = "java(null)" )
Target mapSuperclass(Source source);

@InheritConfiguration( name = "mapSuperclass" )
@Mapping( target = "target4", source = "subclassProperty" )
@Mapping( target = "target5" ) // Have to declare in order to override with default behavior
Target mapSubclass(SourceSubclass source);
}
@@ -0,0 +1,54 @@
/*
* 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._2438;

public class Target {
private String target1;
private String target2;
private String target3;
private String target4;
private String target5;

public String getTarget1() {
return target1;
}

public String getTarget2() {
return target2;
}

public String getTarget3() {
return target3;
}

public String getTarget4() {
return target4;
}

public String getTarget5() {
return target5;
}

public void setTarget1(String target1) {
this.target1 = target1;
}

public void setTarget2(String target2) {
this.target2 = target2;
}

public void setTarget3(String target3) {
this.target3 = target3;
}

public void setTarget4(String target4) {
this.target4 = target4;
}

public void setTarget5(String target5) {
this.target5 = target5;
}
}

0 comments on commit b10bafb

Please sign in to comment.