From bbf152a32f3913daf5aea1dfcd4fd1104fa9d56f Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 29 Jun 2021 14:52:33 +0100 Subject: [PATCH] Deserialize errorType from extensions The errorType field on GraphQLError is specific to GraphQL Java and when serialized via GraphqlErrorHelper#toSpecification it becomes an extension with the key "classification". This commit ensures that we correctly deserialize back to errorType from the "classification" extension but unfortunately that's limited to the ErrorClassification enums that we know of. --- .../graphql/SampleApplicationTests.java | 9 +++---- .../graphql/SampleApplicationTests.java | 9 +++---- .../graphql/test/tester/TestGraphQlError.java | 27 ++++++++++++------- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/samples/webflux-security/src/test/java/io/spring/sample/graphql/SampleApplicationTests.java b/samples/webflux-security/src/test/java/io/spring/sample/graphql/SampleApplicationTests.java index 1f926f96..67c61c1c 100644 --- a/samples/webflux-security/src/test/java/io/spring/sample/graphql/SampleApplicationTests.java +++ b/samples/webflux-security/src/test/java/io/spring/sample/graphql/SampleApplicationTests.java @@ -66,8 +66,7 @@ class SampleApplicationTests { .errors() .satisfy(errors -> { assertThat(errors).hasSize(1); - assertThat(errors.get(0).getExtensions().get("classification")) - .isEqualTo(ErrorType.UNAUTHORIZED.name()); + assertThat(errors.get(0).getErrorType()).isEqualTo(ErrorType.UNAUTHORIZED); }); } @@ -86,8 +85,7 @@ class SampleApplicationTests { .errors() .satisfy(errors -> { assertThat(errors).hasSize(1); - assertThat(errors.get(0).getExtensions().get("classification")) - .isEqualTo(ErrorType.FORBIDDEN.name()); + assertThat(errors.get(0).getErrorType()).isEqualTo(ErrorType.FORBIDDEN); }); } @@ -118,8 +116,7 @@ class SampleApplicationTests { .errors() .satisfy(errors -> { assertThat(errors).hasSize(1); - assertThat(errors.get(0).getExtensions().get("classification")) - .isEqualTo(ErrorType.UNAUTHORIZED.name()); + assertThat(errors.get(0).getErrorType()).isEqualTo(ErrorType.UNAUTHORIZED); }); } diff --git a/samples/webmvc-http-security/src/test/java/io/spring/sample/graphql/SampleApplicationTests.java b/samples/webmvc-http-security/src/test/java/io/spring/sample/graphql/SampleApplicationTests.java index 60e14f91..42141303 100644 --- a/samples/webmvc-http-security/src/test/java/io/spring/sample/graphql/SampleApplicationTests.java +++ b/samples/webmvc-http-security/src/test/java/io/spring/sample/graphql/SampleApplicationTests.java @@ -51,8 +51,7 @@ class SampleApplicationTests { .errors() .satisfy(errors -> { assertThat(errors).hasSize(1); - assertThat(errors.get(0).getExtensions().get("classification")) - .isEqualTo(ErrorType.UNAUTHORIZED.name()); + assertThat(errors.get(0).getErrorType()).isEqualTo(ErrorType.UNAUTHORIZED); }); } @@ -71,8 +70,7 @@ class SampleApplicationTests { .errors() .satisfy(errors -> { assertThat(errors).hasSize(1); - assertThat(errors.get(0).getExtensions().get("classification")) - .isEqualTo(ErrorType.FORBIDDEN.name()); + assertThat(errors.get(0).getErrorType()).isEqualTo(ErrorType.FORBIDDEN); }); } @@ -103,8 +101,7 @@ class SampleApplicationTests { .errors() .satisfy(errors -> { assertThat(errors).hasSize(1); - assertThat(errors.get(0).getExtensions().get("classification")) - .isEqualTo(ErrorType.UNAUTHORIZED.name()); + assertThat(errors.get(0).getErrorType()).isEqualTo(ErrorType.UNAUTHORIZED); }); } diff --git a/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/TestGraphQlError.java b/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/TestGraphQlError.java index 699905c6..c945cda7 100644 --- a/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/TestGraphQlError.java +++ b/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/TestGraphQlError.java @@ -42,9 +42,6 @@ class TestGraphQlError implements GraphQLError { @Nullable private List locations; - @Nullable - private ErrorClassification errorType; - @Nullable private List path; @@ -75,15 +72,27 @@ class TestGraphQlError implements GraphQLError { return this.locations; } - @SuppressWarnings("unused") - void setErrorType(ErrorClassification errorType) { - this.errorType = errorType; - } - @Override @Nullable public ErrorClassification getErrorType() { - return this.errorType; + // Attempt the reverse of how errorType is serialized in GraphqlErrorHelper.toSpecification. + // However we can only do that for ErrorClassification enums that we know of. + String value = (getExtensions() != null ? (String) getExtensions().get("classification") : null); + if (value != null) { + try { + return graphql.ErrorType.valueOf(value); + } + catch (IllegalArgumentException ex) { + // ignore + } + try { + return org.springframework.graphql.execution.ErrorType.valueOf(value); + } + catch (IllegalArgumentException ex) { + // ignore + } + } + return null; } @SuppressWarnings("unused")