DATAREST-511 - Support for executing repository methods returning Optionals.
Added an UnwrappingRepositoryInvokerFactory that transparently unwraps JDK 8 and Guava Optionals to make sure the consuming code works with values or plain nulls correctly.
This commit is contained in:
@@ -65,6 +65,7 @@ import org.springframework.data.rest.core.mapping.ResourceDescription;
|
||||
import org.springframework.data.rest.core.mapping.ResourceMappings;
|
||||
import org.springframework.data.rest.core.support.DomainObjectMerger;
|
||||
import org.springframework.data.rest.core.support.RepositoryRelProvider;
|
||||
import org.springframework.data.rest.core.support.UnwrappingRepositoryInvokerFactory;
|
||||
import org.springframework.data.rest.webmvc.BasePathAwareController;
|
||||
import org.springframework.data.rest.webmvc.BasePathAwareHandlerMapping;
|
||||
import org.springframework.data.rest.webmvc.BaseUri;
|
||||
@@ -575,7 +576,8 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
|
||||
|
||||
@Bean
|
||||
public RepositoryInvokerFactory repositoryInvokerFactory() {
|
||||
return new DefaultRepositoryInvokerFactory(repositories(), defaultConversionService());
|
||||
return new UnwrappingRepositoryInvokerFactory(new DefaultRepositoryInvokerFactory(repositories(),
|
||||
defaultConversionService()));
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -20,6 +20,8 @@ import static org.junit.Assert.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.hateoas.Link;
|
||||
@@ -184,6 +186,27 @@ public class TestMvcClient {
|
||||
return discover.get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Traverses the given link relations from the root.
|
||||
*
|
||||
* @param rels
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public Link discoverUnique(String... rels) throws Exception {
|
||||
|
||||
Iterator<String> toTraverse = Arrays.asList(rels).iterator();
|
||||
Link lastLink = null;
|
||||
|
||||
while (toTraverse.hasNext()) {
|
||||
|
||||
String rel = toTraverse.next();
|
||||
lastLink = lastLink == null ? discoverUnique(rel) : discoverUnique(lastLink, rel);
|
||||
}
|
||||
|
||||
return lastLink;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a URI (root), discover the URIs for a given rel.
|
||||
*
|
||||
|
||||
@@ -301,4 +301,18 @@ public class MongoWebTests extends CommonWebTests {
|
||||
andExpect(status().isNotModified()).//
|
||||
andExpect(header().string(ETAG, is(notNullValue())));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREST-511
|
||||
*/
|
||||
@Test
|
||||
public void invokesQueryResourceReturningAnOptional() throws Exception {
|
||||
|
||||
Profile profile = repository.findAll().iterator().next();
|
||||
|
||||
Link link = client.discoverUnique("profiles", "search", "findById");
|
||||
|
||||
mvc.perform(get(link.expand(profile.getId()).getHref())).//
|
||||
andExpect(status().isOk());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
* Copyright 2012-2015 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.
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.data.rest.webmvc.mongodb;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
@@ -32,4 +33,9 @@ public interface ProfileRepository extends PagingAndSortingRepository<Profile, S
|
||||
* @see DATAREST-247
|
||||
*/
|
||||
long countByType(@Param("type") String type);
|
||||
|
||||
/**
|
||||
* @see DATAREST-511
|
||||
*/
|
||||
Optional<Profile> findById(@Param("id") String id);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user