#3652 Inverse Inheritance should be possible for ignore-mappings without source

This commit is contained in:
Stefan Simon 2024-08-18 17:46:35 +02:00 committed by GitHub
parent 96d0698417
commit b452d7f2c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 135 additions and 3 deletions

View File

@ -4,6 +4,8 @@
### Bugs
* Inverse Inheritance Strategy not working for ignored mappings only with target (#3652)
### Documentation
### Build

View File

@ -479,13 +479,12 @@ public class MappingOptions extends DelegatingOptions {
}
/**
* mapping can only be inversed if the source was not a constant nor an expression nor a nested property
* and the mapping is not a 'target-source-ignore' mapping
* Mapping can only be inversed if the source was not a constant nor an expression
*
* @return true when the above applies
*/
public boolean canInverse() {
return constant == null && javaExpression == null && !( isIgnored && sourceName == null );
return constant == null && javaExpression == null;
}
public MappingOptions copyForInverseInheritance(SourceMethod templateMethod,

View File

@ -0,0 +1,30 @@
/*
* 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._3652;
public class Bar {
private int secret;
private int doesNotExistInFoo;
public int getSecret() {
return secret;
}
public void setSecret(int secret) {
this.secret = secret;
}
public int getDoesNotExistInFoo() {
return doesNotExistInFoo;
}
public void setDoesNotExistInFoo(int doesNotExistInFoo) {
this.doesNotExistInFoo = doesNotExistInFoo;
}
}

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._3652;
public class Foo {
private int secret;
public int getSecret() {
return secret;
}
public void setSecret(int secret) {
this.secret = secret;
}
}

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._3652;
import org.mapstruct.InheritInverseConfiguration;
import org.mapstruct.MapperConfig;
import org.mapstruct.Mapping;
import org.mapstruct.MappingInheritanceStrategy;
@MapperConfig(mappingInheritanceStrategy = MappingInheritanceStrategy.AUTO_INHERIT_ALL_FROM_CONFIG)
public interface FooBarConfig {
@Mapping(target = "doesNotExistInFoo", ignore = true)
@Mapping(target = "secret", ignore = true)
Bar toBar(Foo foo);
@InheritInverseConfiguration(name = "toBar")
Foo toFoo(Bar bar);
}

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._3652;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@Mapper(config = FooBarConfig.class)
public interface FooBarMapper {
FooBarMapper INSTANCE = Mappers.getMapper( FooBarMapper.class );
Bar toBar(Foo foo);
Foo toFoo(Bar bar);
}

View File

@ -0,0 +1,35 @@
/*
* 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._3652;
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;
@IssueKey("3652")
public class Issue3652Test {
@WithClasses({
Bar.class,
Foo.class,
FooBarConfig.class,
FooBarMapper.class,
})
@ProcessorTest
void ignoreMappingsWithoutSourceShouldBeInvertible() {
Bar bar = new Bar();
bar.setSecret( 123 );
bar.setDoesNotExistInFoo( 6 );
Foo foo = FooBarMapper.INSTANCE.toFoo( bar );
assertThat( foo.getSecret() ).isEqualTo( 0 );
}
}