Repository navigation
Float coercion tidy up #2982
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Float coercion tidy up #2982
Changes from all commits
cf7da3c
39d5cb7
1c63f44
216380c
b74d960
978bfd3
3b032cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,18 +29,27 @@ | |
| public class GraphqlFloatCoercing implements Coercing<Double, Double> { | ||
|
|
||
| private Double convertImpl(Object input) { | ||
| if (isNumberIsh(input)) { | ||
| // From the GraphQL Float spec, non-finite floating-point internal values (NaN and Infinity) | ||
| // must raise a field error on both result and input coercion | ||
| Double doubleInput; | ||
| if (input instanceof Double) { | ||
| doubleInput = (Double) input; | ||
| } else if (isNumberIsh(input)) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did not add a separate Getting the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Current this is So it does not apply the checks to the BigDecimal derived string Why not do Forgive me if this doesnt compile
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems more sensible - eg first get it into a double and then check it - regardless Even if BigDecimal never produces Nan or infinity, the logic reads better
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh no, it can produce infinity! This was a great idea |
||
| BigDecimal value; | ||
| try { | ||
| value = new BigDecimal(input.toString()); | ||
| } catch (NumberFormatException e) { | ||
| return null; | ||
| } | ||
| return value.doubleValue(); | ||
| doubleInput = value.doubleValue(); | ||
| } else { | ||
| return null; | ||
| } | ||
|
|
||
| if (Double.isNaN(doubleInput) || Double.isInfinite(doubleInput)) { | ||
| return null; | ||
| } | ||
| return doubleInput; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move NaN/Infinity check lower down to make this easier to read
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Edit: not only easier to read, it catches the edge case where |
||
| } | ||
|
|
||
| @NotNull | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -114,24 +114,46 @@ class ScalarsFloatTest extends Specification { | |
| thrown(CoercingSerializeException) | ||
|
|
||
| where: | ||
| value | _ | ||
| "" | _ | ||
| "not a number " | _ | ||
| Double.NaN | _ | ||
| value | _ | ||
| "" | _ | ||
| "not a number " | _ | ||
| Double.NaN | _ | ||
| Double.NaN.toString() | _ | ||
| Double.POSITIVE_INFINITY | _ | ||
| Double.POSITIVE_INFINITY.toString() | _ | ||
| Double.NEGATIVE_INFINITY | _ | ||
| Double.NEGATIVE_INFINITY.toString() | _ | ||
| Float.NaN | _ | ||
| Float.NaN.toString() | _ | ||
| Float.POSITIVE_INFINITY | _ | ||
| Float.POSITIVE_INFINITY.toString() | _ | ||
| Float.NEGATIVE_INFINITY | _ | ||
| Float.NEGATIVE_INFINITY.toString() | _ | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add String representations to test |
||
| } | ||
|
|
||
| @Unroll | ||
| def "serialize/parseValue throws exception for invalid input #value"() { | ||
| def "parseValue throws exception for invalid input #value"() { | ||
| when: | ||
| Scalars.GraphQLFloat.getCoercing().parseValue(value, GraphQLContext.default, Locale.default) | ||
| then: | ||
| thrown(CoercingParseValueException) | ||
|
|
||
| where: | ||
| value | _ | ||
| "" | _ | ||
| "not a number " | _ | ||
| Double.NaN | _ | ||
| value | _ | ||
| "" | _ | ||
| "not a number " | _ | ||
| Double.NaN | _ | ||
| Double.NaN.toString() | _ | ||
| Double.POSITIVE_INFINITY | _ | ||
| Double.POSITIVE_INFINITY.toString() | _ | ||
| Double.NEGATIVE_INFINITY | _ | ||
| Double.NEGATIVE_INFINITY.toString() | _ | ||
| Float.NaN | _ | ||
| Float.NaN.toString() | _ | ||
| Float.POSITIVE_INFINITY | _ | ||
| Float.POSITIVE_INFINITY.toString() | _ | ||
| Float.NEGATIVE_INFINITY | _ | ||
| Float.NEGATIVE_INFINITY.toString() | _ | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,18 +18,6 @@ public class ScalarsQuerySchema { | |
|
|
||
| public static final GraphQLObjectType queryType = newObject() | ||
| .name("QueryType") | ||
| // Static scalars | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was added a long time ago, before the GraphQL Float spec was updated to specifically not allow NaN and positive/negative infinity values |
||
| .field(newFieldDefinition() | ||
| .name("floatNaN") | ||
| .type(Scalars.GraphQLFloat) | ||
| .staticValue(Double.NaN)) // Retain for test coverage | ||
| // Scalars with input of same type, value echoed back | ||
| .field(newFieldDefinition() | ||
| .name("floatNaNInput") | ||
| .type(Scalars.GraphQLFloat) | ||
| .argument(newArgument() | ||
| .name("input") | ||
| .type(GraphQLNonNull.nonNull(Scalars.GraphQLFloat)))) | ||
| .field(newFieldDefinition() | ||
| .name("stringInput") | ||
| .type(Scalars.GraphQLString) | ||
|
|
@@ -51,12 +39,10 @@ public class ScalarsQuerySchema { | |
| .type(Scalars.GraphQLString))) | ||
| .build(); | ||
|
|
||
| static FieldCoordinates floatNaNCoordinates = FieldCoordinates.coordinates("QueryType", "floatNaNInput"); | ||
| static FieldCoordinates stringInputCoordinates = FieldCoordinates.coordinates("QueryType", "stringInput"); | ||
| static FieldCoordinates floatStringCoordinates = FieldCoordinates.coordinates("QueryType", "floatString"); | ||
| static FieldCoordinates intStringCoordinates = FieldCoordinates.coordinates("QueryType", "intString"); | ||
| static GraphQLCodeRegistry codeRegistry = GraphQLCodeRegistry.newCodeRegistry() | ||
| .dataFetcher(floatNaNCoordinates, inputDF) | ||
| .dataFetcher(stringInputCoordinates, inputDF) | ||
| .dataFetcher(floatStringCoordinates, inputDF) | ||
| .dataFetcher(intStringCoordinates, inputDF) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,13 @@ | ||
| package graphql.language | ||
| package graphql.execution | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tidy up package name and constructor usage |
||
|
|
||
| import graphql.GraphQLContext | ||
| import graphql.language.ArrayValue | ||
| import graphql.language.EnumValue | ||
| import graphql.language.FloatValue | ||
| import graphql.language.IntValue | ||
| import graphql.language.ObjectField | ||
| import graphql.language.ObjectValue | ||
| import graphql.language.StringValue | ||
| import graphql.schema.GraphQLEnumType | ||
| import graphql.schema.GraphQLInputObjectType | ||
| import spock.lang.Ignore | ||
|
|
@@ -137,15 +144,15 @@ class ValuesResolverTestLegacy extends Specification { | |
| def 'converts list to lists'() { | ||
| expect: | ||
| valueToLiteralLegacy(['hello', 'world'], list(GraphQLString), graphQLContext, locale).isEqualTo( | ||
| new ArrayValue(['hello', 'world']) | ||
| new ArrayValue([new StringValue('hello'), new StringValue('world')]) | ||
| ) | ||
| } | ||
|
|
||
| def 'converts arrays to lists'() { | ||
| String[] sArr = ['hello', 'world'] as String[] | ||
| expect: | ||
| valueToLiteralLegacy(sArr, list(GraphQLString), graphQLContext, locale).isEqualTo( | ||
| new ArrayValue(['hello', 'world']) | ||
| new ArrayValue([new StringValue('hello'), new StringValue('world')]) | ||
| ) | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This NaN check was unnecessary. The NaN check already happened inside the
serializemethod.