Correctly retrieve operation from request body
Backport of e74240 and 5be2d8. Closes gh-819
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2020-2022 the original author or authors.
|
||||
* Copyright 2020-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -43,8 +43,8 @@ public class RSocketGraphQlRequest extends DefaultExecutionGraphQlRequest implem
|
||||
* @param locale the locale from the HTTP request, if any
|
||||
*/
|
||||
public RSocketGraphQlRequest(Map<String, Object> body, String id, @Nullable Locale locale) {
|
||||
super(getKey("query", body), getKey("operationName", body), getKey("variables", body),
|
||||
getKey("extensions", body), id, locale);
|
||||
super(getKey(QUERY_KEY, body), getKey(OPERATION_NAME_KEY, body),
|
||||
getKey(VARIABLES_KEY, body), getKey(EXTENSIONS_KEY, body), id, locale);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2020-2022 the original author or authors.
|
||||
* Copyright 2020-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -58,7 +58,8 @@ public class WebGraphQlRequest extends DefaultExecutionGraphQlRequest implements
|
||||
public WebGraphQlRequest(
|
||||
URI uri, HttpHeaders headers, Map<String, Object> body, String id, @Nullable Locale locale) {
|
||||
|
||||
super(getQuery(body), getOperation(body), getMap("variables", body), getMap("extensions", body), id, locale);
|
||||
super(getQuery(body), getOperation(body),
|
||||
getMap(VARIABLES_KEY, body), getMap(EXTENSIONS_KEY, body), id, locale);
|
||||
|
||||
Assert.notNull(uri, "URI is required'");
|
||||
Assert.notNull(headers, "HttpHeaders is required'");
|
||||
@@ -68,18 +69,18 @@ public class WebGraphQlRequest extends DefaultExecutionGraphQlRequest implements
|
||||
}
|
||||
|
||||
private static String getQuery(Map<String, Object> body) {
|
||||
Object value = body.get("query");
|
||||
Object value = body.get(QUERY_KEY);
|
||||
if (!(value instanceof String) || !StringUtils.hasText((String) value)) {
|
||||
throw new ServerWebInputException("Invalid value for 'query'");
|
||||
throw new ServerWebInputException("Invalid value for '" + QUERY_KEY + "'");
|
||||
}
|
||||
return (String) value;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static String getOperation(Map<String, Object> body) {
|
||||
Object value = body.get("operation");
|
||||
Object value = body.get(OPERATION_NAME_KEY);
|
||||
if (value != null && !(value instanceof String)) {
|
||||
throw new ServerWebInputException("Invalid value for 'operation'");
|
||||
throw new ServerWebInputException("Invalid value for '" + OPERATION_NAME_KEY + "'");
|
||||
}
|
||||
return (String) value;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -35,6 +35,15 @@ import org.springframework.util.ObjectUtils;
|
||||
*/
|
||||
public class DefaultGraphQlRequest implements GraphQlRequest {
|
||||
|
||||
protected static final String QUERY_KEY = "query";
|
||||
|
||||
protected static final String OPERATION_NAME_KEY = "operationName";
|
||||
|
||||
protected static final String VARIABLES_KEY = "variables";
|
||||
|
||||
protected static final String EXTENSIONS_KEY = "extensions";
|
||||
|
||||
|
||||
private final String document;
|
||||
|
||||
@Nullable
|
||||
@@ -96,15 +105,15 @@ public class DefaultGraphQlRequest implements GraphQlRequest {
|
||||
@Override
|
||||
public Map<String, Object> toMap() {
|
||||
Map<String, Object> map = new LinkedHashMap<>(3);
|
||||
map.put("query", getDocument());
|
||||
map.put(QUERY_KEY, getDocument());
|
||||
if (getOperationName() != null) {
|
||||
map.put("operationName", getOperationName());
|
||||
map.put(OPERATION_NAME_KEY, getOperationName());
|
||||
}
|
||||
if (!CollectionUtils.isEmpty(getVariables())) {
|
||||
map.put("variables", new LinkedHashMap<>(getVariables()));
|
||||
map.put(VARIABLES_KEY, new LinkedHashMap<>(getVariables()));
|
||||
}
|
||||
if (!CollectionUtils.isEmpty(getExtensions())) {
|
||||
map.put("extensions", new LinkedHashMap<>(getExtensions()));
|
||||
map.put(EXTENSIONS_KEY, new LinkedHashMap<>(getExtensions()));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@@ -24,7 +24,6 @@ import java.util.Map;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.web.server.ServerWebInputException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
@@ -39,17 +38,21 @@ public class WebGraphQlRequestTests {
|
||||
@Test // gh-726
|
||||
void invalidBody() {
|
||||
testInvalidBody(new HashMap<>());
|
||||
|
||||
Map<String, Object> empty = new HashMap<>();
|
||||
empty.put("query", Collections.emptyMap());
|
||||
testInvalidBody(empty);
|
||||
|
||||
Map<String, Object> emptyOperations = new HashMap<>();
|
||||
emptyOperations.put("query", "query { foo }");
|
||||
emptyOperations.put("operation", Collections.emptyMap());
|
||||
testInvalidBody(emptyOperations);
|
||||
|
||||
Map<String, Object> invalidVariables = new HashMap<>();
|
||||
invalidVariables.put("query", "query { foo }");
|
||||
invalidVariables.put("variables", "not-a-map");
|
||||
testInvalidBody(invalidVariables);
|
||||
|
||||
Map<String, Object> invalidExtensions = new HashMap<>();
|
||||
invalidExtensions.put("query", "query { foo }");
|
||||
invalidExtensions.put("extensions", "not-a-map");
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2020-2022 the original author or authors.
|
||||
* Copyright 2020-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -21,6 +21,8 @@ import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.graphql.GraphQlRequest;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
@@ -31,19 +33,24 @@ class DefaultGraphQlRequestTests {
|
||||
|
||||
@Test
|
||||
void requestAsMapShouldContainAllEntries() {
|
||||
|
||||
String document = "query HeroNameAndFriends($episode: Episode) {" +
|
||||
" hero(episode: $episode) {" +
|
||||
" name"
|
||||
+ " }" +
|
||||
"}";
|
||||
|
||||
Map<String, Object> variables = Collections.singletonMap("episode", "JEDI");
|
||||
Map<String, Object> extensions = Collections.singletonMap("myExtension", "value");
|
||||
|
||||
DefaultExecutionGraphQlRequest request = new DefaultExecutionGraphQlRequest(document, "HeroNameAndFriends",
|
||||
variables, extensions, "1", null);
|
||||
GraphQlRequest request = new DefaultExecutionGraphQlRequest(
|
||||
document, "HeroNameAndFriends", variables, extensions, "1", null);
|
||||
|
||||
assertThat(request.toMap()).containsEntry("query", document).containsEntry("operationName", "HeroNameAndFriends")
|
||||
.containsEntry("variables", variables).containsEntry("extensions", extensions);
|
||||
assertThat(request.toMap())
|
||||
.containsEntry("query", document)
|
||||
.containsEntry("operationName", "HeroNameAndFriends")
|
||||
.containsEntry("variables", variables)
|
||||
.containsEntry("extensions", extensions);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user