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
This commit is contained in:
Brian Clozel
2025-02-13 16:52:23 +01:00
parent 803df55188
commit 5d615eb5cd
4 changed files with 34 additions and 32 deletions

View File

@@ -44,7 +44,7 @@ public class GraphQlHttpHandler extends AbstractGraphQlHttpHandler {
private static final List<MediaType> SUPPORTED_MEDIA_TYPES = List.of(
MediaTypes.APPLICATION_GRAPHQL_RESPONSE, MediaType.APPLICATION_JSON, APPLICATION_GRAPHQL);
private boolean isStandardMode = false;
private boolean httpOkOnValidationErrors = false;
/**
@@ -65,29 +65,31 @@ public class GraphQlHttpHandler extends AbstractGraphQlHttpHandler {
}
/**
* Return whether this HTTP handler should conform to the "GraphQL over HTTP specification"
* when the {@link MediaTypes#APPLICATION_GRAPHQL_RESPONSE} is selected.
* <p>When enabled, this mode will use 4xx/5xx HTTP response status if an error occurs before
* Return whether this HTTP handler should use HTTP 200 OK responses if an error occurs before
* the GraphQL request execution phase starts; for example, if JSON parsing, GraphQL document parsing,
* or GraphQL document validation fails. When disabled, behavior will remain consistent with the
* "application/json" response content type.
* or GraphQL document validation fail.
* <p>This option only applies to {@link MediaTypes#APPLICATION_GRAPHQL_RESPONSE} responses,
* as legacy {@link MediaType#APPLICATION_JSON} responses always use HTTP 200 OK in such cases.
* Enabling this option means the server will not conform to the "GraphQL over HTTP specification".
* <p>By default, this is set to {@code false}.
* @since 1.4.0
* @see <a href="https://graphql.github.io/graphql-over-http/draft/#sec-application-graphql-response-json">GraphQL over HTTP specification</a>
*/
public boolean isStandardMode() {
return this.isStandardMode;
public boolean isHttpOkOnValidationErrors() {
return this.httpOkOnValidationErrors;
}
/**
* Set whether this HTTP handler should conform to the "GraphQL over HTTP specification"
* when the {@link MediaTypes#APPLICATION_GRAPHQL_RESPONSE} is selected.
* @param standardMode whether the "standard mode" should be enabled
* Set whether this HTTP handler should use HTTP 200 OK responses if an error occurs before
* the GraphQL request execution phase starts.
* @param httpOkOnValidationErrors whether "HTTP 200 OK" responses should always be used
* @since 1.4.0
* @see #isStandardMode
* @deprecated since 1.4, will be made {@code false} permanently in a future release
* @see #isHttpOkOnValidationErrors
*/
public void setStandardMode(boolean standardMode) {
this.isStandardMode = standardMode;
@Deprecated(since = "1.4.0", forRemoval = true)
public void setHttpOkOnValidationErrors(boolean httpOkOnValidationErrors) {
this.httpOkOnValidationErrors = httpOkOnValidationErrors;
}
protected Mono<ServerResponse> prepareResponse(ServerRequest request, WebGraphQlResponse response) {
@@ -100,7 +102,7 @@ public class GraphQlHttpHandler extends AbstractGraphQlHttpHandler {
}
protected HttpStatus selectResponseStatus(WebGraphQlResponse response, MediaType responseMediaType) {
if (this.isStandardMode
if (!isHttpOkOnValidationErrors()
&& !response.getExecutionResult().isDataPresent()
&& MediaTypes.APPLICATION_GRAPHQL_RESPONSE.equals(responseMediaType)) {
return HttpStatus.BAD_REQUEST;

View File

@@ -49,7 +49,7 @@ public class GraphQlHttpHandler extends AbstractGraphQlHttpHandler {
private static final List<MediaType> SUPPORTED_MEDIA_TYPES = List.of(
MediaTypes.APPLICATION_GRAPHQL_RESPONSE, MediaType.APPLICATION_JSON, APPLICATION_GRAPHQL);
private boolean isStandardMode = false;
private boolean httpOkOnValidationErrors = false;
/**
* Create a new instance.
@@ -72,29 +72,31 @@ public class GraphQlHttpHandler extends AbstractGraphQlHttpHandler {
}
/**
* Return whether this HTTP handler should conform to the "GraphQL over HTTP specification"
* when the {@link MediaTypes#APPLICATION_GRAPHQL_RESPONSE} is selected.
* <p>When enabled, this mode will use 4xx/5xx HTTP response status if an error occurs before
* Return whether this HTTP handler should use HTTP 200 OK responses if an error occurs before
* the GraphQL request execution phase starts; for example, if JSON parsing, GraphQL document parsing,
* or GraphQL document validation fails. When disabled, behavior will remain consistent with the
* "application/json" response content type.
* or GraphQL document validation fail.
* <p>This option only applies to {@link MediaTypes#APPLICATION_GRAPHQL_RESPONSE} responses,
* as legacy {@link MediaType#APPLICATION_JSON} responses always use HTTP 200 OK in such cases.
* Enabling this option means the server will not conform to the "GraphQL over HTTP specification".
* <p>By default, this is set to {@code false}.
* @since 1.4.0
* @see <a href="https://graphql.github.io/graphql-over-http/draft/#sec-application-graphql-response-json">GraphQL over HTTP specification</a>
*/
public boolean isStandardMode() {
return this.isStandardMode;
public boolean isHttpOkOnValidationErrors() {
return this.httpOkOnValidationErrors;
}
/**
* Set whether this HTTP handler should conform to the "GraphQL over HTTP specification"
* when the {@link MediaTypes#APPLICATION_GRAPHQL_RESPONSE} is selected.
* @param standardMode whether the "standard mode" should be enabled
* Set whether this HTTP handler should use HTTP 200 OK responses if an error occurs before
* the GraphQL request execution phase starts.
* @param httpOkOnValidationErrors whether "HTTP 200 OK" responses should always be used
* @since 1.4.0
* @see #isStandardMode
* @deprecated since 1.4, will be made {@code false} permanently in a future release
* @see #isHttpOkOnValidationErrors
*/
public void setStandardMode(boolean standardMode) {
this.isStandardMode = standardMode;
@Deprecated(since = "1.4.0", forRemoval = true)
public void setHttpOkOnValidationErrors(boolean httpOkOnValidationErrors) {
this.httpOkOnValidationErrors = httpOkOnValidationErrors;
}
@@ -129,7 +131,7 @@ public class GraphQlHttpHandler extends AbstractGraphQlHttpHandler {
}
protected HttpStatus selectResponseStatus(WebGraphQlResponse response, MediaType responseMediaType) {
if (this.isStandardMode
if (!isHttpOkOnValidationErrors()
&& !response.getExecutionResult().isDataPresent()
&& MediaTypes.APPLICATION_GRAPHQL_RESPONSE.equals(responseMediaType)) {
return HttpStatus.BAD_REQUEST;

View File

@@ -229,7 +229,6 @@ public class GraphQlHttpProtocolTests {
AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(context);
reader.register(WebFluxTestConfig.class);
GraphQlHttpHandler httpHandler = graphQlSetup.toHttpHandlerWebFlux();
httpHandler.setStandardMode(true);
RouterFunction<ServerResponse> routerFunction = RouterFunctions
.route()
.POST("/graphql", RequestPredicates.accept(MediaType.APPLICATION_JSON, MediaTypes.APPLICATION_GRAPHQL_RESPONSE),

View File

@@ -226,7 +226,6 @@ public class GraphQlHttpProtocolTests {
reader.register(MvcTestConfig.class);
context.setServletContext(new MockServletContext());
GraphQlHttpHandler httpHandler = graphQlSetup.toHttpHandler();
httpHandler.setStandardMode(true);
RouterFunction<ServerResponse> routerFunction = RouterFunctions
.route()
.POST("/graphql", RequestPredicates.accept(MediaType.APPLICATION_JSON, MediaTypes.APPLICATION_GRAPHQL_RESPONSE),