Skip to content

Commit d21ebe8

Browse files
authored
Fixing issue with rxjava2/3 based Promise implementations not handling null (#13972)
* fixing issue with rxjava based promise implementations not handling a void/null promise return time * cleaning up test comments
1 parent c4c29da commit d21ebe8

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

grails-async/rxjava2/src/main/groovy/org/grails/async/factory/rxjava2/RxPromise.groovy

+11-1
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ class RxPromise<T> implements Promise<T> {
4141
protected boolean finished = false
4242

4343
RxPromise(RxPromiseFactory promiseFactory, Closure callable, Scheduler scheduler) {
44+
4445
this(promiseFactory, Single.create( { SingleEmitter<? super T> singleSubscriber ->
4546
try {
46-
singleSubscriber.onSuccess((T)callable.call())
47+
singleSubscriber.onSuccess((T)runCallable(callable))
4748
} catch (Throwable t) {
4849
singleSubscriber.onError(t)
4950
}
@@ -158,4 +159,13 @@ class RxPromise<T> implements Promise<T> {
158159
}
159160
}
160161
}
162+
163+
static Object runCallable(Closure callable) {
164+
Object rtn = callable.call()
165+
if(rtn == null) {
166+
return Void
167+
} else {
168+
return rtn
169+
}
170+
}
161171
}

grails-async/rxjava2/src/test/groovy/org/grails/async/factory/rxjava2/RxPromiseSpec.groovy

+12
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ class RxPromiseSpec extends Specification {
5454
result == [one: 1, two: 2, four: 4]
5555
}
5656

57+
void 'Test promise null handling'() {
58+
59+
when: 'a null promise result is created'
60+
def promise = Promises.createPromise {
61+
return null
62+
}
63+
def result = promise.get()
64+
65+
then: 'result is void'
66+
result == Void
67+
}
68+
5769
void 'Test promise list handling'() {
5870

5971
when: 'a promise list is created from two promises'

grails-async/rxjava3/src/main/groovy/org/grails/async/factory/rxjava3/RxPromise.groovy

+10-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class RxPromise<T> implements Promise<T> {
4141
RxPromise(RxPromiseFactory promiseFactory, Closure callable, Scheduler scheduler) {
4242
this(promiseFactory, Single.create( { SingleEmitter<? super T> singleSubscriber ->
4343
try {
44-
singleSubscriber.onSuccess((T)callable.call())
44+
singleSubscriber.onSuccess((T)runCallable(callable))
4545
} catch (Throwable t) {
4646
singleSubscriber.onError(t)
4747
}
@@ -156,4 +156,13 @@ class RxPromise<T> implements Promise<T> {
156156
}
157157
}
158158
}
159+
160+
static Object runCallable(Closure callable) {
161+
Object rtn = callable.call()
162+
if(rtn == null) {
163+
return Void
164+
} else {
165+
return rtn
166+
}
167+
}
159168
}

grails-async/rxjava3/src/test/groovy/org/grails/async/factory/rxjava3/RxPromiseSpec.groovy

+12
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ class RxPromiseSpec extends Specification {
4141

4242
}
4343

44+
void 'Test promise null handling'() {
45+
46+
when: 'a null promise result is created'
47+
def promise = Promises.createPromise {
48+
return null
49+
}
50+
def result = promise.get()
51+
52+
then: 'result is void'
53+
result == Void
54+
}
55+
4456
void 'Test promise map handling'() {
4557

4658
when: 'a promise map is created'

0 commit comments

Comments
 (0)