Skip to content

Commit 5d615eb

Browse files
committed
Update config flag for legacy HTTP behavior
This commit renames and changes the behavior of the configuration option on `GraphQlHttpHandler` implementations. The `setHttpOkOnValidationErrors` option is `false` by default and is introduced as a deprecated method right away. Our goal here is to fade out this option as soon as possible the traditional behavior for "application/graphql-response+json" media types. See gh-1117
1 parent 803df55 commit 5d615eb

File tree

4 files changed

+34
-32
lines changed

4 files changed

+34
-32
lines changed

spring-graphql/src/main/java/org/springframework/graphql/server/webflux/GraphQlHttpHandler.java

+17-15
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class GraphQlHttpHandler extends AbstractGraphQlHttpHandler {
4444
private static final List<MediaType> SUPPORTED_MEDIA_TYPES = List.of(
4545
MediaTypes.APPLICATION_GRAPHQL_RESPONSE, MediaType.APPLICATION_JSON, APPLICATION_GRAPHQL);
4646

47-
private boolean isStandardMode = false;
47+
private boolean httpOkOnValidationErrors = false;
4848

4949

5050
/**
@@ -65,29 +65,31 @@ public GraphQlHttpHandler(WebGraphQlHandler graphQlHandler, CodecConfigurer code
6565
}
6666

6767
/**
68-
* Return whether this HTTP handler should conform to the "GraphQL over HTTP specification"
69-
* when the {@link MediaTypes#APPLICATION_GRAPHQL_RESPONSE} is selected.
70-
* <p>When enabled, this mode will use 4xx/5xx HTTP response status if an error occurs before
68+
* Return whether this HTTP handler should use HTTP 200 OK responses if an error occurs before
7169
* the GraphQL request execution phase starts; for example, if JSON parsing, GraphQL document parsing,
72-
* or GraphQL document validation fails. When disabled, behavior will remain consistent with the
73-
* "application/json" response content type.
70+
* or GraphQL document validation fail.
71+
* <p>This option only applies to {@link MediaTypes#APPLICATION_GRAPHQL_RESPONSE} responses,
72+
* as legacy {@link MediaType#APPLICATION_JSON} responses always use HTTP 200 OK in such cases.
73+
* Enabling this option means the server will not conform to the "GraphQL over HTTP specification".
7474
* <p>By default, this is set to {@code false}.
7575
* @since 1.4.0
7676
* @see <a href="https://graphql.github.io/graphql-over-http/draft/#sec-application-graphql-response-json">GraphQL over HTTP specification</a>
7777
*/
78-
public boolean isStandardMode() {
79-
return this.isStandardMode;
78+
public boolean isHttpOkOnValidationErrors() {
79+
return this.httpOkOnValidationErrors;
8080
}
8181

8282
/**
83-
* Set whether this HTTP handler should conform to the "GraphQL over HTTP specification"
84-
* when the {@link MediaTypes#APPLICATION_GRAPHQL_RESPONSE} is selected.
85-
* @param standardMode whether the "standard mode" should be enabled
83+
* Set whether this HTTP handler should use HTTP 200 OK responses if an error occurs before
84+
* the GraphQL request execution phase starts.
85+
* @param httpOkOnValidationErrors whether "HTTP 200 OK" responses should always be used
8686
* @since 1.4.0
87-
* @see #isStandardMode
87+
* @deprecated since 1.4, will be made {@code false} permanently in a future release
88+
* @see #isHttpOkOnValidationErrors
8889
*/
89-
public void setStandardMode(boolean standardMode) {
90-
this.isStandardMode = standardMode;
90+
@Deprecated(since = "1.4.0", forRemoval = true)
91+
public void setHttpOkOnValidationErrors(boolean httpOkOnValidationErrors) {
92+
this.httpOkOnValidationErrors = httpOkOnValidationErrors;
9193
}
9294

9395
protected Mono<ServerResponse> prepareResponse(ServerRequest request, WebGraphQlResponse response) {
@@ -100,7 +102,7 @@ protected Mono<ServerResponse> prepareResponse(ServerRequest request, WebGraphQl
100102
}
101103

102104
protected HttpStatus selectResponseStatus(WebGraphQlResponse response, MediaType responseMediaType) {
103-
if (this.isStandardMode
105+
if (!isHttpOkOnValidationErrors()
104106
&& !response.getExecutionResult().isDataPresent()
105107
&& MediaTypes.APPLICATION_GRAPHQL_RESPONSE.equals(responseMediaType)) {
106108
return HttpStatus.BAD_REQUEST;

spring-graphql/src/main/java/org/springframework/graphql/server/webmvc/GraphQlHttpHandler.java

+17-15
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class GraphQlHttpHandler extends AbstractGraphQlHttpHandler {
4949
private static final List<MediaType> SUPPORTED_MEDIA_TYPES = List.of(
5050
MediaTypes.APPLICATION_GRAPHQL_RESPONSE, MediaType.APPLICATION_JSON, APPLICATION_GRAPHQL);
5151

52-
private boolean isStandardMode = false;
52+
private boolean httpOkOnValidationErrors = false;
5353

5454
/**
5555
* Create a new instance.
@@ -72,29 +72,31 @@ public GraphQlHttpHandler(WebGraphQlHandler graphQlHandler, @Nullable HttpMessag
7272
}
7373

7474
/**
75-
* Return whether this HTTP handler should conform to the "GraphQL over HTTP specification"
76-
* when the {@link MediaTypes#APPLICATION_GRAPHQL_RESPONSE} is selected.
77-
* <p>When enabled, this mode will use 4xx/5xx HTTP response status if an error occurs before
75+
* Return whether this HTTP handler should use HTTP 200 OK responses if an error occurs before
7876
* the GraphQL request execution phase starts; for example, if JSON parsing, GraphQL document parsing,
79-
* or GraphQL document validation fails. When disabled, behavior will remain consistent with the
80-
* "application/json" response content type.
77+
* or GraphQL document validation fail.
78+
* <p>This option only applies to {@link MediaTypes#APPLICATION_GRAPHQL_RESPONSE} responses,
79+
* as legacy {@link MediaType#APPLICATION_JSON} responses always use HTTP 200 OK in such cases.
80+
* Enabling this option means the server will not conform to the "GraphQL over HTTP specification".
8181
* <p>By default, this is set to {@code false}.
8282
* @since 1.4.0
8383
* @see <a href="https://graphql.github.io/graphql-over-http/draft/#sec-application-graphql-response-json">GraphQL over HTTP specification</a>
8484
*/
85-
public boolean isStandardMode() {
86-
return this.isStandardMode;
85+
public boolean isHttpOkOnValidationErrors() {
86+
return this.httpOkOnValidationErrors;
8787
}
8888

8989
/**
90-
* Set whether this HTTP handler should conform to the "GraphQL over HTTP specification"
91-
* when the {@link MediaTypes#APPLICATION_GRAPHQL_RESPONSE} is selected.
92-
* @param standardMode whether the "standard mode" should be enabled
90+
* Set whether this HTTP handler should use HTTP 200 OK responses if an error occurs before
91+
* the GraphQL request execution phase starts.
92+
* @param httpOkOnValidationErrors whether "HTTP 200 OK" responses should always be used
9393
* @since 1.4.0
94-
* @see #isStandardMode
94+
* @deprecated since 1.4, will be made {@code false} permanently in a future release
95+
* @see #isHttpOkOnValidationErrors
9596
*/
96-
public void setStandardMode(boolean standardMode) {
97-
this.isStandardMode = standardMode;
97+
@Deprecated(since = "1.4.0", forRemoval = true)
98+
public void setHttpOkOnValidationErrors(boolean httpOkOnValidationErrors) {
99+
this.httpOkOnValidationErrors = httpOkOnValidationErrors;
98100
}
99101

100102

@@ -129,7 +131,7 @@ protected ServerResponse prepareResponse(ServerRequest request, Mono<WebGraphQlR
129131
}
130132

131133
protected HttpStatus selectResponseStatus(WebGraphQlResponse response, MediaType responseMediaType) {
132-
if (this.isStandardMode
134+
if (!isHttpOkOnValidationErrors()
133135
&& !response.getExecutionResult().isDataPresent()
134136
&& MediaTypes.APPLICATION_GRAPHQL_RESPONSE.equals(responseMediaType)) {
135137
return HttpStatus.BAD_REQUEST;

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

-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@ static WebTestClient createTestClient(WebGraphQlSetup graphQlSetup) {
229229
AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(context);
230230
reader.register(WebFluxTestConfig.class);
231231
GraphQlHttpHandler httpHandler = graphQlSetup.toHttpHandlerWebFlux();
232-
httpHandler.setStandardMode(true);
233232
RouterFunction<ServerResponse> routerFunction = RouterFunctions
234233
.route()
235234
.POST("/graphql", RequestPredicates.accept(MediaType.APPLICATION_JSON, MediaTypes.APPLICATION_GRAPHQL_RESPONSE),

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

-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ static MockMvcTester createMvcTester(WebGraphQlSetup graphQlSetup) {
226226
reader.register(MvcTestConfig.class);
227227
context.setServletContext(new MockServletContext());
228228
GraphQlHttpHandler httpHandler = graphQlSetup.toHttpHandler();
229-
httpHandler.setStandardMode(true);
230229
RouterFunction<ServerResponse> routerFunction = RouterFunctions
231230
.route()
232231
.POST("/graphql", RequestPredicates.accept(MediaType.APPLICATION_JSON, MediaTypes.APPLICATION_GRAPHQL_RESPONSE),

0 commit comments

Comments
 (0)