Properly handle associations in nested entities.

Nested entities that contain a reference to an aggregate root get a link to that attached to their representation. Previously, the creation of those links assumed that the reference is a materialized instance of the remote aggregate. That's now altered to be able to deal with associations, use identifiers directly or materialize to an intermediate aggregate instance to potentially use a custom lookup.
This commit is contained in:
Oliver Drotbohm
2021-04-07 16:46:31 +02:00
parent ba2a34152d
commit 1ae7efd0b8
11 changed files with 98 additions and 36 deletions

View File

@@ -25,6 +25,8 @@ import java.util.Collections;
import org.junit.Test;
import org.mockito.internal.stubbing.answers.ReturnsArgumentAt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.mapping.context.PersistentEntities;
import org.springframework.data.rest.core.support.DefaultSelfLinkProvider;
import org.springframework.data.rest.tests.AbstractControllerIntegrationTests;
@@ -33,9 +35,9 @@ import org.springframework.data.rest.tests.mongodb.MongoDbRepositoryConfig;
import org.springframework.data.rest.tests.mongodb.User;
import org.springframework.data.rest.webmvc.mapping.Associations;
import org.springframework.data.rest.webmvc.support.Projector;
import org.springframework.hateoas.server.EntityLinks;
import org.springframework.hateoas.IanaLinkRelations;
import org.springframework.hateoas.Links;
import org.springframework.hateoas.server.EntityLinks;
import org.springframework.test.context.ContextConfiguration;
/**
@@ -57,8 +59,9 @@ public class PersistentEntityResourceAssemblerIntegrationTests extends AbstractC
when(projector.projectExcerpt(any())).thenAnswer(new ReturnsArgumentAt(0));
ConversionService conversionService = new DefaultConversionService();
PersistentEntityResourceAssembler assembler = new PersistentEntityResourceAssembler(entities, projector,
associations, new DefaultSelfLinkProvider(entities, entityLinks, Collections.emptyList()));
associations, new DefaultSelfLinkProvider(entities, entityLinks, Collections.emptyList(), conversionService));
User user = new User();
user.id = BigInteger.valueOf(4711);

View File

@@ -51,6 +51,7 @@ import org.springframework.web.context.request.ServletWebRequest;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.ReadContext;
/**
* Integration tests for entity (de)serialization.
@@ -129,7 +130,9 @@ public class PersistentEntitySerializationTests {
PersistentEntityResource resource = PersistentEntityResource
.build(dave, repositories.getPersistentEntity(User.class)).build();
assertThat(JsonPath.parse(mapper.writeValueAsString(resource)).read("$.colleaguesMap.carter._links.user.href",
String.class)).isNotNull();
String result = mapper.writeValueAsString(resource);
ReadContext document = JsonPath.parse(result);
assertThat(document.read("$.colleaguesMap.carter._links.user.href", String.class)).isNotNull();
}
}