From b4c7dd5713dbaaa12742d785f1bab28e7707e186 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Bertholino?= <138716903+joaobertholino@users.noreply.github.com> Date: Wed, 20 Nov 2024 04:23:10 -0300 Subject: [PATCH] fix: Correcting class mapping annotations with MapStruct, correcting warnings present for the failure to map some properties. (#176) --- .../samples/petclinic/mapper/OwnerMapper.java | 5 ++++- .../samples/petclinic/mapper/PetMapper.java | 9 ++++++--- .../samples/petclinic/mapper/PetTypeMapper.java | 4 +++- .../samples/petclinic/mapper/UserMapper.java | 8 ++++++-- .../samples/petclinic/mapper/VetMapper.java | 4 +++- .../samples/petclinic/mapper/VisitMapper.java | 4 +++- 6 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/springframework/samples/petclinic/mapper/OwnerMapper.java b/src/main/java/org/springframework/samples/petclinic/mapper/OwnerMapper.java index 04e679c26..5acb8a19c 100755 --- a/src/main/java/org/springframework/samples/petclinic/mapper/OwnerMapper.java +++ b/src/main/java/org/springframework/samples/petclinic/mapper/OwnerMapper.java @@ -1,8 +1,9 @@ package org.springframework.samples.petclinic.mapper; import org.mapstruct.Mapper; -import org.springframework.samples.petclinic.rest.dto.OwnerDto; +import org.mapstruct.Mapping; import org.springframework.samples.petclinic.model.Owner; +import org.springframework.samples.petclinic.rest.dto.OwnerDto; import org.springframework.samples.petclinic.rest.dto.OwnerFieldsDto; import java.util.Collection; @@ -18,6 +19,8 @@ public interface OwnerMapper { Owner toOwner(OwnerDto ownerDto); + @Mapping(target = "id", ignore = true) + @Mapping(target = "pets", ignore = true) Owner toOwner(OwnerFieldsDto ownerDto); List toOwnerDtoCollection(Collection ownerCollection); diff --git a/src/main/java/org/springframework/samples/petclinic/mapper/PetMapper.java b/src/main/java/org/springframework/samples/petclinic/mapper/PetMapper.java index 1270af8d3..ae161009c 100755 --- a/src/main/java/org/springframework/samples/petclinic/mapper/PetMapper.java +++ b/src/main/java/org/springframework/samples/petclinic/mapper/PetMapper.java @@ -2,18 +2,18 @@ import org.mapstruct.Mapper; import org.mapstruct.Mapping; +import org.springframework.samples.petclinic.model.Pet; +import org.springframework.samples.petclinic.model.PetType; import org.springframework.samples.petclinic.rest.dto.PetDto; import org.springframework.samples.petclinic.rest.dto.PetFieldsDto; import org.springframework.samples.petclinic.rest.dto.PetTypeDto; -import org.springframework.samples.petclinic.model.Pet; -import org.springframework.samples.petclinic.model.PetType; import java.util.Collection; /** * Map Pet & PetDto using mapstruct */ -@Mapper +@Mapper(uses = VisitMapper.class) public interface PetMapper { @Mapping(source = "owner.id", target = "ownerId") @@ -26,6 +26,9 @@ public interface PetMapper { @Mapping(source = "ownerId", target = "owner.id") Pet toPet(PetDto petDto); + @Mapping(target = "id", ignore = true) + @Mapping(target = "owner", ignore = true) + @Mapping(target = "visits", ignore = true) Pet toPet(PetFieldsDto petFieldsDto); PetTypeDto toPetTypeDto(PetType petType); diff --git a/src/main/java/org/springframework/samples/petclinic/mapper/PetTypeMapper.java b/src/main/java/org/springframework/samples/petclinic/mapper/PetTypeMapper.java index 05617161a..efb6d60bf 100644 --- a/src/main/java/org/springframework/samples/petclinic/mapper/PetTypeMapper.java +++ b/src/main/java/org/springframework/samples/petclinic/mapper/PetTypeMapper.java @@ -1,8 +1,9 @@ package org.springframework.samples.petclinic.mapper; import org.mapstruct.Mapper; -import org.springframework.samples.petclinic.rest.dto.PetTypeDto; +import org.mapstruct.Mapping; import org.springframework.samples.petclinic.model.PetType; +import org.springframework.samples.petclinic.rest.dto.PetTypeDto; import org.springframework.samples.petclinic.rest.dto.PetTypeFieldsDto; import java.util.Collection; @@ -16,6 +17,7 @@ public interface PetTypeMapper { PetType toPetType(PetTypeDto petTypeDto); + @Mapping(target = "id", ignore = true) PetType toPetType(PetTypeFieldsDto petTypeFieldsDto); PetTypeDto toPetTypeDto(PetType petType); diff --git a/src/main/java/org/springframework/samples/petclinic/mapper/UserMapper.java b/src/main/java/org/springframework/samples/petclinic/mapper/UserMapper.java index 718e5410f..2b5aea0fc 100644 --- a/src/main/java/org/springframework/samples/petclinic/mapper/UserMapper.java +++ b/src/main/java/org/springframework/samples/petclinic/mapper/UserMapper.java @@ -1,10 +1,11 @@ package org.springframework.samples.petclinic.mapper; import org.mapstruct.Mapper; -import org.springframework.samples.petclinic.rest.dto.RoleDto; -import org.springframework.samples.petclinic.rest.dto.UserDto; +import org.mapstruct.Mapping; import org.springframework.samples.petclinic.model.Role; import org.springframework.samples.petclinic.model.User; +import org.springframework.samples.petclinic.rest.dto.RoleDto; +import org.springframework.samples.petclinic.rest.dto.UserDto; import java.util.Collection; @@ -13,6 +14,9 @@ */ @Mapper public interface UserMapper { + + @Mapping(target = "id", ignore = true) + @Mapping(target = "user", ignore = true) Role toRole(RoleDto roleDto); RoleDto toRoleDto(Role role); diff --git a/src/main/java/org/springframework/samples/petclinic/mapper/VetMapper.java b/src/main/java/org/springframework/samples/petclinic/mapper/VetMapper.java index 3c25e5142..fdcab6cd1 100644 --- a/src/main/java/org/springframework/samples/petclinic/mapper/VetMapper.java +++ b/src/main/java/org/springframework/samples/petclinic/mapper/VetMapper.java @@ -1,8 +1,9 @@ package org.springframework.samples.petclinic.mapper; import org.mapstruct.Mapper; -import org.springframework.samples.petclinic.rest.dto.VetDto; +import org.mapstruct.Mapping; import org.springframework.samples.petclinic.model.Vet; +import org.springframework.samples.petclinic.rest.dto.VetDto; import org.springframework.samples.petclinic.rest.dto.VetFieldsDto; import java.util.Collection; @@ -14,6 +15,7 @@ public interface VetMapper { Vet toVet(VetDto vetDto); + @Mapping(target = "id", ignore = true) Vet toVet(VetFieldsDto vetFieldsDto); VetDto toVetDto(Vet vet); diff --git a/src/main/java/org/springframework/samples/petclinic/mapper/VisitMapper.java b/src/main/java/org/springframework/samples/petclinic/mapper/VisitMapper.java index 7960c6d20..98dc2db97 100644 --- a/src/main/java/org/springframework/samples/petclinic/mapper/VisitMapper.java +++ b/src/main/java/org/springframework/samples/petclinic/mapper/VisitMapper.java @@ -2,8 +2,8 @@ import org.mapstruct.Mapper; import org.mapstruct.Mapping; -import org.springframework.samples.petclinic.rest.dto.VisitDto; import org.springframework.samples.petclinic.model.Visit; +import org.springframework.samples.petclinic.rest.dto.VisitDto; import org.springframework.samples.petclinic.rest.dto.VisitFieldsDto; import java.util.Collection; @@ -16,6 +16,8 @@ public interface VisitMapper { @Mapping(source = "petId", target = "pet.id") Visit toVisit(VisitDto visitDto); + @Mapping(target = "id", ignore = true) + @Mapping(target = "pet", ignore = true) Visit toVisit(VisitFieldsDto visitFieldsDto); @Mapping(source = "pet.id", target = "petId")