You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ra...@apache.org on 2022/08/29 16:19:10 UTC

[sling-org-apache-sling-graphql-core] 01/01: SLING-11248 - Post empty body should fail gracefully

This is an automated email from the ASF dual-hosted git repository.

radu pushed a commit to branch issue/SLING-11248
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-graphql-core.git

commit 2a398c3a36c707f22c3a699dfab1133bbbdc2013
Author: Radu Cotescu <ra...@apache.org>
AuthorDate: Mon Aug 29 18:16:51 2022 +0200

    SLING-11248 - Post empty body should fail gracefully
    
    * applied slightly modified patch
    
    Co-authored-by: Thierry Ygé <ty...@adobe.com>
---
 .../sling/graphql/core/servlet/QueryParser.java      |  6 ++++++
 .../graphql/core/servlet/GraphQLServletTest.java     | 20 ++++++++++++++------
 2 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/src/main/java/org/apache/sling/graphql/core/servlet/QueryParser.java b/src/main/java/org/apache/sling/graphql/core/servlet/QueryParser.java
index 9f4f00e..2b6b0ce 100644
--- a/src/main/java/org/apache/sling/graphql/core/servlet/QueryParser.java
+++ b/src/main/java/org/apache/sling/graphql/core/servlet/QueryParser.java
@@ -33,9 +33,13 @@ import org.apache.johnzon.mapper.MapperBuilder;
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class QueryParser {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(QueryParser.class);
+
     private QueryParser() {}
 
     static class Result {
@@ -88,6 +92,8 @@ public class QueryParser {
                 if (input.containsKey(JSON_KEY_VARIABLES)) {
                     variables = MAPPER.readObject(input.get(JSON_KEY_VARIABLES), Map.class);
                 }
+            } catch (Exception e) {
+                LOGGER.error("Invalid payload.", e);
             }
         }
 
diff --git a/src/test/java/org/apache/sling/graphql/core/servlet/GraphQLServletTest.java b/src/test/java/org/apache/sling/graphql/core/servlet/GraphQLServletTest.java
index 1fe9d84..39f1f8d 100644
--- a/src/test/java/org/apache/sling/graphql/core/servlet/GraphQLServletTest.java
+++ b/src/test/java/org/apache/sling/graphql/core/servlet/GraphQLServletTest.java
@@ -174,7 +174,7 @@ public class GraphQLServletTest {
         assertEquals(0.5f, metricRegistry.getGauges().get(expectedMetric).getValue());
     }
 
-    private void assertPostWithBody(String contentType, int expectedStatus) throws IOException {
+    private void assertPostWithBody(String contentType, String query, int expectedStatus) throws IOException {
         context.registerInjectActivateService(new SimpleGraphQLCacheProvider());
         context.registerInjectActivateService(new GraphQLServlet(), ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES, TEST_RESOURCE_TYPE,
                 "persistedQueries.suffix", "");
@@ -185,7 +185,7 @@ public class GraphQLServletTest {
         MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(context.bundleContext());
 
         request.setMethod("POST");
-        request.setContent(TEST_QUERY.getBytes(StandardCharsets.UTF_8));
+        request.setContent(query.getBytes(StandardCharsets.UTF_8));
         request.setContentType(contentType);
 
         request.setResource(resource);
@@ -199,21 +199,29 @@ public class GraphQLServletTest {
 
     @Test
     public void testBasicJsonContentType() throws IOException {
-        assertPostWithBody("application/json", 200);
+        assertPostWithBody("application/json", TEST_QUERY, 200);
     }
 
     @Test
     public void testJsonContentTypeWithCharset() throws IOException {
-        assertPostWithBody("application/json  ; charset=UTF-8", 200);
+        assertPostWithBody("application/json  ; charset=UTF-8", TEST_QUERY,200);
     }
 
     @Test
     public void testNoContentType() throws IOException {
-        assertPostWithBody(null, 400);
+        assertPostWithBody(null, TEST_QUERY, 400);
     }
 
     @Test
     public void testWrongContentType() throws IOException {
-        assertPostWithBody("text/html", 400);
+        assertPostWithBody("text/html", TEST_QUERY, 400);
+    }
+
+    /**
+     * SLING-11248 NothingToRead exception should return 400.
+     */
+    @Test
+    public void testEmptyQueryNothingToRead() throws IOException {
+        assertPostWithBody("application/json  ; charset=UTF-8", "", 400);
     }
 }