Skip to content
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

[Bug Reproduce] Can't use undefined and NaN as enum values #1267

Merged
merged 1 commit into from
Jun 5, 2018

Conversation

IvanGoncharov
Copy link
Member

I clean up some existing tests but the main purpose of this PR is to report problem with enum values(test case added in 9452661).

From #836 it looks like both undefined and NaN could be used as an enum values:

const TestEnum = new GraphQLEnumType({
  name: 'TestEnum',
  values: {
    UNDEFINED: { value: undefined },
    NAN: { value: NaN },
  },
});

And run this query:

{
  undefined: fieldWithEnumInput(input: UNDEFINED)
  NaN: fieldWithEnumInput(input: NAN)
}

It will result in:

{
  "data": {
    "undefined": null,
    "NaN": null
  },
  "errors": [
    {
      "message": "Argument \"input\" has invalid value UNDEFINED.",
      "path": [ "undefined" ]
    }
    {
      "message": "Argument \"input\" has invalid value NAN.",
      "path": [ "NaN" ]
    }
  ]
}

Because of this check:

const coercedValue = valueFromAST(valueNode, argType, variableValues);
if (isInvalid(coercedValue)) {

This issue can't be easily fixed because both undefined and NaN are invalid according to isInvalid function.

I would be happy to help with fixing this issue but I don't know how to fix it correctly.
My main question is why valueFromAST can't just throw on errors?

@leebyron
Copy link
Contributor

leebyron commented Mar 5, 2018

Good catch - want to factor this into two PRs? The improvements to test files should be mergeable as is. Your last commit testing the edge case enum values should be handled separately. Not sure if valueFromAST should throw or not, but it's clear that it needs some way to indicate coercion failure other than returning undefined

@greysteil
Copy link

Hey, maintainer of Dependabot here. Sorry about the reference spam.

I'm going to chat to the GitHub team about whether there's a way to link to an issue / pull from a GitHub pull request description without creating these references. (They come from Dependabot pulling the changelog / release notes into the PR description for the PRs it generates.)

If there's no way that GitHub can let Dependabot do the above, I'll remove the links from the PRs. A pity, but I really don't want to create spam like this!

@mjmahone
Copy link
Contributor

mjmahone commented Jun 1, 2018

@IvanGoncharov it looks like we can close this, as it was done via two smaller commits instead.

Copy link
Contributor

@mjmahone mjmahone left a comment

Choose a reason for hiding this comment

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

Oh wait, you left the test changes in this PR. This looks good to me if you want to fix any conflicts and merge it.

@IvanGoncharov
Copy link
Member Author

Oh wait, you left the test changes in this PR. This looks good to me if you want to fix any conflicts and merge it.

This PR adds test cases that reproducess bug in current implement that prevents NaN and undefined from being used as enum values.
It's not the most critical issue but fixing it involves breaking change to valueFromAST function (throw instead of returning undefined) so I will try fix it on weekends so we can include it in 14.0.0.

@IvanGoncharov
Copy link
Member Author

@mjmahone I agree that this PR looks confusing so will extract failing test case into separate one with proper description.

@IvanGoncharov IvanGoncharov merged commit 85971a2 into graphql:master Jun 5, 2018
@IvanGoncharov IvanGoncharov deleted the enumCoercion branch June 5, 2018 05:44
IvanGoncharov added a commit to IvanGoncharov/graphql-js that referenced this pull request Jun 5, 2018
**Extracted from graphql#1267**

From graphql#836 it looks like both `undefined` and `NaN` could be used as an enum values:
```js
const TestEnum = new GraphQLEnumType({
  name: 'TestEnum',
  values: {
    UNDEFINED: { value: undefined },
    NAN: { value: NaN },
  },
});
```
And run this query:
```graphql
{
  undefined: fieldWithEnumInput(input: UNDEFINED)
  NaN: fieldWithEnumInput(input: NAN)
}
```
It will result in:
```json
{
  "data": {
    "undefined": null,
    "NaN": null
  },
  "errors": [
    {
      "message": "Argument \"input\" has invalid value UNDEFINED.",
      "path": [ "undefined" ]
    }
    {
      "message": "Argument \"input\" has invalid value NAN.",
      "path": [ "NaN" ]
    }
  ]
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants