#1345 ignoring reversed mappings with no target accessor silently

This commit is contained in:
sjaakd 2018-01-11 23:15:17 +01:00
parent 3d26318301
commit 4c1dcc5272
5 changed files with 111 additions and 9 deletions

View File

@ -37,7 +37,6 @@ import javax.lang.model.util.Types;
import org.mapstruct.ap.internal.model.common.FormattingParameters;
import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.TypeFactory;
import org.mapstruct.ap.internal.prism.CollectionMappingStrategyPrism;
import org.mapstruct.ap.internal.prism.MappingPrism;
import org.mapstruct.ap.internal.prism.MappingsPrism;
import org.mapstruct.ap.internal.util.FormattingMessager;
@ -390,11 +389,6 @@ public class Mapping {
return dependsOn;
}
private boolean hasPropertyInReverseMethod(String name, SourceMethod method) {
CollectionMappingStrategyPrism cms = method.getMapperConfiguration().getCollectionMappingStrategy();
return method.getResultType().getPropertyWriteAccessors( cms ).containsKey( name );
}
public Mapping reverse(SourceMethod method, FormattingMessager messager, TypeFactory typeFactory) {
// mapping can only be reversed if the source was not a constant nor an expression nor a nested property
@ -426,6 +420,12 @@ public class Mapping {
true,
sourceReference != null ? sourceReference.getParameter() : null
);
// check if the reverse mapping has indeed a write accessor, otherwise the mapping cannot be reversed
if ( !reverse.targetReference.isValid() ) {
return null;
}
return reverse;
}

View File

@ -170,7 +170,7 @@ public class TargetReference {
foundEntryMatch = (entries.size() == targetPropertyNames.length);
}
if ( !foundEntryMatch && errorMessage != null) {
if ( !foundEntryMatch && errorMessage != null && !isReverse ) {
// This is called only for reporting errors
errorMessage.report( isReverse );
}

View File

@ -0,0 +1,62 @@
/**
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.ap.test.bugs._1345;
import org.mapstruct.InheritInverseConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
/**
*
* @author Sjaak Derksen
*/
@Mapper
public interface Issue1345Mapper {
Issue1345Mapper INSTANCE = Mappers.getMapper( Issue1345Mapper.class );
@Mapping(target = "property", source = "readOnlyProperty")
B a2B(A a);
@InheritInverseConfiguration(name = "a2B")
A b2A(B b);
class A {
private String readOnlyProperty;
public String getReadOnlyProperty() {
return readOnlyProperty;
}
}
class B {
private String property;
public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property = property;
}
}
}

View File

@ -0,0 +1,40 @@
/**
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.ap.test.bugs._1345;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
/**
* @author Sjaak Derksen
*/
@WithClasses({
Issue1345Mapper.class
})
@IssueKey("1345")
@RunWith(AnnotationProcessorTestRunner.class)
public class Issue1345Test {
@Test
public void shouldCompile() {
}
}

View File

@ -183,8 +183,8 @@ public class NestedSourcePropertiesTest {
diagnostics = {
@Diagnostic( type = ArtistToChartEntryErroneous.class,
kind = javax.tools.Diagnostic.Kind.ERROR,
line = 46,
messageRegExp = "Unknown property \"position\" in result type java\\.lang\\.Integer\\." )
line = 47,
messageRegExp = "java.lang.Integer does not have an accessible empty constructor." )
}
)
@WithClasses({ ArtistToChartEntryErroneous.class })