DATACMNS-734 - Improved Querydsl predicate binding using formatters.

We now default to a FormattingConversionService to make sure comma-separated values are transformed into arrays correctly. Also, we now build up dedicated TypeDescriptor instances for the property or field about to be bound to potentially pick up formatting annotations on those.
This commit is contained in:
Oliver Gierke
2015-07-21 15:45:58 +02:00
parent 11056be292
commit e3668c621d
3 changed files with 58 additions and 7 deletions

View File

@@ -18,6 +18,9 @@ package org.springframework.data.querydsl;
import java.util.Date;
import java.util.List;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;
import com.mysema.query.annotations.QueryEntity;
/**
@@ -29,7 +32,7 @@ import com.mysema.query.annotations.QueryEntity;
public class User {
public String firstname, lastname;
public Date dateOfBirth;
public @DateTimeFormat(iso = ISO.DATE) Date dateOfBirth;
public Address address;
public List<String> nickNames;
public Long inceptionYear;

View File

@@ -19,16 +19,20 @@ import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.springframework.test.util.ReflectionTestUtils.*;
import java.text.ParseException;
import java.util.List;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.querydsl.QUser;
import org.springframework.data.querydsl.SimpleEntityPathResolver;
import org.springframework.data.querydsl.User;
import org.springframework.data.querydsl.Users;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
@@ -53,7 +57,8 @@ public class QuerydslPredicateBuilderUnitTests {
@Before
public void setUp() {
this.builder = new QuerydslPredicateBuilder(new DefaultConversionService(), SimpleEntityPathResolver.INSTANCE);
this.builder = new QuerydslPredicateBuilder(new DefaultFormattingConversionService(),
SimpleEntityPathResolver.INSTANCE);
this.values = new LinkedMultiValueMap<String, String>();
}
@@ -185,4 +190,20 @@ public class QuerydslPredicateBuilderUnitTests {
assertThat(constant.getConstant(), instanceOf(String.class));
assertThat((String) (constant.getConstant()), equalTo("rivers,two"));
}
/**
* @see DATACMNS-734
*/
@Test
public void bindsDateCorrectly() throws ParseException {
DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd");
String date = format.print(new DateTime());
values.add("dateOfBirth", format.print(new DateTime()));
Predicate predicate = builder.getPredicate(USER_TYPE, values, DEFAULT_BINDINGS);
assertThat(predicate, is((Predicate) QUser.user.dateOfBirth.eq(format.parseDateTime(date).toDate())));
}
}