Skip to content

Commit 5c799b7

Browse files
authored
mapstruct#1452 support BeanMapping#ignoreByDefault for builders
1 parent cf19a6b commit 5c799b7

8 files changed

Lines changed: 363 additions & 1 deletion

File tree

processor/src/main/java/org/mapstruct/ap/internal/model/source/MappingOptions.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Set;
3131

3232
import org.mapstruct.ap.internal.model.common.Parameter;
33+
import org.mapstruct.ap.internal.model.common.Type;
3334
import org.mapstruct.ap.internal.model.common.TypeFactory;
3435
import org.mapstruct.ap.internal.prism.CollectionMappingStrategyPrism;
3536
import org.mapstruct.ap.internal.util.FormattingMessager;
@@ -298,7 +299,11 @@ public void applyInheritedOptions(MappingOptions inherited, boolean isInverse, S
298299
public void applyIgnoreAll(MappingOptions inherited, SourceMethod method, FormattingMessager messager,
299300
TypeFactory typeFactory ) {
300301
CollectionMappingStrategyPrism cms = method.getMapperConfiguration().getCollectionMappingStrategy();
301-
Map<String, Accessor> writeAccessors = method.getResultType().getPropertyWriteAccessors( cms );
302+
Type writeType = method.getResultType();
303+
if ( !method.isUpdateMethod() ) {
304+
writeType = writeType.getEffectiveType();
305+
}
306+
Map<String, Accessor> writeAccessors = writeType.getPropertyWriteAccessors( cms );
302307
List<String> mappedPropertyNames = new ArrayList<String>();
303308
for ( String targetMappingName : mappings.keySet() ) {
304309
mappedPropertyNames.add( targetMappingName.split( "\\." )[0] );
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.ap.test.builder.ignore;
20+
21+
/**
22+
* @author Filip Hrisafov
23+
*/
24+
public class BaseDto {
25+
26+
private Long id;
27+
28+
public Long getId() {
29+
return id;
30+
}
31+
32+
public void setId(Long id) {
33+
this.id = id;
34+
}
35+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.ap.test.builder.ignore;
20+
21+
/**
22+
* @author Filip Hrisafov
23+
*/
24+
public class BaseEntity {
25+
26+
private final Long id;
27+
28+
public BaseEntity(Builder builder) {
29+
this.id = builder.id;
30+
}
31+
32+
public Long getId() {
33+
return id;
34+
}
35+
36+
public static Builder baseBuilder() {
37+
return new Builder();
38+
}
39+
40+
public static class Builder {
41+
private Long id;
42+
43+
public Builder id(Long id) {
44+
this.id = id;
45+
return this;
46+
}
47+
48+
public BaseEntity createBase() {
49+
return new BaseEntity( this );
50+
}
51+
}
52+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.ap.test.builder.ignore;
20+
21+
import org.mapstruct.BeanMapping;
22+
import org.mapstruct.InheritConfiguration;
23+
import org.mapstruct.Mapper;
24+
import org.mapstruct.Mapping;
25+
import org.mapstruct.factory.Mappers;
26+
27+
/**
28+
* @author Filip Hrisafov
29+
*/
30+
@Mapper(config = BuilderIgnoringMappingConfig.class)
31+
public interface BuilderIgnoringMapper {
32+
33+
BuilderIgnoringMapper INSTANCE = Mappers.getMapper( BuilderIgnoringMapper.class );
34+
35+
@InheritConfiguration(name = "mapBase")
36+
Person mapWithIgnoringBase(PersonDto source);
37+
38+
@BeanMapping(ignoreByDefault = true)
39+
@Mapping(target = "name", source = "name")
40+
Person mapOnlyWithExplicit(PersonDto source);
41+
42+
Person mapAll(PersonDto source);
43+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.ap.test.builder.ignore;
20+
21+
import org.mapstruct.BeanMapping;
22+
import org.mapstruct.MapperConfig;
23+
24+
/**
25+
* @author Filip Hrisafov
26+
*/
27+
@MapperConfig
28+
public interface BuilderIgnoringMappingConfig {
29+
30+
@BeanMapping(ignoreByDefault = true)
31+
BaseEntity mapBase(BaseDto dto);
32+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.ap.test.builder.ignore;
20+
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.mapstruct.ap.testutil.IssueKey;
24+
import org.mapstruct.ap.testutil.WithClasses;
25+
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* @author Filip Hrisafov
31+
*/
32+
@RunWith(AnnotationProcessorTestRunner.class)
33+
@IssueKey("1452")
34+
@WithClasses({
35+
BaseDto.class,
36+
BaseEntity.class,
37+
BuilderIgnoringMapper.class,
38+
BuilderIgnoringMappingConfig.class,
39+
Person.class,
40+
PersonDto.class
41+
})
42+
public class BuilderIgnoringTest {
43+
44+
@Test
45+
public void shouldIgnoreBase() {
46+
PersonDto source = new PersonDto();
47+
source.setId( 100L );
48+
source.setName( "John" );
49+
source.setLastName( "Doe" );
50+
51+
Person target = BuilderIgnoringMapper.INSTANCE.mapWithIgnoringBase( source );
52+
53+
assertThat( target.getId() ).isNull();
54+
assertThat( target.getName() ).isEqualTo( "John" );
55+
assertThat( target.getLastName() ).isEqualTo( "Doe" );
56+
}
57+
58+
@Test
59+
public void shouldMapOnlyExplicit() {
60+
PersonDto source = new PersonDto();
61+
source.setId( 100L );
62+
source.setName( "John" );
63+
source.setLastName( "Doe" );
64+
65+
Person target = BuilderIgnoringMapper.INSTANCE.mapOnlyWithExplicit( source );
66+
67+
assertThat( target.getId() ).isNull();
68+
assertThat( target.getName() ).isEqualTo( "John" );
69+
assertThat( target.getLastName() ).isNull();
70+
}
71+
72+
@Test
73+
public void shouldMapAll() {
74+
PersonDto source = new PersonDto();
75+
source.setId( 100L );
76+
source.setName( "John" );
77+
source.setLastName( "Doe" );
78+
79+
Person target = BuilderIgnoringMapper.INSTANCE.mapAll( source );
80+
81+
assertThat( target.getId() ).isEqualTo( 100L );
82+
assertThat( target.getName() ).isEqualTo( "John" );
83+
assertThat( target.getLastName() ).isEqualTo( "Doe" );
84+
}
85+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.ap.test.builder.ignore;
20+
21+
/**
22+
* @author Filip Hrisafov
23+
*/
24+
public class Person extends BaseEntity {
25+
26+
private final String name;
27+
private final String lastName;
28+
29+
public Person(Builder builder) {
30+
super( builder );
31+
this.name = builder.name;
32+
this.lastName = builder.lastName;
33+
}
34+
35+
public String getName() {
36+
return name;
37+
}
38+
39+
public String getLastName() {
40+
return lastName;
41+
}
42+
43+
public static Builder builder() {
44+
return new Builder();
45+
46+
}
47+
48+
public static class Builder extends BaseEntity.Builder {
49+
private String name;
50+
private String lastName;
51+
52+
public Builder name(String name) {
53+
this.name = name;
54+
return this;
55+
}
56+
57+
public Builder lastName(String lastName) {
58+
this.lastName = lastName;
59+
return this;
60+
}
61+
62+
public Person create() {
63+
return new Person( this );
64+
}
65+
}
66+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.ap.test.builder.ignore;
20+
21+
/**
22+
* @author Filip Hrisafov
23+
*/
24+
public class PersonDto extends BaseDto {
25+
26+
private String name;
27+
private String lastName;
28+
29+
public String getName() {
30+
return name;
31+
}
32+
33+
public void setName(String name) {
34+
this.name = name;
35+
}
36+
37+
public String getLastName() {
38+
return lastName;
39+
}
40+
41+
public void setLastName(String lastName) {
42+
this.lastName = lastName;
43+
}
44+
}

0 commit comments

Comments
 (0)