Skip to content

Commit c8877de

Browse files
mp911deodrotbohm
authored andcommitted
DATACMNS-874 - Fix Scala Option unwrapping.
We now correctly unwrap empty Scala Options so the unwrapping no longer fails. Original pull request: #165.
1 parent bf01d8f commit c8877de

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/main/java/org/springframework/data/repository/util/QueryExecutionConverters.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2015 the original author or authors.
2+
* Copyright 2014-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,7 +15,9 @@
1515
*/
1616
package org.springframework.data.repository.util;
1717

18+
import scala.Function0;
1819
import scala.Option;
20+
import scala.runtime.AbstractFunction0;
1921

2022
import java.util.Collections;
2123
import java.util.HashSet;
@@ -48,6 +50,7 @@
4850
* </ul>
4951
*
5052
* @author Oliver Gierke
53+
* @author Mark Paluch
5154
* @since 1.8
5255
*/
5356
public abstract class QueryExecutionConverters {
@@ -416,19 +419,32 @@ public Object convert(Object source) {
416419
* A {@link Converter} to unwrap a Scala {@link Option} instance.
417420
*
418421
* @author Oliver Gierke
422+
* @author Mark Paluch
419423
* @author 1.13
420424
*/
421425
private static enum ScalOptionUnwrapper implements Converter<Object, Object> {
422426

423427
INSTANCE;
424428

425-
/*
429+
private final Function0<Object> alternative = new AbstractFunction0<Object>() {
430+
431+
/*
432+
* (non-Javadoc)
433+
* @see scala.Function0#apply()
434+
*/
435+
@Override
436+
public Option<Object> apply() {
437+
return null;
438+
}
439+
};
440+
441+
/*
426442
* (non-Javadoc)
427443
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
428444
*/
429445
@Override
430446
public Object convert(Object source) {
431-
return source instanceof Option ? ((Option<?>) source).orNull(null) : source;
447+
return source instanceof Option ? ((Option<?>) source).getOrElse(alternative) : source;
432448
}
433449
}
434450
}

src/test/java/org/springframework/data/repository/util/QueryExecutionConvertersUnitTests.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2015 the original author or authors.
2+
* Copyright 2014-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,6 +37,7 @@
3737
* Unit tests for {@link QueryExecutionConverters}.
3838
*
3939
* @author Oliver Gierke
40+
* @author Mark Paluch
4041
*/
4142
public class QueryExecutionConvertersUnitTests {
4243

@@ -163,4 +164,12 @@ public void turnsNullIntoScalaOptionEmpty() {
163164
public void unwrapsScalaOption() {
164165
assertThat(QueryExecutionConverters.unwrap(Option.apply("foo")), is((Object) "foo"));
165166
}
167+
168+
/**
169+
* @see DATACMNS-874
170+
*/
171+
@Test
172+
public void unwrapsEmptyScalaOption() {
173+
assertThat(QueryExecutionConverters.unwrap(Option.empty()), is((Object) null));
174+
}
166175
}

0 commit comments

Comments
 (0)