Skip to content

Commit b4ff9b3

Browse files
koenpuntrstoyanchev
authored andcommitted
Correctly retrieve operation from request body
Backport of e74240 and 5be2d8. Closes gh-819
1 parent 791992b commit b4ff9b3

File tree

5 files changed

+40
-20
lines changed

5 files changed

+40
-20
lines changed

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")

spring-graphql/src/main/java/org/springframework/graphql/server/WebGraphQlRequest.java

+7-6
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.
@@ -58,7 +58,8 @@ public class WebGraphQlRequest extends DefaultExecutionGraphQlRequest implements
5858
public WebGraphQlRequest(
5959
URI uri, HttpHeaders headers, Map<String, Object> body, String id, @Nullable Locale locale) {
6060

61-
super(getQuery(body), getOperation(body), getMap("variables", body), getMap("extensions", body), id, locale);
61+
super(getQuery(body), getOperation(body),
62+
getMap(VARIABLES_KEY, body), getMap(EXTENSIONS_KEY, body), id, locale);
6263

6364
Assert.notNull(uri, "URI is required'");
6465
Assert.notNull(headers, "HttpHeaders is required'");
@@ -68,18 +69,18 @@ public WebGraphQlRequest(
6869
}
6970

7071
private static String getQuery(Map<String, Object> body) {
71-
Object value = body.get("query");
72+
Object value = body.get(QUERY_KEY);
7273
if (!(value instanceof String) || !StringUtils.hasText((String) value)) {
73-
throw new ServerWebInputException("Invalid value for 'query'");
74+
throw new ServerWebInputException("Invalid value for '" + QUERY_KEY + "'");
7475
}
7576
return (String) value;
7677
}
7778

7879
@Nullable
7980
private static String getOperation(Map<String, Object> body) {
80-
Object value = body.get("operation");
81+
Object value = body.get(OPERATION_NAME_KEY);
8182
if (value != null && !(value instanceof String)) {
82-
throw new ServerWebInputException("Invalid value for 'operation'");
83+
throw new ServerWebInputException("Invalid value for '" + OPERATION_NAME_KEY + "'");
8384
}
8485
return (String) value;
8586
}

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
}

spring-graphql/src/test/java/org/springframework/graphql/server/WebGraphQlRequestTests.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.junit.jupiter.api.Test;
2525

2626
import org.springframework.http.HttpHeaders;
27-
import org.springframework.util.LinkedMultiValueMap;
2827
import org.springframework.web.server.ServerWebInputException;
2928

3029
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@@ -39,17 +38,21 @@ public class WebGraphQlRequestTests {
3938
@Test // gh-726
4039
void invalidBody() {
4140
testInvalidBody(new HashMap<>());
41+
4242
Map<String, Object> empty = new HashMap<>();
4343
empty.put("query", Collections.emptyMap());
4444
testInvalidBody(empty);
45+
4546
Map<String, Object> emptyOperations = new HashMap<>();
4647
emptyOperations.put("query", "query { foo }");
4748
emptyOperations.put("operation", Collections.emptyMap());
4849
testInvalidBody(emptyOperations);
50+
4951
Map<String, Object> invalidVariables = new HashMap<>();
5052
invalidVariables.put("query", "query { foo }");
5153
invalidVariables.put("variables", "not-a-map");
5254
testInvalidBody(invalidVariables);
55+
5356
Map<String, Object> invalidExtensions = new HashMap<>();
5457
invalidExtensions.put("query", "query { foo }");
5558
invalidExtensions.put("extensions", "not-a-map");

spring-graphql/src/test/java/org/springframework/graphql/support/DefaultGraphQlRequestTests.java

+12-5
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.
@@ -21,6 +21,8 @@
2121

2222
import org.junit.jupiter.api.Test;
2323

24+
import org.springframework.graphql.GraphQlRequest;
25+
2426
import static org.assertj.core.api.Assertions.assertThat;
2527

2628
/**
@@ -31,19 +33,24 @@ class DefaultGraphQlRequestTests {
3133

3234
@Test
3335
void requestAsMapShouldContainAllEntries() {
36+
3437
String document = "query HeroNameAndFriends($episode: Episode) {" +
3538
" hero(episode: $episode) {" +
3639
" name"
3740
+ " }" +
3841
"}";
42+
3943
Map<String, Object> variables = Collections.singletonMap("episode", "JEDI");
4044
Map<String, Object> extensions = Collections.singletonMap("myExtension", "value");
4145

42-
DefaultExecutionGraphQlRequest request = new DefaultExecutionGraphQlRequest(document, "HeroNameAndFriends",
43-
variables, extensions, "1", null);
46+
GraphQlRequest request = new DefaultExecutionGraphQlRequest(
47+
document, "HeroNameAndFriends", variables, extensions, "1", null);
4448

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

4956
}

0 commit comments

Comments
 (0)