Skip to content

Commit 158684f

Browse files
oliemansmapottere
authored andcommitted
Treat operationName:"" as if it were null (#35)
* Treat operationName:"" as if it were null * Reformatted condition line
1 parent 3e5d2b3 commit 158684f

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/main/java/graphql/servlet/GraphQLServlet.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,9 @@ private GraphQL newGraphQL(GraphQLSchema schema) {
262262
}
263263

264264
private void query(String query, String operationName, Map<String, Object> variables, GraphQLSchema schema, HttpServletRequest req, HttpServletResponse resp, GraphQLContext context) throws IOException {
265-
if (Subject.getSubject(AccessController.getContext()) == null && context.getSubject().isPresent()) {
265+
if (operationName != null && operationName.isEmpty()) {
266+
query(query, null, variables, schema, req, resp, context);
267+
} else if (Subject.getSubject(AccessController.getContext()) == null && context.getSubject().isPresent()) {
266268
Subject.doAs(context.getSubject().get(), (PrivilegedAction<Void>) () -> {
267269
try {
268270
query(query, operationName, variables, schema, req, resp, context);

src/test/groovy/graphql/servlet/GraphQLServletSpec.groovy

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,19 @@ class GraphQLServletSpec extends Specification {
148148

149149
}
150150

151+
def "query over HTTP GET with empty non-null operationName returns data"() {
152+
when:
153+
response = new MockHttpServletResponse()
154+
request.addParameter('query', 'query echo{ echo: echo(arg:"test") }')
155+
request.addParameter('operationName', '')
156+
servlet.doGet(request, response)
157+
then:
158+
response.getStatus() == STATUS_OK
159+
response.getContentType() == CONTENT_TYPE_JSON_UTF8
160+
getResponseContent().data.echo == "test"
161+
162+
}
163+
151164
def "mutation over HTTP GET returns errors"() {
152165
setup:
153166
request.addParameter('query', 'mutation { echo(arg:"test") }')
@@ -217,6 +230,22 @@ class GraphQLServletSpec extends Specification {
217230
getResponseContent().data.echoTwo == "test-two"
218231
}
219232

233+
def "query over HTTP POST body with empty non-null operationName returns data"() {
234+
setup:
235+
request.setContent(mapper.writeValueAsBytes([
236+
query: 'query echo{ echo: echo(arg:"test") }',
237+
operationName: ''
238+
]))
239+
240+
when:
241+
servlet.doPost(request, response)
242+
243+
then:
244+
response.getStatus() == STATUS_OK
245+
response.getContentType() == CONTENT_TYPE_JSON_UTF8
246+
getResponseContent().data.echo == "test"
247+
}
248+
220249
def "query over HTTP POST multipart named 'graphql' returns data"() {
221250
setup:
222251
request.setContentType("multipart/form-data, boundary=test")
@@ -271,6 +300,24 @@ class GraphQLServletSpec extends Specification {
271300
getResponseContent().data.echoTwo == "test-two"
272301
}
273302

303+
def "query over HTTP POST multipart named 'query' with empty non-null operationName returns data"() {
304+
setup:
305+
request.setContentType("multipart/form-data, boundary=test")
306+
request.setMethod("POST")
307+
request.setContent(new TestMultipartContentBuilder()
308+
.addPart('query', 'query echo{ echo: echo(arg:"test") }')
309+
.addPart('operationName', '')
310+
.build())
311+
312+
when:
313+
servlet.doPost(request, response)
314+
315+
then:
316+
response.getStatus() == STATUS_OK
317+
response.getContentType() == CONTENT_TYPE_JSON_UTF8
318+
getResponseContent().data.echo == "test"
319+
}
320+
274321
def "query over HTTP POST multipart named 'query' with variables returns data"() {
275322
setup:
276323
request.setContentType("multipart/form-data, boundary=test")

0 commit comments

Comments
 (0)