Skip to content

Commit 2001c19

Browse files
authored
partial suppress warnings on unmapped targets (for update methods)
1 parent 0919158 commit 2001c19

9 files changed

Lines changed: 378 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Currently, the following examples exist:
1919
* _mapstruct-kotlin_: Example on how to use MapStruct with Kotlin using KAPT (Kotlin Annotation Processing Tool)
2020
* _mapstruct-kotlin-gradle_: Example on how to use MapStruct with Kotlin and Gradle Kotlin DSL using KAPT
2121
* _mapstruct-jpa-parent-child_: Example on how to use @Context in relation to parent / child relations in JPA)
22+
* _mapstruct-suppress-unmapped_: Shows how mapping to target properties can be ignored without warning by default in a mixed scenario. However bean property mappings that have the same name will still be applied.
2223
* _mapstruct-lookup-entity-with-composed-key_: Shows how an object with composite key can be read from the database in a mapping method.
2324

2425
## License
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
5+
and/or other contributors as indicated by the @authors tag. See the
6+
copyright.txt file in the distribution for a full listing of all
7+
contributors.
8+
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at
12+
13+
http://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
21+
-->
22+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
23+
<modelVersion>4.0.0</modelVersion>
24+
<groupId>org.mapstruct.examples.suppress</groupId>
25+
<artifactId>mapstruct-suppress-unmapped</artifactId>
26+
<version>1.0-SNAPSHOT</version>
27+
28+
<properties>
29+
<org.mapstruct.version>1.3.0.Beta1</org.mapstruct.version>
30+
<maven.compiler.source>1.8</maven.compiler.source>
31+
<maven.compiler.target>1.8</maven.compiler.target>
32+
</properties>
33+
34+
<dependencies>
35+
36+
<dependency>
37+
<groupId>org.mapstruct</groupId>
38+
<artifactId>mapstruct</artifactId>
39+
<version>${org.mapstruct.version}</version>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>org.mapstruct</groupId>
44+
<artifactId>mapstruct-processor</artifactId>
45+
<version>${org.mapstruct.version}</version>
46+
<scope>provided</scope>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>junit</groupId>
51+
<artifactId>junit</artifactId>
52+
<version>4.12</version>
53+
<scope>test</scope>
54+
</dependency>
55+
56+
<dependency>
57+
<groupId>org.assertj</groupId>
58+
<artifactId>assertj-core</artifactId>
59+
<version>3.9.0</version>
60+
<scope>test</scope>
61+
</dependency>
62+
</dependencies>
63+
64+
65+
<build>
66+
<plugins>
67+
<plugin>
68+
<groupId>org.apache.maven.plugins</groupId>
69+
<artifactId>maven-compiler-plugin</artifactId>
70+
<version>3.6.2</version>
71+
<configuration>
72+
<annotationProcessorPaths>
73+
<path>
74+
<groupId>org.mapstruct</groupId>
75+
<artifactId>mapstruct-processor</artifactId>
76+
<version>${org.mapstruct.version}</version>
77+
</path>
78+
</annotationProcessorPaths>
79+
</configuration>
80+
</plugin>
81+
</plugins>
82+
</build>
83+
84+
</project>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.jpa;
20+
21+
/**
22+
*
23+
* @author Sjaak Derksen
24+
*/
25+
public class ChildDto {
26+
27+
private String name;
28+
private int age;
29+
30+
public String getName() {
31+
return name;
32+
}
33+
34+
public void setName(String name) {
35+
this.name = name;
36+
}
37+
38+
public int getAge() {
39+
return age;
40+
}
41+
42+
public void setAge(int age) {
43+
this.age = age;
44+
}
45+
46+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.jpa;
20+
21+
/**
22+
*
23+
* @author Sjaak Derksen
24+
*/
25+
public class ChildEntity {
26+
27+
private String name;
28+
private Integer childAge;
29+
30+
public String getName() {
31+
return name;
32+
}
33+
34+
public void setName(String name) {
35+
this.name = name;
36+
}
37+
38+
public Integer getChildAge() {
39+
return childAge;
40+
}
41+
42+
public void setChildAge(Integer childAge) {
43+
this.childAge = childAge;
44+
}
45+
46+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.jpa;
20+
21+
import java.util.List;
22+
23+
/**
24+
*
25+
* @author Sjaak Derksen
26+
*/
27+
public class ParentDto {
28+
29+
private String name;
30+
private List<ChildDto> children;
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
40+
public List<ChildDto> getChildren() {
41+
return children;
42+
}
43+
44+
public void setChildren(List<ChildDto> children) {
45+
this.children = children;
46+
}
47+
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.jpa;
20+
21+
import java.util.List;
22+
23+
/**
24+
*
25+
* @author Sjaak Derksen
26+
*/
27+
public class ParentEntity {
28+
29+
private String name;
30+
private List<ChildEntity> children;
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
40+
public List<ChildEntity> getChildren() {
41+
return children;
42+
}
43+
44+
public void setChildren(List<ChildEntity> children) {
45+
this.children = children;
46+
}
47+
48+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.jpa;
20+
21+
import org.mapstruct.Mapper;
22+
import org.mapstruct.MappingTarget;
23+
import org.mapstruct.ReportingPolicy;
24+
import org.mapstruct.factory.Mappers;
25+
26+
@Mapper( uses = SourceTargetMapper.LenientMapper.class )
27+
public interface SourceTargetMapper {
28+
29+
SourceTargetMapper MAPPER = Mappers.getMapper( SourceTargetMapper.class );
30+
31+
void toEntity(ParentDto s, @MappingTarget ParentEntity t);
32+
33+
@Mapper( unmappedTargetPolicy = ReportingPolicy.IGNORE )
34+
public interface LenientMapper {
35+
36+
// ChildDto.name will be mapped to ChildEntity.name (based on name), explicit ignore would still be required
37+
// ChildDto.age will not be mapped ChildDto.childAge (ignore) and there's no warning either
38+
// this is handy when @BeanMapping#ignoreByDefault causes too many manual mappings to add
39+
void toEntity(ChildDto s, @MappingTarget ChildEntity t);
40+
}
41+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.jpa;
20+
21+
import java.util.Arrays;
22+
import org.junit.Test;
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
/**
26+
*
27+
* @author Sjaak Derksen
28+
*/
29+
public class SourceTargetMapperTest {
30+
31+
public SourceTargetMapperTest() {
32+
}
33+
34+
/**
35+
* Test of toTarget method, of class SourceTargetMapper.
36+
*/
37+
@Test
38+
public void testToTarget() {
39+
40+
// prepare dto's
41+
ParentDto parent = new ParentDto();
42+
parent.setName( "jim" );
43+
ChildDto childDto1 = new ChildDto();
44+
childDto1.setName( "jack" );
45+
ChildDto childDto2 = new ChildDto();
46+
childDto2.setName( "jill" );
47+
parent.setChildren( Arrays.asList( childDto1, childDto2 ) );
48+
49+
50+
ParentEntity parentEntity = new ParentEntity();
51+
SourceTargetMapper.MAPPER.toEntity( parent, parentEntity );
52+
53+
//results
54+
assertThat( parentEntity ).isNotNull();
55+
assertThat( parentEntity.getName() ).isEqualTo( "jim" );
56+
assertThat( parentEntity.getChildren() ).hasSize( 2 );
57+
assertThat( parentEntity.getChildren().get( 0 ).getName() ).isEqualTo( "jack" );
58+
assertThat( parentEntity.getChildren().get( 0 ).getChildAge() ).isNull();
59+
assertThat( parentEntity.getChildren().get( 1 ).getName() ).isEqualTo( "jill" );
60+
assertThat( parentEntity.getChildren().get( 1 ).getChildAge() ).isNull();
61+
}
62+
63+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@
4141
<module>mapstruct-kotlin</module>
4242
<module>mapstruct-jpa-child-parent</module>
4343
<module>mapstruct-lookup-entity-with-id</module>
44+
<module>mapstruct-suppress-unmapped</module>
4445
</modules>
4546
</project>

0 commit comments

Comments
 (0)