Skip to content

Commit

Permalink
fix: Correcting class mapping annotations with MapStruct, correcting …
Browse files Browse the repository at this point in the history
…warnings present for the failure to map some properties. (#176)
  • Loading branch information
joaobertholino authored Nov 20, 2024
1 parent e2e11ec commit b4c7dd5
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<OwnerDto> toOwnerDtoCollection(Collection<Owner> ownerCollection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,6 +17,7 @@ public interface PetTypeMapper {

PetType toPetType(PetTypeDto petTypeDto);

@Mapping(target = "id", ignore = true)
PetType toPetType(PetTypeFieldsDto petTypeFieldsDto);

PetTypeDto toPetTypeDto(PetType petType);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,6 +15,7 @@
public interface VetMapper {
Vet toVet(VetDto vetDto);

@Mapping(target = "id", ignore = true)
Vet toVet(VetFieldsDto vetFieldsDto);

VetDto toVetDto(Vet vet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand Down

0 comments on commit b4c7dd5

Please sign in to comment.