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
10 changes: 0 additions & 10 deletions src/main/java/graphql/execution/ValuesResolverLegacy.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ static Value<?> valueToLiteralLegacy(Object value, GraphQLType type, GraphQLCont
// Since value is an internally represented value, it must be serialized
// to an externally represented value before converting into an AST.
final Object serialized = serializeLegacy(type, value, graphqlContext, locale);
if (isNullishLegacy(serialized)) {
return null;
}

Copy link
Copy Markdown
Member Author

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 serialize method.


// Others serialize based on their corresponding JavaScript scalar types.
if (serialized instanceof Boolean) {
Expand Down Expand Up @@ -157,11 +154,4 @@ private static Object serializeLegacy(GraphQLType type, Object value, GraphQLCon
return ((GraphQLEnumType) type).serialize(value, graphqlContext, locale);
}
}

private static boolean isNullishLegacy(Object serialized) {
if (serialized instanceof Number) {
return Double.isNaN(((Number) serialized).doubleValue());
}
return serialized == null;
}
}
13 changes: 11 additions & 2 deletions src/main/java/graphql/scalar/GraphqlFloatCoercing.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I did not add a separate else if (input instanceof Float) branch because it causes some tests to fail.

Getting the doubleValue() from a BigDecimal produces an ever-so-slightly-different result to getting the doubleValue() out of a Float.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Current this is

private Double convertImpl(Object 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
        if (input instanceof Double) {
            Double doubleInput = (Double) input;
            if (Double.isNaN(doubleInput) || Double.isInfinite(doubleInput)) {
                return null;
            }
            return doubleInput;
        } else if (isNumberIsh(input)) {
            BigDecimal value;
            try {
                value = new BigDecimal(input.toString());
            } catch (NumberFormatException e) {
                return null;
            }
            return value.doubleValue();
        } else {
            return null;
        }
    }

So it does not apply the checks to the BigDecimal derived string

Why not do

private Double convertImpl(Object 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)) {
            BigDecimal value;
            try {
                value = new BigDecimal(input.toString());
            } catch (NumberFormatException e) {
                return null;
            }
            doubleInput =  value.doubleValue();
        } else {
            return null;
        }
        
        if (Double.isNaN(doubleInput) || Double.isInfinite(doubleInput)) {
                return null;
        }
       return doubleInput
    }

Forgive me if this doesnt compile

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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

@dondonz dondonz Oct 27, 2022 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Move NaN/Infinity check lower down to make this easier to read

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Edit: not only easier to read, it catches the edge case where value.doubleValue() returns positive/negative infinity

}

@NotNull
Expand Down
40 changes: 31 additions & 9 deletions src/test/groovy/graphql/ScalarsFloatTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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() | _

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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() | _
}

}
14 changes: 0 additions & 14 deletions src/test/groovy/graphql/ScalarsQuerySchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,6 @@ public class ScalarsQuerySchema {

public static final GraphQLObjectType queryType = newObject()
.name("QueryType")
// Static scalars

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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)
Expand All @@ -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)
Expand Down
13 changes: 10 additions & 3 deletions src/test/groovy/graphql/execution/ValuesResolverTestLegacy.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package graphql.language
package graphql.execution

@dondonz dondonz Oct 16, 2022 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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')])
)
}

Expand Down