diff --git a/settings.gradle b/settings.gradle index 72827aa7ad..160b054c14 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,2 +1,16 @@ -rootProject.name = 'graphql-java' +pluginManagement { + repositories { + mavenCentral() + maven { + 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' diff --git a/src/main/java/graphql/scalar/GraphqlStringCoercing.java b/src/main/java/graphql/scalar/GraphqlStringCoercing.java index 541d157fd9..b330f254ae 100644 --- a/src/main/java/graphql/scalar/GraphqlStringCoercing.java +++ b/src/main/java/graphql/scalar/GraphqlStringCoercing.java @@ -24,11 +24,19 @@ @Internal public class GraphqlStringCoercing implements Coercing { - 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( @@ -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 diff --git a/src/main/resources/i18n/Scalars.properties b/src/main/resources/i18n/Scalars.properties index 0897fe58eb..4dbc68d683 100644 --- a/src/main/resources/i18n/Scalars.properties +++ b/src/main/resources/i18n/Scalars.properties @@ -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}'' diff --git a/src/main/resources/i18n/Scalars_de.properties b/src/main/resources/i18n/Scalars_de.properties index de929f4770..ce045d8404 100644 --- a/src/main/resources/i18n/Scalars_de.properties +++ b/src/main/resources/i18n/Scalars_de.properties @@ -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}'' diff --git a/src/test/groovy/graphql/ScalarsStringTest.groovy b/src/test/groovy/graphql/ScalarsStringTest.groovy index e19e02796a..8a725eb122 100644 --- a/src/test/groovy/graphql/ScalarsStringTest.groovy +++ b/src/test/groovy/graphql/ScalarsStringTest.groovy @@ -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 @@ -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 where: value | result @@ -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 @@ -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'" + } }