add namePrefix to mapper error message

This commit is contained in:
Michael Hoennig 2024-09-25 17:13:56 +02:00
parent 8ebbf20b93
commit 0701d8a629
3 changed files with 22 additions and 7 deletions

View File

@ -84,7 +84,7 @@ public class HsOfficeDebitorController implements HsOfficeDebitorsApi {
final var entityToSave = mapper.map(body, HsOfficeDebitorEntity.class);
if ( body.getDebitorRel() != null ) {
body.getDebitorRel().setType(DEBITOR.name());
final var debitorRel = mapper.map(body.getDebitorRel(), HsOfficeRelationRealEntity.class);
final var debitorRel = mapper.map("debitorRel.", body.getDebitorRel(), HsOfficeRelationRealEntity.class);
validateEntityExists("debitorRel.anchorUuid", debitorRel.getAnchor());
validateEntityExists("debitorRel.holderUuid", debitorRel.getHolder());
validateEntityExists("debitorRel.contactUuid", debitorRel.getContact());

View File

@ -47,6 +47,10 @@ abstract class Mapper extends ModelMapper {
@Override
public <D> D map(final Object source, final Class<D> destinationType) {
return map("", source, destinationType);
}
public <D> D map(final String namePrefix, final Object source, final Class<D> destinationType) {
final var target = super.map(source, destinationType);
for (Field f : getDeclaredFieldsIncludingSuperClasses(destinationType)) {
if (f.getAnnotation(ManyToOne.class) == null) {
@ -66,7 +70,7 @@ abstract class Mapper extends ModelMapper {
if (subEntityUuid == null) {
continue;
}
ReflectionUtils.setField(f, target, fetchEntity(f.getType(), subEntityUuid));
ReflectionUtils.setField(f, target, fetchEntity(namePrefix + f.getName() + ".uuid", f.getType(), subEntityUuid));
}
return target;
}
@ -82,13 +86,15 @@ abstract class Mapper extends ModelMapper {
.toArray(Field[]::new);
}
public <E> E fetchEntity(final Class<E> entityClass, final Object subEntityUuid) {
// using getReference would be more efficent, but results in very technical error messages
public <E> E fetchEntity(final String propertyName, final Class<E> entityClass, final Object subEntityUuid) {
// using getReference would be more efficient, but results in very technical error messages
final var entity = em.find(entityClass, subEntityUuid); // FIXME: try getReference
if (entity != null) {
return entity;
}
throw new ValidationException("Unable to find " + DisplayName.of(entityClass) + " by uuid: " + subEntityUuid);
throw new ValidationException(
"Unable to find " + DisplayName.of(entityClass) +
" by " + propertyName + ": " + subEntityUuid);
}
public <S, T> T map(final S source, final Class<T> targetClass, final BiConsumer<S, T> postMapper) {
@ -99,4 +105,13 @@ abstract class Mapper extends ModelMapper {
postMapper.accept(source, target);
return target;
}
public <S, T> T map(final String namePrefix, final S source, final Class<T> targetClass, final BiConsumer<S, T> postMapper) {
if (source == null) {
return null;
}
final var target = map(source, targetClass);
postMapper.accept(source, target);
return target;
}
}

View File

@ -405,7 +405,7 @@ class HsOfficeDebitorControllerAcceptanceTest extends ContextBasedTestWithCleanu
.post("http://localhost/api/hs/office/debitors")
.then().log().all().assertThat()
.statusCode(400)
.body("message", is("ERROR: [400] Unable to find RealContact by debitorRel.contactUuid: 00000000-0000-0000-0000-000000000000"));
.body("message", is("ERROR: [400] Unable to find RealContact by debitorRel.contact.uuid: 00000000-0000-0000-0000-000000000000"));
// @formatter:on
}
@ -433,7 +433,7 @@ class HsOfficeDebitorControllerAcceptanceTest extends ContextBasedTestWithCleanu
.post("http://localhost/api/hs/office/debitors")
.then().log().all().assertThat()
.statusCode(400)
.body("message", is("ERROR: [400] Unable to find RealRelation by uuid: 00000000-0000-0000-0000-000000000000"));
.body("message", is("ERROR: [400] Unable to find RealRelation by debitorRel.uuid: 00000000-0000-0000-0000-000000000000"));
// @formatter:on
}
}