intoduce Mapper.withMatchingStrategy

This commit is contained in:
Michael Hoennig 2024-09-25 14:13:03 +02:00
parent 6225eb3bf6
commit 087d59b238
2 changed files with 15 additions and 4 deletions

View File

@ -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()

View File

@ -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 <S, T> List<T> mapList(final List<S> source, final Class<T> targetClass) {