Skip to content

Commit a3db3fd

Browse files
koenpuntrstoyanchev
authored andcommittedSep 19, 2023
Correctly retrieve operation from request body
Backport of e74240 and 5be2d8. Closes gh-818
1 parent c9e0d53 commit a3db3fd

File tree

5 files changed

+38
-22
lines changed

5 files changed

+38
-22
lines changed
 

Diff for: ‎spring-graphql/src/main/java/org/springframework/graphql/server/RSocketGraphQlRequest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2022 the original author or authors.
2+
* Copyright 2020-2023 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.
@@ -43,8 +43,8 @@ public class RSocketGraphQlRequest extends DefaultExecutionGraphQlRequest implem
4343
* @param locale the locale from the HTTP request, if any
4444
*/
4545
public RSocketGraphQlRequest(Map<String, Object> body, String id, @Nullable Locale locale) {
46-
super(getKey("query", body), getKey("operationName", body), getKey("variables", body),
47-
getKey("extensions", body), id, locale);
46+
super(getKey(QUERY_KEY, body), getKey(OPERATION_NAME_KEY, body),
47+
getKey(VARIABLES_KEY, body), getKey(EXTENSIONS_KEY, body), id, locale);
4848
}
4949

5050
@SuppressWarnings("unchecked")

Diff for: ‎spring-graphql/src/main/java/org/springframework/graphql/server/WebGraphQlRequest.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ public WebGraphQlRequest(
8484
URI uri, HttpHeaders headers, @Nullable MultiValueMap<String, HttpCookie> cookies,
8585
Map<String, Object> attributes, Map<String, Object> body, String id, @Nullable Locale locale) {
8686

87-
super(getQuery(body), getOperation(body), getMap("variables", body), getMap("extensions", body), id, locale);
87+
super(getQuery(body), getOperation(body),
88+
getMap(VARIABLES_KEY, body), getMap(EXTENSIONS_KEY, body), id, locale);
8889

8990
Assert.notNull(uri, "URI is required'");
9091
Assert.notNull(headers, "HttpHeaders is required'");
@@ -96,18 +97,18 @@ public WebGraphQlRequest(
9697
}
9798

9899
private static String getQuery(Map<String, Object> body) {
99-
Object value = body.get("query");
100+
Object value = body.get(QUERY_KEY);
100101
if (!(value instanceof String query) || !StringUtils.hasText(query)) {
101-
throw new ServerWebInputException("Invalid value for 'query'");
102+
throw new ServerWebInputException("Invalid value for '" + QUERY_KEY + "'");
102103
}
103104
return (String) value;
104105
}
105106

106107
@Nullable
107108
private static String getOperation(Map<String, Object> body) {
108-
Object value = body.get("operation");
109+
Object value = body.get(OPERATION_NAME_KEY);
109110
if (value != null && !(value instanceof String)) {
110-
throw new ServerWebInputException("Invalid value for 'operation'");
111+
throw new ServerWebInputException("Invalid value for '" + OPERATION_NAME_KEY + "'");
111112
}
112113
return (String) value;
113114
}

Diff for: ‎spring-graphql/src/main/java/org/springframework/graphql/support/DefaultGraphQlRequest.java

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -35,6 +35,15 @@
3535
*/
3636
public class DefaultGraphQlRequest implements GraphQlRequest {
3737

38+
protected static final String QUERY_KEY = "query";
39+
40+
protected static final String OPERATION_NAME_KEY = "operationName";
41+
42+
protected static final String VARIABLES_KEY = "variables";
43+
44+
protected static final String EXTENSIONS_KEY = "extensions";
45+
46+
3847
private final String document;
3948

4049
@Nullable
@@ -96,15 +105,15 @@ public Map<String, Object> getExtensions() {
96105
@Override
97106
public Map<String, Object> toMap() {
98107
Map<String, Object> map = new LinkedHashMap<>(3);
99-
map.put("query", getDocument());
108+
map.put(QUERY_KEY, getDocument());
100109
if (getOperationName() != null) {
101-
map.put("operationName", getOperationName());
110+
map.put(OPERATION_NAME_KEY, getOperationName());
102111
}
103112
if (!CollectionUtils.isEmpty(getVariables())) {
104-
map.put("variables", new LinkedHashMap<>(getVariables()));
113+
map.put(VARIABLES_KEY, new LinkedHashMap<>(getVariables()));
105114
}
106115
if (!CollectionUtils.isEmpty(getExtensions())) {
107-
map.put("extensions", new LinkedHashMap<>(getExtensions()));
116+
map.put(EXTENSIONS_KEY, new LinkedHashMap<>(getExtensions()));
108117
}
109118
return map;
110119
}

Diff for: ‎spring-graphql/src/test/java/org/springframework/graphql/server/WebGraphQlRequestTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class WebGraphQlRequestTests {
3939
void invalidBody() {
4040
testInvalidBody(Map.of());
4141
testInvalidBody(Map.of("query", Collections.emptyMap()));
42-
testInvalidBody(Map.of("query", "query { foo }", "operation", Collections.emptyMap()));
42+
testInvalidBody(Map.of("query", "query { foo }", "operationName", Collections.emptyMap()));
4343
testInvalidBody(Map.of("query", "query { foo }", "variables", "not-a-map"));
4444
testInvalidBody(Map.of("query", "query { foo }", "extensions", "not-a-map"));
4545
}

Diff for: ‎spring-graphql/src/test/java/org/springframework/graphql/support/DefaultGraphQlRequestTests.java

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2022 the original author or authors.
2+
* Copyright 2020-2023 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.
@@ -16,11 +16,12 @@
1616

1717
package org.springframework.graphql.support;
1818

19-
import java.util.Collections;
2019
import java.util.Map;
2120

2221
import org.junit.jupiter.api.Test;
2322

23+
import org.springframework.graphql.GraphQlRequest;
24+
2425
import static org.assertj.core.api.Assertions.assertThat;
2526

2627
/**
@@ -31,19 +32,24 @@ class DefaultGraphQlRequestTests {
3132

3233
@Test
3334
void requestAsMapShouldContainAllEntries() {
35+
3436
String document = "query HeroNameAndFriends($episode: Episode) {" +
3537
" hero(episode: $episode) {" +
3638
" name"
3739
+ " }" +
3840
"}";
39-
Map<String, Object> variables = Collections.singletonMap("episode", "JEDI");
40-
Map<String, Object> extensions = Collections.singletonMap("myExtension", "value");
4141

42-
DefaultExecutionGraphQlRequest request = new DefaultExecutionGraphQlRequest(document, "HeroNameAndFriends",
43-
variables, extensions, "1", null);
42+
Map<String, Object> variables = Map.of("episode", "JEDI");
43+
Map<String, Object> extensions = Map.of("myExtension", "value");
44+
45+
GraphQlRequest request = new DefaultExecutionGraphQlRequest(
46+
document, "HeroNameAndFriends", variables, extensions, "1", null);
4447

45-
assertThat(request.toMap()).containsEntry("query", document).containsEntry("operationName", "HeroNameAndFriends")
46-
.containsEntry("variables", variables).containsEntry("extensions", extensions);
48+
assertThat(request.toMap())
49+
.containsEntry("query", document)
50+
.containsEntry("operationName", "HeroNameAndFriends")
51+
.containsEntry("variables", variables)
52+
.containsEntry("extensions", extensions);
4753
}
4854

4955
}

0 commit comments

Comments
 (0)
Please sign in to comment.