Ignoring unmapped properties with MapStruct (#7809)
This commit is contained in:
parent
39e99cd907
commit
b86270a6aa
@ -35,6 +35,12 @@
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${org.projectlombok.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@ -70,6 +76,7 @@
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<org.projectlombok.version>1.18.4</org.projectlombok.version>
|
||||
<assertj.version>3.11.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
@ -0,0 +1,23 @@
|
||||
package com.baeldung.unmappedproperties.dto;
|
||||
|
||||
public class CarDTO {
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package com.baeldung.unmappedproperties.dto;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class DocumentDTO {
|
||||
private int id;
|
||||
private String title;
|
||||
private String text;
|
||||
private List<String> comments;
|
||||
private String author;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public List<String> getComments() {
|
||||
return comments;
|
||||
}
|
||||
|
||||
public void setComments(List<String> comments) {
|
||||
this.comments = comments;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.baeldung.unmappedproperties.entity;
|
||||
|
||||
public class Car {
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package com.baeldung.unmappedproperties.entity;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Document {
|
||||
private int id;
|
||||
private String title;
|
||||
private String text;
|
||||
private Date modificationTime;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public Date getModificationTime() {
|
||||
return modificationTime;
|
||||
}
|
||||
|
||||
public void setModificationTime(Date modificationTime) {
|
||||
this.modificationTime = modificationTime;
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.baeldung.unmappedproperties.mapper;
|
||||
|
||||
import com.baeldung.unmappedproperties.dto.CarDTO;
|
||||
import com.baeldung.unmappedproperties.entity.Car;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@Mapper
|
||||
public interface CarMapper {
|
||||
CarMapper INSTANCE = Mappers.getMapper(CarMapper.class);
|
||||
|
||||
CarDTO carToCarDTO(Car car);
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.baeldung.unmappedproperties.mapper;
|
||||
|
||||
import com.baeldung.unmappedproperties.dto.DocumentDTO;
|
||||
import com.baeldung.unmappedproperties.entity.Document;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@Mapper
|
||||
public interface DocumentMapper {
|
||||
|
||||
DocumentMapper INSTANCE = Mappers.getMapper(DocumentMapper.class);
|
||||
|
||||
DocumentDTO documentToDocumentDTO(Document entity);
|
||||
|
||||
Document documentDTOToDocument(DocumentDTO dto);
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.baeldung.unmappedproperties.mapper;
|
||||
|
||||
import com.baeldung.unmappedproperties.dto.DocumentDTO;
|
||||
import com.baeldung.unmappedproperties.entity.Document;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@Mapper
|
||||
public interface DocumentMapperMappingIgnore {
|
||||
|
||||
DocumentMapperMappingIgnore INSTANCE = Mappers.getMapper(DocumentMapperMappingIgnore.class);
|
||||
|
||||
@Mapping(target = "comments", ignore = true)
|
||||
DocumentDTO documentToDocumentDTO(Document entity);
|
||||
|
||||
@Mapping(target = "modificationTime", ignore = true)
|
||||
Document documentDTOToDocument(DocumentDTO dto);
|
||||
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.baeldung.unmappedproperties.mapper;
|
||||
|
||||
import com.baeldung.unmappedproperties.dto.DocumentDTO;
|
||||
import com.baeldung.unmappedproperties.entity.Document;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.ReportingPolicy;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
|
||||
public interface DocumentMapperUnmappedPolicy {
|
||||
|
||||
DocumentMapperUnmappedPolicy INSTANCE = Mappers.getMapper(DocumentMapperUnmappedPolicy.class);
|
||||
|
||||
DocumentDTO documentToDocumentDTO(Document entity);
|
||||
|
||||
Document documentDTOToDocument(DocumentDTO dto);
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.baeldung.unmappedproperties.mapper;
|
||||
|
||||
import com.baeldung.unmappedproperties.dto.DocumentDTO;
|
||||
import com.baeldung.unmappedproperties.entity.Document;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
@Mapper(config = IgnoreUnmappedMapperConfig.class)
|
||||
public interface DocumentMapperWithConfig {
|
||||
|
||||
DocumentMapperWithConfig INSTANCE = Mappers.getMapper(DocumentMapperWithConfig.class);
|
||||
|
||||
DocumentDTO documentToDocumentDTO(Document entity);
|
||||
|
||||
Document documentDTOToDocument(DocumentDTO dto);
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.baeldung.unmappedproperties.mapper;
|
||||
|
||||
import org.mapstruct.MapperConfig;
|
||||
import org.mapstruct.ReportingPolicy;
|
||||
|
||||
@MapperConfig(unmappedTargetPolicy = ReportingPolicy.IGNORE)
|
||||
public interface IgnoreUnmappedMapperConfig {
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.baeldung.mapper.unmappedproperties;
|
||||
|
||||
import com.baeldung.unmappedproperties.dto.CarDTO;
|
||||
import com.baeldung.unmappedproperties.entity.Car;
|
||||
import com.baeldung.unmappedproperties.mapper.CarMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class CarMapperUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenCarEntitytoCar_whenMaps_thenCorrect() {
|
||||
Car entity = new Car();
|
||||
entity.setId(1);
|
||||
entity.setName("Toyota");
|
||||
|
||||
CarDTO carDto = CarMapper.INSTANCE.carToCarDTO(entity);
|
||||
|
||||
assertThat(carDto.getId()).isEqualTo(entity.getId());
|
||||
assertThat(carDto.getName()).isEqualTo(entity.getName());
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package com.baeldung.mapper.unmappedproperties;
|
||||
|
||||
import com.baeldung.unmappedproperties.dto.DocumentDTO;
|
||||
import com.baeldung.unmappedproperties.entity.Document;
|
||||
import com.baeldung.unmappedproperties.mapper.DocumentMapperMappingIgnore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DocumentMapperMappingIgnoreUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDocumentEntityToDocumentDto_whenMaps_thenCorrect() {
|
||||
Document entity = new Document();
|
||||
entity.setId(1);
|
||||
entity.setTitle("Price 13-42");
|
||||
entity.setText("List of positions.......");
|
||||
entity.setModificationTime(new Date());
|
||||
|
||||
DocumentDTO dto = DocumentMapperMappingIgnore.INSTANCE.documentToDocumentDTO(entity);
|
||||
|
||||
assertThat(dto.getId()).isEqualTo(entity.getId());
|
||||
assertThat(dto.getTitle()).isEqualTo(entity.getTitle());
|
||||
assertThat(dto.getText()).isEqualTo(entity.getText());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDocumentDtoToDocumentEntity_whenMaps_thenCorrect() {
|
||||
DocumentDTO dto = new DocumentDTO();
|
||||
dto.setId(1);
|
||||
dto.setTitle("Price 13-42");
|
||||
dto.setText("List of positions.......");
|
||||
dto.setComments(Arrays.asList("Not all positions", "Wrong price values"));
|
||||
dto.setAuthor("Author1");
|
||||
|
||||
Document entity = DocumentMapperMappingIgnore.INSTANCE.documentDTOToDocument(dto);
|
||||
|
||||
assertThat(entity.getId()).isEqualTo(dto.getId());
|
||||
assertThat(entity.getTitle()).isEqualTo(dto.getTitle());
|
||||
assertThat(entity.getText()).isEqualTo(dto.getText());
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package com.baeldung.mapper.unmappedproperties;
|
||||
|
||||
import com.baeldung.unmappedproperties.dto.DocumentDTO;
|
||||
import com.baeldung.unmappedproperties.entity.Document;
|
||||
import com.baeldung.unmappedproperties.mapper.DocumentMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DocumentMapperUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDocumentEntityToDocumentDto_whenMaps_thenCorrect() {
|
||||
Document entity = new Document();
|
||||
entity.setId(1);
|
||||
entity.setTitle("Price 13-42");
|
||||
entity.setText("List of positions.......");
|
||||
entity.setModificationTime(new Date());
|
||||
|
||||
DocumentDTO dto = DocumentMapper.INSTANCE.documentToDocumentDTO(entity);
|
||||
|
||||
assertThat(dto.getId()).isEqualTo(entity.getId());
|
||||
assertThat(dto.getTitle()).isEqualTo(entity.getTitle());
|
||||
assertThat(dto.getText()).isEqualTo(entity.getText());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDocumentDtoToDocumentEntity_whenMaps_thenCorrect() {
|
||||
DocumentDTO dto = new DocumentDTO();
|
||||
dto.setId(1);
|
||||
dto.setTitle("Price 13-42");
|
||||
dto.setText("List of positions.......");
|
||||
dto.setComments(Arrays.asList("Not all positions", "Wrong price values"));
|
||||
dto.setAuthor("Author1");
|
||||
|
||||
Document entity = DocumentMapper.INSTANCE.documentDTOToDocument(dto);
|
||||
|
||||
assertThat(entity.getId()).isEqualTo(dto.getId());
|
||||
assertThat(entity.getTitle()).isEqualTo(dto.getTitle());
|
||||
assertThat(entity.getText()).isEqualTo(dto.getText());
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package com.baeldung.mapper.unmappedproperties;
|
||||
|
||||
import com.baeldung.unmappedproperties.dto.DocumentDTO;
|
||||
import com.baeldung.unmappedproperties.entity.Document;
|
||||
import com.baeldung.unmappedproperties.mapper.DocumentMapperUnmappedPolicy;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DocumentMapperUnmappedPolicyUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDocumentEntityToDocumentDto_whenMaps_thenCorrect() {
|
||||
Document entity = new Document();
|
||||
entity.setId(1);
|
||||
entity.setTitle("Price 13-42");
|
||||
entity.setText("List of positions.......");
|
||||
entity.setModificationTime(new Date());
|
||||
|
||||
DocumentDTO dto = DocumentMapperUnmappedPolicy.INSTANCE.documentToDocumentDTO(entity);
|
||||
|
||||
assertThat(dto.getId()).isEqualTo(entity.getId());
|
||||
assertThat(dto.getTitle()).isEqualTo(entity.getTitle());
|
||||
assertThat(dto.getText()).isEqualTo(entity.getText());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDocumentDtoToDocumentEntity_whenMaps_thenCorrect() {
|
||||
DocumentDTO dto = new DocumentDTO();
|
||||
dto.setId(1);
|
||||
dto.setTitle("Price 13-42");
|
||||
dto.setText("List of positions.......");
|
||||
dto.setComments(Arrays.asList("Not all positions", "Wrong price values"));
|
||||
dto.setAuthor("Author1");
|
||||
|
||||
Document entity = DocumentMapperUnmappedPolicy.INSTANCE.documentDTOToDocument(dto);
|
||||
|
||||
assertThat(entity.getId()).isEqualTo(dto.getId());
|
||||
assertThat(entity.getTitle()).isEqualTo(dto.getTitle());
|
||||
assertThat(entity.getText()).isEqualTo(dto.getText());
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package com.baeldung.mapper.unmappedproperties;
|
||||
|
||||
import com.baeldung.unmappedproperties.dto.DocumentDTO;
|
||||
import com.baeldung.unmappedproperties.entity.Document;
|
||||
import com.baeldung.unmappedproperties.mapper.DocumentMapperWithConfig;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DocumentMapperWithConfigUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDocumentEntityToDocumentDto_whenMaps_thenCorrect() {
|
||||
Document entity = new Document();
|
||||
entity.setId(1);
|
||||
entity.setTitle("Price 13-42");
|
||||
entity.setText("List of positions.......");
|
||||
entity.setModificationTime(new Date());
|
||||
|
||||
DocumentDTO dto = DocumentMapperWithConfig.INSTANCE.documentToDocumentDTO(entity);
|
||||
|
||||
assertThat(dto.getId()).isEqualTo(entity.getId());
|
||||
assertThat(dto.getTitle()).isEqualTo(entity.getTitle());
|
||||
assertThat(dto.getText()).isEqualTo(entity.getText());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDocumentDtoToDocumentEntity_whenMaps_thenCorrect() {
|
||||
DocumentDTO dto = new DocumentDTO();
|
||||
dto.setId(1);
|
||||
dto.setTitle("Price 13-42");
|
||||
dto.setText("List of positions.......");
|
||||
dto.setComments(Arrays.asList("Not all positions", "Wrong price values"));
|
||||
dto.setAuthor("Author1");
|
||||
|
||||
Document entity = DocumentMapperWithConfig.INSTANCE.documentDTOToDocument(dto);
|
||||
|
||||
assertThat(entity.getId()).isEqualTo(dto.getId());
|
||||
assertThat(entity.getTitle()).isEqualTo(dto.getTitle());
|
||||
assertThat(entity.getText()).isEqualTo(dto.getText());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user