DATAREST-909 - Revert Jackson-aware Sort and Pageable translation.

Revert changes introduced by DATAREST-883 - Jackson-aware field translation in Sort and DATAREST-906 - Consider default pageable if Sort is null. The way how Sort translation was implemented breaks Sort and Pageable argument resolution for custom controllers as a domain type is always required. Argument resolution fails if the related domain type cannot be resolved.

 Related pull requests: #231, #222.
This commit is contained in:
Mark Paluch
2016-09-27 08:59:56 +02:00
parent 5e163d1913
commit 0a49b099bb
15 changed files with 76 additions and 962 deletions

View File

@@ -26,11 +26,8 @@ import javax.persistence.ManyToMany;
import org.springframework.data.rest.core.annotation.RestResource;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* @author Oliver Gierke
* @author Mark Paluch
*/
@Entity
public class Book {
@@ -38,20 +35,16 @@ public class Book {
public @Id @GeneratedValue Long id;
public String isbn, title;
@JsonProperty("sales")
public long soldUnits;
@ManyToMany(cascade = { CascadeType.MERGE }) //
@RestResource(path = "creators") //
public Set<Author> authors;
protected Book() {}
public Book(String isbn, String title, long soldUnits, Iterable<Author> authors) {
public Book(String isbn, String title, Iterable<Author> authors) {
this.isbn = isbn;
this.title = title;
this.soldUnits = soldUnits;
this.authors = new HashSet<Author>();

View File

@@ -17,8 +17,6 @@ package org.springframework.data.rest.webmvc.jpa;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
@@ -28,7 +26,6 @@ import org.springframework.data.rest.core.annotation.RestResource;
/**
* @author Oliver Gierke
* @author Mark Paluch
*/
@RepositoryRestResource(excerptProjection = BookExcerpt.class)
public interface BookRepository extends CrudRepository<Book, Long> {
@@ -38,8 +35,4 @@ public interface BookRepository extends CrudRepository<Book, Long> {
@Query("select b from Book b where :author member of b.authors")
List<Book> findByAuthorsContains(@Param("author") Author author);
@RestResource(rel = "find-spring-books-sorted")
@Query("select b from Book b where b.title like 'Spring%'")
Page<Book> findByTitleIsLike(Pageable pageable);
}

View File

@@ -1,137 +0,0 @@
/*
* Copyright 2016 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
*
* http://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.webmvc.jpa;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import java.util.Collections;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.tests.AbstractWebIntegrationTests;
import org.springframework.data.rest.webmvc.RepositoryRestController;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
import org.springframework.hateoas.Link;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Web integration tests specific to default {@link Pageable} handling.
*
* @author Mark Paluch
*/
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = JpaDefaultPageableWebTests.Config.class)
public class JpaDefaultPageableWebTests extends AbstractWebIntegrationTests {
@Configuration
@Import({ RepositoryRestMvcConfiguration.class, JpaRepositoryConfig.class })
@EnableJpaRepositories(considerNestedRepositories = true)
static class Config extends RepositoryRestConfigurerAdapter {
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.setDefaultPageSize(1);
}
}
@RepositoryRestController
public static class MyRestController {
@RequestMapping(method = RequestMethod.GET, path = "books/default-pageable")
@ResponseBody
Page<Book> getDefaultPageable(Pageable pageable) {
if (pageable != null) {
return new PageImpl<Book>(Collections.singletonList(new Book()), pageable, 1);
}
return new PageImpl<Book>(Collections.emptyList(), pageable, 0);
}
}
@Autowired TestDataPopulator loader;
@Autowired ApplicationContext context;
@Before
public void setUp() {
loader.populateRepositories();
super.setUp();
}
/**
* @see DATAREST-906
*/
@Test
public void executesSearchThatTakesAMappedSortProperty() throws Exception {
Link findBySortedLink = client.discoverUnique("books", "search", "find-spring-books-sorted");
// Assert sort options advertised
assertThat(findBySortedLink.isTemplated(), is(true));
assertThat(findBySortedLink.getVariableNames(), hasItems("sort", "projection"));
// Assert results returned as specified
client.follow(findBySortedLink.expand()).//
andExpect(jsonPath("$._embedded.books[0].title").exists()).//
andExpect(jsonPath("$._embedded.books[1].title").doesNotExist());
client.follow(findBySortedLink.expand("sales,desc")).//
andExpect(jsonPath("$._embedded.books[0].title").exists()).//
andExpect(jsonPath("$._embedded.books[1].title").doesNotExist());
}
/**
* @see DATAREST-906
*/
@Test
public void shouldApplyDefaultPageable() throws Exception {
mvc.perform(get("/books/default-pageable")).andDo(print()) //
.andExpect(jsonPath("$.content[0].sales").value(0)) //
.andExpect(jsonPath("$.size").value(1));
}
/**
* @see DATAREST-906
*/
@Test
public void shouldOverrideDefaultPageable() throws Exception {
mvc.perform(get("/books/default-pageable?size=10")).andDo(print()) //
.andExpect(jsonPath("$.content[0].sales").value(0)) //
.andExpect(jsonPath("$.size").value(10));
}
}

View File

@@ -57,7 +57,6 @@ import com.jayway.jsonpath.JsonPath;
*
* @author Oliver Gierke
* @author Greg Turnquist
* @author Mark Paluch
*/
@Transactional
@ContextConfiguration(classes = JpaRepositoryConfig.class)
@@ -547,7 +546,7 @@ public class JpaWebTests extends CommonWebTests {
* @see DATAREST-384
*/
@Test
public void exectuesSearchThatTakesASort() throws Exception {
public void execturesSearchThatTakesASort() throws Exception {
Link booksLink = client.discoverUnique("books");
Link searchLink = client.discoverUnique(booksLink, "search");
@@ -655,53 +654,6 @@ public class JpaWebTests extends CommonWebTests {
assertThat(links.hasLink("person"), is(true));
}
/**
* @see DATAREST-883
*/
@Test
public void exectuesSearchThatTakesAMappedSortProperty() throws Exception {
Link findBySortedLink = client.discoverUnique("books", "search", "find-by-sorted");
// Assert sort options advertised
assertThat(findBySortedLink.isTemplated(), is(true));
assertThat(findBySortedLink.getVariableNames(), hasItems("sort", "projection"));
// Assert results returned as specified
client.follow(findBySortedLink.expand("sales,desc")).//
andExpect(jsonPath("$._embedded.books[0].title").value("Spring Data (Second Edition)")).//
andExpect(jsonPath("$._embedded.books[1].title").value("Spring Data")).//
andExpect(client.hasLinkWithRel("self"));
client.follow(findBySortedLink.expand("sales,asc")).//
andExpect(jsonPath("$._embedded.books[0].title").value("Spring Data")).//
andExpect(jsonPath("$._embedded.books[1].title").value("Spring Data (Second Edition)")).//
andExpect(client.hasLinkWithRel("self"));
}
/**
* @see DATAREST-883
*/
@Test
public void exectuesCustomQuerySearchThatTakesAMappedSortProperty() throws Exception {
Link findByLink = client.discoverUnique("books", "search", "find-spring-books-sorted");
// Assert sort options advertised
assertThat(findByLink.isTemplated(), is(true));
// Assert results returned as specified
client.follow(findByLink.expand("0", "10", "sales,desc")).//
andExpect(jsonPath("$._embedded.books[0].title").value("Spring Data (Second Edition)")).//
andExpect(jsonPath("$._embedded.books[1].title").value("Spring Data")).//
andExpect(client.hasLinkWithRel("self"));
client.follow(findByLink.expand("0", "10", "unknown,asc,sales,asc")).//
andExpect(jsonPath("$._embedded.books[0].title").value("Spring Data")).//
andExpect(jsonPath("$._embedded.books[1].title").value("Spring Data (Second Edition)")).//
andExpect(client.hasLinkWithRel("self"));
}
private List<Link> preparePersonResources(Person primary, Person... persons) throws Exception {
Link peopleLink = client.discoverUnique("people");

View File

@@ -22,7 +22,6 @@ import org.springframework.beans.factory.annotation.Autowired;
/**
* @author Jon Brisbin
* @author Oliver Gierke
* @author Mark Paluch
*/
public class TestDataPopulator {
@@ -54,8 +53,8 @@ public class TestDataPopulator {
Iterable<Author> authors = this.authors.save(Arrays.asList(ollie, mark, michael, david, john, thomas));
books.save(new Book("1449323952", "Spring Data", 1000, authors));
books.save(new Book("1449323953", "Spring Data (Second Edition)", 2000, authors));
books.save(new Book("1449323952", "Spring Data", authors));
books.save(new Book("1449323953", "Spring Data (Second Edition)", authors));
}
private void populateOrders() {