Skip to content

Commit 705c283

Browse files
committed
Merge branch '1.3.x'
2 parents 41e2a2f + 518617d commit 705c283

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

spring-graphql/src/main/java/org/springframework/graphql/execution/ReactiveAdapterRegistryHelper.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -25,7 +25,6 @@
2525

2626
import org.springframework.core.ReactiveAdapter;
2727
import org.springframework.core.ReactiveAdapterRegistry;
28-
import org.springframework.util.Assert;
2928

3029
/**
3130
* Helper to adapt a result Object to {@link Mono} or {@link Flux} through
@@ -90,7 +89,8 @@ public static Object toMonoOrFluxIfReactive(@Nullable Object result) {
9089

9190
/**
9291
* Return a {@link Flux} for the given result Object, adapting to a
93-
* {@link Publisher} first if necessary via {@link ReactiveAdapterRegistry}.
92+
* {@link Publisher} via {@link ReactiveAdapterRegistry} or wrapping it as
93+
* {@code Flux} if necessary.
9494
* @param result the result Object to adapt
9595
* @return a {@link Flux}, possibly empty if the result is {@code null}
9696
*/
@@ -102,8 +102,7 @@ public static Flux<?> toSubscriptionFlux(@Nullable Object result) {
102102
return Flux.from(publisher);
103103
}
104104
ReactiveAdapter adapter = registry.getAdapter(result.getClass());
105-
Assert.state(adapter != null, "Expected Publisher for a subscription");
106-
return Flux.from(adapter.toPublisher(result));
105+
return ((adapter != null) ? Flux.from(adapter.toPublisher(result)) : Flux.just(result));
107106
}
108107

109108
/**

spring-graphql/src/test/java/org/springframework/graphql/server/webflux/GraphQlSseHandlerTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,26 @@ void shouldWriteMultipleEventsForSubscription() {
105105
""");
106106
}
107107

108+
@Test // gh-1213
109+
void shouldHandleNonPublisherValue() {
110+
111+
SerializableGraphQlRequest request = initRequest(
112+
"subscription TestSubscription { bookSearch(author:\"Orwell\") { id name } }");
113+
114+
GraphQlSseHandler handler = createHandler(env -> BookSource.getBook(1L));
115+
MockServerHttpResponse response = handleRequest(this.httpRequest, handler, request);
116+
117+
assertThat(response.getHeaders().getContentType().isCompatibleWith(MediaType.TEXT_EVENT_STREAM)).isTrue();
118+
assertThat(response.getBodyAsString().block()).isEqualTo("""
119+
event:next
120+
data:{"data":{"bookSearch":{"id":"1","name":"Nineteen Eighty-Four"}}}
121+
122+
event:complete
123+
data:{}
124+
125+
""");
126+
}
127+
108128
@Test
109129
void shouldWriteEventsAndTerminalError() {
110130

spring-graphql/src/test/java/org/springframework/graphql/server/webmvc/GraphQlSseHandlerTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,25 @@ void shouldWriteMultipleEventsForSubscription() throws Exception {
115115
""");
116116
}
117117

118+
@Test // gh-1213
119+
void shouldHandleNonPublisherValue() throws Exception {
120+
GraphQlSseHandler handler = createSseHandler(env -> BookSource.getBook(1L));
121+
MockHttpServletRequest request = createServletRequest("""
122+
{ "query": "subscription TestSubscription { bookSearch { id name } }" }
123+
""");
124+
MockHttpServletResponse response = handleAndAwait(request, handler);
125+
126+
assertThat(response.getContentType()).isEqualTo(MediaType.TEXT_EVENT_STREAM_VALUE);
127+
assertThat(response.getContentAsString()).isEqualTo("""
128+
event:next
129+
data:{"data":{"bookSearch":{"id":"1","name":"Nineteen Eighty-Four"}}}
130+
131+
event:complete
132+
data:
133+
134+
""");
135+
}
136+
118137
@Test
119138
void shouldSendKeepAlivePings() throws Exception {
120139
WebGraphQlHandler webGraphQlHandler = createWebGraphQlHandler(env -> Mono.delay(Duration.ofMillis(50)).then());

0 commit comments

Comments
 (0)