Support to receive aggregate references as request parameters.
We now support using AggregateReference as type to bind request parameters taking URIs pointing to related aggregates. The default resolution will try to resolve the entire URI via UriToEntityConverter but one can also provide a function that can extract any part of the URI to be then resolved into either an identifier, aggregate instance or jMolecules Association against the ConversionService. Fixes #2239.
This commit is contained in:
@@ -109,16 +109,18 @@ public class RepositoryTestsConfig {
|
||||
@Bean
|
||||
public Module persistentEntityModule() {
|
||||
|
||||
var conversionService = new DefaultConversionService();
|
||||
|
||||
RepositoryResourceMappings mappings = new RepositoryResourceMappings(repositories(), persistentEntities(),
|
||||
config());
|
||||
EntityLinks entityLinks = new RepositoryEntityLinks(repositories(), mappings, config(),
|
||||
mock(PagingAndSortingTemplateVariables.class), PluginRegistry.of(DefaultIdConverter.INSTANCE));
|
||||
SelfLinkProvider selfLinkProvider = new DefaultSelfLinkProvider(persistentEntities(), entityLinks,
|
||||
Collections.<EntityLookup<?>> emptyList(), new DefaultConversionService());
|
||||
Collections.<EntityLookup<?>> emptyList(), conversionService);
|
||||
|
||||
DefaultRepositoryInvokerFactory invokerFactory = new DefaultRepositoryInvokerFactory(repositories());
|
||||
UriToEntityConverter uriToEntityConverter = new UriToEntityConverter(persistentEntities(), invokerFactory,
|
||||
repositories());
|
||||
() -> conversionService);
|
||||
|
||||
Associations associations = new Associations(mappings, config());
|
||||
LinkCollector collector = new DefaultLinkCollector(persistentEntities(), selfLinkProvider, associations);
|
||||
|
||||
@@ -116,16 +116,18 @@ class RepositoryTestsConfig {
|
||||
@Bean
|
||||
public Module persistentEntityModule() {
|
||||
|
||||
var conversionService = new DefaultConversionService();
|
||||
|
||||
RepositoryResourceMappings mappings = new RepositoryResourceMappings(repositories(), persistentEntities(),
|
||||
config());
|
||||
EntityLinks entityLinks = new RepositoryEntityLinks(repositories(), mappings, config(),
|
||||
mock(PagingAndSortingTemplateVariables.class), PluginRegistry.of(DefaultIdConverter.INSTANCE));
|
||||
SelfLinkProvider selfLinkProvider = new DefaultSelfLinkProvider(persistentEntities(), entityLinks,
|
||||
Collections.emptyList(), new DefaultConversionService());
|
||||
Collections.emptyList(), conversionService);
|
||||
|
||||
DefaultRepositoryInvokerFactory invokerFactory = new DefaultRepositoryInvokerFactory(repositories());
|
||||
UriToEntityConverter uriToEntityConverter = new UriToEntityConverter(persistentEntities(), invokerFactory,
|
||||
repositories());
|
||||
() -> conversionService);
|
||||
|
||||
Associations associations = new Associations(mappings, config());
|
||||
LinkCollector collector = new DefaultLinkCollector(persistentEntities(), selfLinkProvider, associations);
|
||||
|
||||
@@ -1,44 +1,51 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-rest-tests</artifactId>
|
||||
<version>4.1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<name>Spring Data REST Tests - Shop</name>
|
||||
<artifactId>spring-data-rest-tests-shop</artifactId>
|
||||
|
||||
<properties>
|
||||
<java-module-name>spring.data.rest.tests.shop</java-module-name>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>spring-data-rest-tests-core</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<type>test-jar</type>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-keyvalue</artifactId>
|
||||
<version>${springdata.keyvalue}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Explicit declaration required on Java 11 and above-->
|
||||
|
||||
<dependency>
|
||||
<groupId>jakarta.annotation</groupId>
|
||||
<artifactId>jakarta.annotation-api</artifactId>
|
||||
<version>2.0.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jmolecules</groupId>
|
||||
<artifactId>jmolecules-ddd</artifactId>
|
||||
<version>${jmolecules}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jmolecules.integrations</groupId>
|
||||
<artifactId>jmolecules-spring</artifactId>
|
||||
<version>${jmolecules-integration}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jmolecules.integrations</groupId>
|
||||
<artifactId>jmolecules-jackson</artifactId>
|
||||
<version>${jmolecules-integration}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.rest.tests.shop;
|
||||
|
||||
import org.springframework.data.rest.core.AggregateReference;
|
||||
import org.springframework.data.rest.core.AssociationAggregateReference;
|
||||
import org.springframework.data.rest.tests.shop.Order.OrderIdentifier;
|
||||
import org.springframework.data.rest.webmvc.BasePathAwareController;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
/**
|
||||
* Custom controller to mimic a user-defined one using {@link AggregateReference}s to receive references to other Spring
|
||||
* Data REST managed aggregates.
|
||||
*
|
||||
* @author Oliver Drotbohm
|
||||
* @since 4.1
|
||||
*/
|
||||
@ResponseBody
|
||||
@BasePathAwareController
|
||||
class CustomController {
|
||||
|
||||
@GetMapping("/order-custom-id")
|
||||
OrderIdentifier customOrderId(@RequestParam("order") AggregateReference<Order, OrderIdentifier> reference) {
|
||||
|
||||
return reference //
|
||||
.withIdSource(it -> it.getPathSegments().get(3)) //
|
||||
.resolveId();
|
||||
}
|
||||
|
||||
@GetMapping("/order-custom-association")
|
||||
OrderIdentifier customOrderAssociation(
|
||||
@RequestParam("order") AssociationAggregateReference<Order, OrderIdentifier> reference) {
|
||||
|
||||
return reference //
|
||||
.withIdSource(it -> it.getPathSegments().get(3)) //
|
||||
.resolveAssociation() //
|
||||
.getId();
|
||||
}
|
||||
|
||||
@GetMapping("/order-custom")
|
||||
OrderIdentifier customOrder(@RequestParam("order") AggregateReference<Order, OrderIdentifier> reference) {
|
||||
|
||||
return reference //
|
||||
.withIdSource(it -> it.getPathSegments().get(3)) //
|
||||
.resolveAggregate() //
|
||||
.getId();
|
||||
}
|
||||
}
|
||||
@@ -17,28 +17,32 @@ package org.springframework.data.rest.tests.shop;
|
||||
|
||||
import lombok.Value;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.jmolecules.ddd.types.AggregateRoot;
|
||||
import org.jmolecules.ddd.types.Identifier;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.annotation.Reference;
|
||||
import org.springframework.data.rest.core.config.Projection;
|
||||
import org.springframework.data.rest.tests.shop.LineItem.LineItemProductsOnlyProjection;
|
||||
import org.springframework.data.rest.tests.shop.Order.OrderIdentifier;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
* @author Craig Andrews
|
||||
*/
|
||||
@Value
|
||||
public class Order {
|
||||
public class Order implements AggregateRoot<Order, OrderIdentifier> {
|
||||
|
||||
@Projection(name = "itemsOnly", types = Order.class)
|
||||
public interface OrderItemsOnlyProjection {
|
||||
List<LineItemProductsOnlyProjection> getItems();
|
||||
}
|
||||
|
||||
private final @Id UUID id = UUID.randomUUID();
|
||||
private final @Id OrderIdentifier id = new OrderIdentifier(UUID.randomUUID());
|
||||
private final List<LineItem> items = new ArrayList<>();
|
||||
private final @Reference Customer customer;
|
||||
|
||||
@@ -47,4 +51,10 @@ public class Order {
|
||||
this.items.add(item);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Value
|
||||
static class OrderIdentifier implements Identifier, Serializable {
|
||||
private static final long serialVersionUID = -3362660123468974881L;
|
||||
UUID id;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,13 +15,12 @@
|
||||
*/
|
||||
package org.springframework.data.rest.tests.shop;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.rest.tests.shop.Order.OrderIdentifier;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public interface OrderRepository extends CrudRepository<Order, UUID> {
|
||||
public interface OrderRepository extends CrudRepository<Order, OrderIdentifier> {
|
||||
|
||||
}
|
||||
|
||||
@@ -19,17 +19,22 @@ import jakarta.annotation.PostConstruct;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import org.jmolecules.jackson.JMoleculesModule;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.map.repository.config.EnableMapRepositories;
|
||||
import org.springframework.data.rest.core.config.EntityLookupRegistrar;
|
||||
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
|
||||
import org.springframework.data.rest.tests.shop.Customer.Gender;
|
||||
import org.springframework.data.rest.tests.shop.Product.ProductNameOnlyProjection;
|
||||
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer;
|
||||
import org.springframework.hateoas.EntityModel;
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.server.RepresentationModelProcessor;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
@@ -68,6 +73,33 @@ class ShopConfiguration {
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
CustomController customController() {
|
||||
return new CustomController();
|
||||
}
|
||||
|
||||
@Bean
|
||||
RepositoryRestConfigurer repositoryRestConfigurer() {
|
||||
|
||||
return new RepositoryRestConfigurer() {
|
||||
|
||||
@Override
|
||||
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config, CorsRegistry cors) {
|
||||
|
||||
EntityLookupRegistrar lookup = config.withEntityLookup();
|
||||
|
||||
lookup.forRepository(ProductRepository.class, Product::getName, ProductRepository::findByName);
|
||||
lookup.forValueRepository(LineItemTypeRepository.class, LineItemType::getName,
|
||||
LineItemTypeRepository::findByName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configureJacksonObjectMapper(ObjectMapper objectMapper) {
|
||||
objectMapper.registerModule(new JMoleculesModule());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
void init() {
|
||||
|
||||
@@ -86,21 +118,4 @@ class ShopConfiguration {
|
||||
|
||||
orders.save(order);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class SpringDataRestConfiguration implements RepositoryRestConfigurer {
|
||||
|
||||
@Bean
|
||||
RepositoryRestConfigurer configurer() {
|
||||
|
||||
return RepositoryRestConfigurer.withConfig(config -> {
|
||||
|
||||
EntityLookupRegistrar lookup = config.withEntityLookup();
|
||||
|
||||
lookup.forRepository(ProductRepository.class, Product::getName, ProductRepository::findByName);
|
||||
lookup.forValueRepository(LineItemTypeRepository.class, LineItemType::getName,
|
||||
LineItemTypeRepository::findByName);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,19 +16,21 @@
|
||||
package org.springframework.data.rest.tests.shop;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.rest.tests.AbstractWebIntegrationTests;
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import org.springframework.test.web.servlet.ResultActions;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
|
||||
|
||||
import com.jayway.jsonpath.JsonPath;
|
||||
|
||||
@@ -42,6 +44,8 @@ import com.jayway.jsonpath.JsonPath;
|
||||
@ContextConfiguration(classes = ShopConfiguration.class)
|
||||
class ShopIntegrationTests extends AbstractWebIntegrationTests {
|
||||
|
||||
@Autowired OrderRepository orders;
|
||||
|
||||
@Test
|
||||
void rendersRepresentationCorrectly() throws Exception {
|
||||
|
||||
@@ -70,7 +74,6 @@ class ShopIntegrationTests extends AbstractWebIntegrationTests {
|
||||
|
||||
client.follow(client.discoverUnique("products").expand(arguments))//
|
||||
.andExpect(status().isOk())//
|
||||
.andDo(MockMvcResultHandlers.print()) //
|
||||
.andExpect(jsonPath("$._embedded.products[0].name", notNullValue()))//
|
||||
.andExpect(jsonPath("$._embedded.products[0].price").doesNotExist());
|
||||
}
|
||||
@@ -96,6 +99,36 @@ class ShopIntegrationTests extends AbstractWebIntegrationTests {
|
||||
.andExpect(jsonPath("$._embedded.orders[0].items[0].products[0]._links.beta").exists());
|
||||
}
|
||||
|
||||
@Test // GH-2239
|
||||
void triggersCustomControllerWithAggregateReferenceToId() throws Exception {
|
||||
|
||||
var uuid = UUID.randomUUID();
|
||||
|
||||
mvc.perform(get("/order-custom-id?order=/order/foo/bar/{id}", uuid))
|
||||
.andExpect(status().is2xxSuccessful())
|
||||
.andExpect(content().string("\"%s\"".formatted(uuid.toString())));
|
||||
}
|
||||
|
||||
@Test // GH-2239
|
||||
void triggersCustomControllerWithAggregateReferenceToAggregate() throws Exception {
|
||||
|
||||
var uuid = orders.findAll().iterator().next().getId().getId();
|
||||
|
||||
mvc.perform(get("/order-custom?order=/order/foo/bar/{id}", uuid))
|
||||
.andExpect(status().is2xxSuccessful())
|
||||
.andExpect(content().string("\"%s\"".formatted(uuid.toString())));
|
||||
}
|
||||
|
||||
@Test // GH-2239
|
||||
void triggersCustomControllerWithAggregateReferenceToAssociation() throws Exception {
|
||||
|
||||
var uuid = UUID.randomUUID();
|
||||
|
||||
mvc.perform(get("/order-custom-association?order=/order/foo/bar/{id}", uuid))
|
||||
.andExpect(status().is2xxSuccessful())
|
||||
.andExpect(content().string("\"%s\"".formatted(uuid.toString())));
|
||||
}
|
||||
|
||||
private static void expectRelatedResource(String name, ResultActions actions) throws Exception {
|
||||
|
||||
int dotIndex = name.lastIndexOf('.');
|
||||
|
||||
Reference in New Issue
Block a user