Skip to content

DATACMNS-1836 - Adds JSR310 converters to the converters used for projections. #479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.5.0-SNAPSHOT</version>
<version>2.5.0-DATAJDBC-1836-SNAPSHOT</version>

<name>Spring Data Core</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.convert.Jsr310Converters;
import org.springframework.data.util.NullableWrapperConverters;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
Expand All @@ -43,6 +44,7 @@
* @author Oliver Gierke
* @author Christoph Strobl
* @author Mark Paluch
* @author Jens Schauder
* @see SpelAwareProxyProjectionFactory
* @since 1.10
*/
Expand All @@ -51,6 +53,7 @@ class ProxyProjectionFactory implements ProjectionFactory, BeanClassLoaderAware
final static GenericConversionService CONVERSION_SERVICE = new DefaultConversionService();

static {
Jsr310Converters.getConvertersToRegister().forEach(CONVERSION_SERVICE::addConverter);
NullableWrapperConverters.registerConvertersIn(CONVERSION_SERVICE);
CONVERSION_SERVICE.removeConvertible(Object.class, Object.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@

import java.beans.PropertyDescriptor;
import java.lang.reflect.Proxy;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -37,6 +41,7 @@
* @author Oliver Gierke
* @author Wim Deblauwe
* @author Mark Paluch
* @author Jens Schauder
*/
class ProxyProjectionFactoryUnitTests {

Expand Down Expand Up @@ -276,17 +281,30 @@ void supportsOptionalWithProjectionAsReturnTypeIfPresent() {
});
}

@Test // DATACMNS-1836
void supportsDateToLocalDateTimeConversion() {

Customer customer = new Customer();
customer.firstname = "Dave";
customer.dob = new GregorianCalendar(1967, Calendar.JANUARY, 9).getTime();

customer.address = new Address();
customer.address.city = "New York";
customer.address.zipCode = "ZIP";

CustomerWithLocalDateTime excerpt = factory.createProjection(CustomerWithLocalDateTime.class, customer);

assertThat(excerpt.getFirstname()).isEqualTo("Dave");
assertThat(excerpt.getDob()).isEqualTo(LocalDateTime.of(1967, 1, 9, 0, 0));

}

interface Contact {}

static class Customer implements Contact {
interface CustomerWithLocalDateTime {
String getFirstname();

Long id;
String firstname, lastname;
Address address;
byte[] picture;
Address[] shippingAddresses;
Map<String, Object> data;
Optional<String> optional;
LocalDateTime getDob();
}

static class Address {
Expand Down Expand Up @@ -336,4 +354,16 @@ interface CustomerWithOptionalHavingProjection {

Optional<AddressExcerpt> getAddress();
}

static class Customer implements Contact {

Long id;
String firstname, lastname;
Date dob;
Address address;
byte[] picture;
Address[] shippingAddresses;
Map<String, Object> data;
Optional<String> optional;
}
}