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
16 changes: 15 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
rootProject.name = 'graphql-java'
pluginManagement {
repositories {
mavenCentral()
maven {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

url 'https://plugins.gradle.org/m2'
metadataSources {
// Avoid redirection to defunct JCenter when Gradle module metadata is not published by a plugin (e.g. JMH plugin)
ignoreGradleMetadataRedirection()
mavenPom()
artifact()
}
}
}
}

rootProject.name = 'graphql-java'
14 changes: 11 additions & 3 deletions src/main/java/graphql/scalar/GraphqlStringCoercing.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@
@Internal
public class GraphqlStringCoercing implements Coercing<String, String> {


private String toStringImpl(Object input) {
return String.valueOf(input);
}

private String parseValueImpl(@NotNull Object input, @NotNull Locale locale) {
if (!(input instanceof String)) {
throw new CoercingParseValueException(
i18nMsg(locale, "String.unexpectedRawValueType", typeName(input))
);
}
return (String) input;
}

private String parseLiteralImpl(@NotNull Object input, Locale locale) {
if (!(input instanceof StringValue)) {
throw new CoercingParseLiteralException(
Expand Down Expand Up @@ -56,12 +64,12 @@ public String serialize(@NotNull Object dataFetcherResult) {
@Override
@Deprecated
public String parseValue(@NotNull Object input) {
return toStringImpl(input);
return parseValueImpl(input, Locale.getDefault());
}

@Override
public String parseValue(@NotNull Object input, @NotNull GraphQLContext graphQLContext, @NotNull Locale locale) throws CoercingParseValueException {
return toStringImpl(input);
return parseValueImpl(input, locale);
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/i18n/Scalars.properties
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ Float.unexpectedAstType=Expected an AST type of ''IntValue'' or ''FloatValue'' b
#
Boolean.notBoolean=Expected a value that can be converted to type ''Boolean'' but it was a ''{0}''
Boolean.unexpectedAstType=Expected an AST type of ''BooleanValue'' but it was a ''{0}''
#
String.unexpectedRawValueType=Expected a String input, but it was a ''{0}''
2 changes: 2 additions & 0 deletions src/main/resources/i18n/Scalars_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ Float.unexpectedAstType=Erwartet wurde ein AST type von ''IntValue'' oder ''Floa
#
Boolean.notBoolean=Erwartet wurde ein Wert, der in den Typ ''Boolean'' konvertiert werden kann, aber es war ein ''{0}''
Boolean.unexpectedAstType=Erwartet wurde ein AST type ''BooleanValue'', aber es war ein ''{0}''
#
String.unexpectedRawValueType=Erwartet wurde eine String-Eingabe, aber es war ein ''{0}''
36 changes: 33 additions & 3 deletions src/test/groovy/graphql/ScalarsStringTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import graphql.execution.CoercedVariables
import graphql.language.BooleanValue
import graphql.language.StringValue
import graphql.schema.CoercingParseLiteralException
import graphql.schema.CoercingParseValueException
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll
Expand Down Expand Up @@ -54,7 +55,6 @@ class ScalarsStringTest extends Specification {
def "String serialize #value into #result (#result.class)"() {
expect:
Scalars.GraphQLString.getCoercing().serialize(value, GraphQLContext.default, Locale.default) == result
Scalars.GraphQLString.getCoercing().parseValue(value, GraphQLContext.default, Locale.default) == result
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Splitting out tests for serialize and parseValue


where:
value | result
Expand All @@ -64,10 +64,9 @@ class ScalarsStringTest extends Specification {
}

@Unroll
def "String serialize #value into #result (#result.class) with deprecated methods"() {
def "String serialize #value into #result (#result.class) with deprecated method"() {
expect:
Scalars.GraphQLString.getCoercing().serialize(value) == result // Retain deprecated method for test coverage
Scalars.GraphQLString.getCoercing().parseValue(value) == result // Retain deprecated method for test coverage

where:
value | result
Expand All @@ -76,4 +75,35 @@ class ScalarsStringTest extends Specification {
customObject | "foo"
}

def "String parseValue value into result"() {
expect:
Scalars.GraphQLString.getCoercing().parseValue("123ab", GraphQLContext.default, Locale.default) == "123ab"
}

def "String parseValue value into result with deprecated method"() {
expect:
Scalars.GraphQLString.getCoercing().parseValue("123ab") == "123ab" // Retain deprecated method for test coverage
}

@Unroll
def "String parseValue throws exception for non-String values"() {
when:
Scalars.GraphQLString.getCoercing().parseValue(literal, GraphQLContext.default, Locale.default)
then:
def ex = thrown(CoercingParseValueException)

where:
literal | _
123 | _
true | _
customObject | _
}

def "String parseValue English exception message"() {
when:
Scalars.GraphQLString.getCoercing().parseValue(9001, GraphQLContext.default, Locale.ENGLISH)
then:
def ex = thrown(CoercingParseValueException)
ex.message == "Expected a String input, but it was a 'Integer'"
}
}