diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/MappingOptions.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/MappingOptions.java index 8471210d1..2b4c1b518 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/MappingOptions.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/MappingOptions.java @@ -30,6 +30,7 @@ import java.util.Map; import java.util.Set; import org.mapstruct.ap.internal.model.common.Parameter; +import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.TypeFactory; import org.mapstruct.ap.internal.prism.CollectionMappingStrategyPrism; import org.mapstruct.ap.internal.util.FormattingMessager; @@ -298,7 +299,11 @@ public class MappingOptions { public void applyIgnoreAll(MappingOptions inherited, SourceMethod method, FormattingMessager messager, TypeFactory typeFactory ) { CollectionMappingStrategyPrism cms = method.getMapperConfiguration().getCollectionMappingStrategy(); - Map writeAccessors = method.getResultType().getPropertyWriteAccessors( cms ); + Type writeType = method.getResultType(); + if ( !method.isUpdateMethod() ) { + writeType = writeType.getEffectiveType(); + } + Map writeAccessors = writeType.getPropertyWriteAccessors( cms ); List mappedPropertyNames = new ArrayList(); for ( String targetMappingName : mappings.keySet() ) { mappedPropertyNames.add( targetMappingName.split( "\\." )[0] ); diff --git a/processor/src/test/java/org/mapstruct/ap/test/builder/ignore/BaseDto.java b/processor/src/test/java/org/mapstruct/ap/test/builder/ignore/BaseDto.java new file mode 100644 index 000000000..50b6d35e5 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builder/ignore/BaseDto.java @@ -0,0 +1,35 @@ +/** + * 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.builder.ignore; + +/** + * @author Filip Hrisafov + */ +public class BaseDto { + + private Long id; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builder/ignore/BaseEntity.java b/processor/src/test/java/org/mapstruct/ap/test/builder/ignore/BaseEntity.java new file mode 100644 index 000000000..24ee73188 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builder/ignore/BaseEntity.java @@ -0,0 +1,52 @@ +/** + * 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.builder.ignore; + +/** + * @author Filip Hrisafov + */ +public class BaseEntity { + + private final Long id; + + public BaseEntity(Builder builder) { + this.id = builder.id; + } + + public Long getId() { + return id; + } + + public static Builder baseBuilder() { + return new Builder(); + } + + public static class Builder { + private Long id; + + public Builder id(Long id) { + this.id = id; + return this; + } + + public BaseEntity createBase() { + return new BaseEntity( this ); + } + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builder/ignore/BuilderIgnoringMapper.java b/processor/src/test/java/org/mapstruct/ap/test/builder/ignore/BuilderIgnoringMapper.java new file mode 100644 index 000000000..e3c8dbd81 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builder/ignore/BuilderIgnoringMapper.java @@ -0,0 +1,43 @@ +/** + * 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.builder.ignore; + +import org.mapstruct.BeanMapping; +import org.mapstruct.InheritConfiguration; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.factory.Mappers; + +/** + * @author Filip Hrisafov + */ +@Mapper(config = BuilderIgnoringMappingConfig.class) +public interface BuilderIgnoringMapper { + + BuilderIgnoringMapper INSTANCE = Mappers.getMapper( BuilderIgnoringMapper.class ); + + @InheritConfiguration(name = "mapBase") + Person mapWithIgnoringBase(PersonDto source); + + @BeanMapping(ignoreByDefault = true) + @Mapping(target = "name", source = "name") + Person mapOnlyWithExplicit(PersonDto source); + + Person mapAll(PersonDto source); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builder/ignore/BuilderIgnoringMappingConfig.java b/processor/src/test/java/org/mapstruct/ap/test/builder/ignore/BuilderIgnoringMappingConfig.java new file mode 100644 index 000000000..67a699e53 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builder/ignore/BuilderIgnoringMappingConfig.java @@ -0,0 +1,32 @@ +/** + * 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.builder.ignore; + +import org.mapstruct.BeanMapping; +import org.mapstruct.MapperConfig; + +/** + * @author Filip Hrisafov + */ +@MapperConfig +public interface BuilderIgnoringMappingConfig { + + @BeanMapping(ignoreByDefault = true) + BaseEntity mapBase(BaseDto dto); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builder/ignore/BuilderIgnoringTest.java b/processor/src/test/java/org/mapstruct/ap/test/builder/ignore/BuilderIgnoringTest.java new file mode 100644 index 000000000..0538aeda2 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builder/ignore/BuilderIgnoringTest.java @@ -0,0 +1,85 @@ +/** + * 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.builder.ignore; + +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; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Filip Hrisafov + */ +@RunWith(AnnotationProcessorTestRunner.class) +@IssueKey("1452") +@WithClasses({ + BaseDto.class, + BaseEntity.class, + BuilderIgnoringMapper.class, + BuilderIgnoringMappingConfig.class, + Person.class, + PersonDto.class +}) +public class BuilderIgnoringTest { + + @Test + public void shouldIgnoreBase() { + PersonDto source = new PersonDto(); + source.setId( 100L ); + source.setName( "John" ); + source.setLastName( "Doe" ); + + Person target = BuilderIgnoringMapper.INSTANCE.mapWithIgnoringBase( source ); + + assertThat( target.getId() ).isNull(); + assertThat( target.getName() ).isEqualTo( "John" ); + assertThat( target.getLastName() ).isEqualTo( "Doe" ); + } + + @Test + public void shouldMapOnlyExplicit() { + PersonDto source = new PersonDto(); + source.setId( 100L ); + source.setName( "John" ); + source.setLastName( "Doe" ); + + Person target = BuilderIgnoringMapper.INSTANCE.mapOnlyWithExplicit( source ); + + assertThat( target.getId() ).isNull(); + assertThat( target.getName() ).isEqualTo( "John" ); + assertThat( target.getLastName() ).isNull(); + } + + @Test + public void shouldMapAll() { + PersonDto source = new PersonDto(); + source.setId( 100L ); + source.setName( "John" ); + source.setLastName( "Doe" ); + + Person target = BuilderIgnoringMapper.INSTANCE.mapAll( source ); + + assertThat( target.getId() ).isEqualTo( 100L ); + assertThat( target.getName() ).isEqualTo( "John" ); + assertThat( target.getLastName() ).isEqualTo( "Doe" ); + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builder/ignore/Person.java b/processor/src/test/java/org/mapstruct/ap/test/builder/ignore/Person.java new file mode 100644 index 000000000..0fafbae7c --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builder/ignore/Person.java @@ -0,0 +1,66 @@ +/** + * 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.builder.ignore; + +/** + * @author Filip Hrisafov + */ +public class Person extends BaseEntity { + + private final String name; + private final String lastName; + + public Person(Builder builder) { + super( builder ); + this.name = builder.name; + this.lastName = builder.lastName; + } + + public String getName() { + return name; + } + + public String getLastName() { + return lastName; + } + + public static Builder builder() { + return new Builder(); + + } + + public static class Builder extends BaseEntity.Builder { + private String name; + private String lastName; + + public Builder name(String name) { + this.name = name; + return this; + } + + public Builder lastName(String lastName) { + this.lastName = lastName; + return this; + } + + public Person create() { + return new Person( this ); + } + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builder/ignore/PersonDto.java b/processor/src/test/java/org/mapstruct/ap/test/builder/ignore/PersonDto.java new file mode 100644 index 000000000..b3b137f9f --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builder/ignore/PersonDto.java @@ -0,0 +1,44 @@ +/** + * 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.builder.ignore; + +/** + * @author Filip Hrisafov + */ +public class PersonDto extends BaseDto { + + private String name; + private String lastName; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } +}