Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ezyhttp-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.4.7</version>
<version>1.4.8</version>
</parent>

<artifactId>ezyhttp-client</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.4.7</version>
<version>1.4.8</version>
</parent>

<artifactId>ezyhttp-core</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.4.7</version>
<version>1.4.8</version>
</parent>

<artifactId>ezyhttp-server-boot</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.4.7</version>
<version>1.4.8</version>
</parent>

<artifactId>ezyhttp-server-core</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-graphql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.4.7</version>
<version>1.4.8</version>
</parent>
<artifactId>ezyhttp-server-graphql</artifactId>
<name>ezyhttp-server-graphql</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public Map filter(
) {
Map answer = new HashMap<>();
Map parentMap = null;
Stack<StackEntry> stack = new Stack<>();
stack.add(new StackEntry(queryDefinition, data));
Deque<StackEntry> stack = new ArrayDeque<>();
stack.push(new StackEntry(queryDefinition, data));
while (!stack.isEmpty()) {
StackEntry entry = stack.pop();
String parentName = entry.field.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ public class GraphQLObjectMapperException extends IllegalArgumentException {

private final Object errors;

public GraphQLObjectMapperException(
Object errors
) {
super(errors.toString());
this.errors = errors;
}

public GraphQLObjectMapperException(
Object errors,
Throwable cause
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import com.tvd12.ezyhttp.server.graphql.query.GraphQLQueryDefinition;
import lombok.AllArgsConstructor;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Map;
import java.util.Stack;

import static com.tvd12.ezyfox.io.EzyStrings.EMPTY_STRING;

Expand All @@ -24,7 +25,7 @@ public GraphQLSchema parseQuery(
) {
String query = standardize(queryToParse);

Stack<GraphQLField.Builder> stack = new Stack<>();
Deque<GraphQLField.Builder> stack = new ArrayDeque<>();
GraphQLSchema.Builder schemaBuilder = GraphQLSchema.builder();

int queryLength = query.length();
Expand All @@ -36,7 +37,7 @@ public GraphQLSchema parseQuery(
if (stack.isEmpty()) {
GraphQLQueryDefinition.Builder queryBuilder =
GraphQLQueryDefinition.builder();
stack.add(queryBuilder);
stack.push(queryBuilder);
continue;
}

Expand All @@ -46,12 +47,15 @@ public GraphQLSchema parseQuery(
nameLength = 0;
}
GraphQLField.Builder childBuilder = GraphQLField.builder();
stack.add(childBuilder);
stack.push(childBuilder);
continue;
}

if (ch == '(') {
GraphQLField.Builder childBuilder = stack.peek();
GraphQLField.Builder childBuilder = peekFieldStackItemOrThrow(
stack,
"there is no child"
);
StringBuilder argumentsBuilder = new StringBuilder();
i = extractQueryArguments(
argumentsBuilder,
Expand Down Expand Up @@ -106,7 +110,10 @@ public GraphQLSchema parseQuery(
nameLength = 0;
}

GraphQLField.Builder parentBuilder = stack.peek();
GraphQLField.Builder parentBuilder = peekFieldStackItemOrThrow(
stack,
"there is no parent case curly brace close"
);
parentBuilder.addField(childBuilder.build());

if (stack.size() == 1) {
Expand All @@ -119,7 +126,7 @@ public GraphQLSchema parseQuery(
if (stack.isEmpty()) {
GraphQLQueryDefinition.Builder queryBuilder =
GraphQLQueryDefinition.builder();
stack.add(queryBuilder);
stack.push(queryBuilder);
nameLength = 0;
continue;
}
Expand All @@ -134,7 +141,7 @@ public GraphQLSchema parseQuery(

GraphQLQueryDefinition.Builder queryBuilder =
GraphQLQueryDefinition.builder();
stack.add(queryBuilder);
stack.push(queryBuilder);
continue;
}

Expand All @@ -146,11 +153,14 @@ public GraphQLSchema parseQuery(
nameLength = 0;
}

GraphQLField.Builder parentBuilder = stack.peek();
GraphQLField.Builder parentBuilder = peekFieldStackItemOrThrow(
stack,
"there is no parent case space"
);
parentBuilder.addField(childBuilder.build());

GraphQLField.Builder newChildBuilder = GraphQLField.builder();
stack.add(newChildBuilder);
stack.push(newChildBuilder);
} else {
nameBuffer[nameLength++] = ch;
}
Expand Down Expand Up @@ -282,4 +292,20 @@ private int extractQueryArguments(
}
return i;
}

private GraphQLField.Builder peekFieldStackItemOrThrow(
Deque<GraphQLField.Builder> stack,
String message
) {
GraphQLField.Builder parentBuilder = stack.peek();
if (parentBuilder == null) {
throw new GraphQLObjectMapperException(
EzyMapBuilder.mapBuilder()
.put("arguments", "invalid")
.put("message", message)
.toMap()
);
}
return parentBuilder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.tvd12.ezyhttp.server.graphql.test.data;

import com.tvd12.ezyhttp.server.graphql.data.GraphQLRequest;
import com.tvd12.test.assertion.Asserts;
import org.testng.annotations.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class GraphQLRequestTest {

@Test
public void getVariablesReturnsEmptyMapWhenUnset() {
// given
GraphQLRequest request = new GraphQLRequest();

// when
Map<String, Object> variables = request.getVariables();

// then
Asserts.assertEquals(variables, Collections.emptyMap(), false);
Asserts.assertTrue(variables.isEmpty());
}

@Test
public void getVariablesReturnsProvidedMap() {
// given
GraphQLRequest request = new GraphQLRequest();
Map<String, Object> provided = new HashMap<>();
provided.put("hello", "world");
request.setVariables(provided);

// when
Map<String, Object> variables = request.getVariables();

// then
Asserts.assertEquals(variables, provided, false);
Asserts.assertEquals(variables.get("hello"), "world");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tvd12.ezyfox.util.EzyMapBuilder;
import com.tvd12.ezyhttp.server.graphql.data.GraphQLField;
import com.tvd12.ezyhttp.server.graphql.exception.GraphQLObjectMapperException;
import com.tvd12.ezyhttp.server.graphql.json.GraphQLObjectMapperFactory;
import com.tvd12.ezyhttp.server.graphql.scheme.GraphQLSchema;
Expand All @@ -12,8 +13,12 @@
import com.tvd12.test.util.RandomUtil;
import org.testng.annotations.Test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.Map;

public class GraphQLSchemaParserTest {
Expand Down Expand Up @@ -501,4 +506,80 @@ public void parseQueryInvalidArgument6Test() {
// then
Asserts.assertEqualsType(e, GraphQLObjectMapperException.class);
}

@Test
public void parseQueryNoChildBeforeArgumentsTest() {
// given
GraphQLSchemaParser instance = new GraphQLSchemaParser(
new ObjectMapper()
);

// when
Throwable e = Asserts.assertThrows(() ->
instance.parseQuery(
"(id: 1)",
Collections.emptyMap()
)
);

// then
Asserts.assertEqualsType(e, GraphQLObjectMapperException.class);
GraphQLObjectMapperException exception = (GraphQLObjectMapperException) e;
Asserts.assertEquals(
exception.getErrors(),
EzyMapBuilder.mapBuilder()
.put("arguments", "invalid")
.put("message", "there is no child")
.toMap(),
false
);
}

@Test
public void requireParentBuilderWithEmptyStackTest() {
// given
GraphQLSchemaParser instance = new GraphQLSchemaParser(
new ObjectMapper()
);
Deque<GraphQLField.Builder> stack = new ArrayDeque<>();

// when
final Method method;
try {
method = GraphQLSchemaParser.class.getDeclaredMethod(
"peekFieldStackItemOrThrow",
Deque.class,
String.class
);
} catch (NoSuchMethodException ex) {
throw new IllegalStateException(
"method requireParentBuilder should exist",
ex
);
}
method.setAccessible(true);
Throwable e = Asserts.assertThrows(() -> {
try {
method.invoke(
instance,
stack,
"there is no parent case curly brace close"
);
} catch (InvocationTargetException ex) {
throw ex.getCause();
}
});

// then
Asserts.assertEqualsType(e, GraphQLObjectMapperException.class);
GraphQLObjectMapperException exception = (GraphQLObjectMapperException) e;
Asserts.assertEquals(
exception.getErrors(),
EzyMapBuilder.mapBuilder()
.put("arguments", "invalid")
.put("message", "there is no parent case curly brace close")
.toMap(),
false
);
}
}
2 changes: 1 addition & 1 deletion ezyhttp-server-jetty/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.4.7</version>
<version>1.4.8</version>
</parent>

<artifactId>ezyhttp-server-jetty</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-management/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.4.7</version>
<version>1.4.8</version>
</parent>
<artifactId>ezyhttp-server-management</artifactId>
<name>ezyhttp-server-management</name>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-thymeleaf/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.4.7</version>
<version>1.4.8</version>
</parent>
<artifactId>ezyhttp-server-thymeleaf</artifactId>
<name>ezyhttp-server-thymeleaf</name>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-tomcat/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.4.7</version>
<version>1.4.8</version>
</parent>

<artifactId>ezyhttp-server-tomcat</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<version>1.0.7</version>
</parent>
<artifactId>ezyhttp</artifactId>
<version>1.4.7</version>
<version>1.4.8</version>
<packaging>pom</packaging>

<name>ezyhttp</name>
Expand Down
Loading