Skip to content

Commit

Permalink
fix: Unknown enum values/names should result in nulls and not exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
andimarek committed Jan 30, 2016
1 parent 2ec940f commit f232cbd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/main/java/graphql/schema/GraphQLEnumType.java
@@ -1,7 +1,6 @@
package graphql.schema;


import graphql.GraphQLException;
import graphql.language.EnumValue;

import java.util.ArrayList;
Expand Down Expand Up @@ -40,15 +39,15 @@ public Object parseLiteral(Object input) {

private Object getValueByName(Object value) {
GraphQLEnumValueDefinition enumValueDefinition = valueDefinitionMap.get(value);
if (enumValueDefinition.getValue() != null) return enumValueDefinition.getValue();
throw new GraphQLException("");
if (enumValueDefinition != null) return enumValueDefinition.getValue();
return null;
}

private Object getNameByValue(Object value) {
for (GraphQLEnumValueDefinition valueDefinition : valueDefinitionMap.values()) {
if (value.equals(valueDefinition.getValue())) return valueDefinition.getName();
}
throw new GraphQLException("");
return null;
}

public List<GraphQLEnumValueDefinition> getValues() {
Expand Down
40 changes: 40 additions & 0 deletions src/test/groovy/graphql/schema/GraphQLEnumTypeTest.groovy
@@ -0,0 +1,40 @@
package graphql.schema

import spock.lang.Specification

import static graphql.schema.GraphQLEnumType.newEnum


class GraphQLEnumTypeTest extends Specification {

GraphQLEnumType enumType

def setup() {
enumType = newEnum().name("TestEnum")
.value("NAME", 42)
.build();
}

def "parse value returns null for unknown value"() {
expect:
enumType.getCoercing().parseValue("UNKNOWN") == null
}


def "parse value return value for the name"() {
expect:
enumType.getCoercing().parseValue("NAME") == 42
}

def "serialize returns name for value"() {
expect:
enumType.getCoercing().serialize(42) == "NAME"
}

def "serialize returns null for unknown value"() {
expect:
enumType.getCoercing().serialize(12) == null
}


}

0 comments on commit f232cbd

Please sign in to comment.