From 087d59b2383c2cd6a732509183b1834636be8941 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Wed, 25 Sep 2024 14:13:03 +0200 Subject: [PATCH] intoduce Mapper.withMatchingStrategy --- .../hs/booking/item/HsBookingItemController.java | 4 +++- .../net/hostsharing/hsadminng/mapper/Mapper.java | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemController.java b/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemController.java index f28d418d..e8e01b87 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/booking/item/HsBookingItemController.java @@ -23,6 +23,7 @@ import java.util.UUID; import java.util.function.BiConsumer; import static net.hostsharing.hsadminng.mapper.PostgresDateRange.toPostgresDateRange; +import static org.modelmapper.convention.MatchingStrategies.STRICT; @RestController public class HsBookingItemController implements HsBookingItemsApi { @@ -62,7 +63,8 @@ public class HsBookingItemController implements HsBookingItemsApi { context.define(currentSubject, assumedRoles); - final var entityToSave = mapper.map(body, HsBookingItemRbacEntity.class, RESOURCE_TO_ENTITY_POSTMAPPER); + final var entityToSave = mapper.withMatchingStrategy(STRICT) + .map(body, HsBookingItemRbacEntity.class, RESOURCE_TO_ENTITY_POSTMAPPER); final var mapped = new BookingItemEntitySaveProcessor(em, entityToSave) .preprocessEntity() .validateEntity() diff --git a/src/main/java/net/hostsharing/hsadminng/mapper/Mapper.java b/src/main/java/net/hostsharing/hsadminng/mapper/Mapper.java index 91dc725d..a8355aaf 100644 --- a/src/main/java/net/hostsharing/hsadminng/mapper/Mapper.java +++ b/src/main/java/net/hostsharing/hsadminng/mapper/Mapper.java @@ -1,6 +1,7 @@ package net.hostsharing.hsadminng.mapper; import org.modelmapper.ModelMapper; +import org.modelmapper.spi.MatchingStrategy; import org.springframework.util.ReflectionUtils; import jakarta.persistence.EntityManager; @@ -15,7 +16,6 @@ import java.util.stream.Stream; import static java.util.Arrays.stream; import static net.hostsharing.hsadminng.errors.DisplayAs.DisplayName; -import static org.modelmapper.convention.MatchingStrategies.STRICT; /** * A nicer API for ModelMapper. @@ -26,8 +26,17 @@ public class Mapper extends ModelMapper { EntityManager em; public Mapper() { - // make sure that resource.whateverUuid does not get mapped to entity.uuid, if resource.uuid does not exist - getConfiguration().setMatchingStrategy(STRICT); + getConfiguration().setAmbiguityIgnored(true); + } + + /** Use STRICT to make sure that resource.whateverUuid does not accidentally get mapped to entity.uuid, + * if resource.uuid does not exist + * + * @return this + */ + public Mapper withMatchingStrategy(final MatchingStrategy matchingStrategy) { + getConfiguration().setMatchingStrategy(matchingStrategy); + return this; } public List mapList(final List source, final Class targetClass) {