Skip to content

Boolean scalars accept strings outside true and false (unlike literals) #1103

Closed

Description

@tipok

I found an issue for Boolean typed scalars in

private Boolean convertImpl(Object input) {

As of GraphQL specs:
http://facebook.github.io/graphql/June2018/#sec-Boolean

Input Coercion

When expected as an input type, only boolean input values are accepted. All other input values must > raise a query error indicating an incorrect type.

The problem here:
If the value is a string the call Boolean.parseBoolean((String)input) returns a false if the string is not containing "true".

Activity

  1. bbakerman commented on Jun 30, 2018

    @bbakerman
    Member

    The trick here is the esoteric argument about "what is a boolean input type"

    Boolean.parseBoolean is a java idiomatic way to get a Boolean value. And yes it takes Strings.

    The reference graphql-js (which we tend to go to when the spec is in question) uses JavaScript Boolean parsing

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean

    The value passed as the first parameter is converted to a boolean value, if necessary. If the value is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false. All other values, including any object or the string "false", create an object with an initial value of true.

    So they accept strings / numbers and in fact any object with an idiomatic JavaScript approach.

    So I dont think the current approach is incorrect.

    But if you could explain more about the impact you are seeing and the inputs you think are incorrect we can discuss it

  2. tipok commented on Jun 30, 2018

    @tipok
    ContributorAuthor

    I've interpreted the spec this way:

    If it's true of type boolean in JSON it's correct but if it's "true" of type string in JSON it should fail because of type mismatch.

    Why i did so:

    The difference here between input and output in the spec. For output conversion is fine if no information is lost (this is really non specific), but for input the types have to match.

    The JS Boolean(x) and Boolean.parseBoolean(x) in Java are really different in their behaviour.
    In JS Boolean("false") will always return true because the string isn't empty, but in Java Boolean.parseBoolean("false") will be always false.

    Now to our test:

    We do have a mutation:

    GraphLQ:

    mutation UpdateObject($b: Bool!, $s: String!) {
      update(b: $b, s: $s) {
        id
        b
        s
      }
    }

    Variables:

    {
    	"b": "hello",
    	"s": "hello again"
    }

    The result is an update which is interpreted as:

    {
    	"b": false,
    	"s": "hello again"
    }
  3. changed the title [-]Mutations with non matching type variables handled wrong[/-] [+]Mutations with non matching Boolean type variables handled wrong[/+] on Jul 24, 2018
  4. bbakerman commented on Jul 26, 2018

    @bbakerman
    Member

    I am inclined to leave the behaviour as it is. That is a value for input to a boolean uses idiomatic Java boolean-ness just like the JS reference implementation uses idiomatic JS boolean-ess.

    @andimarek - thoughts?

  5. neelkamath commented on Jun 23, 2021

    @neelkamath

    @bbakerman I ran into this bug recently, and it took me a while to figure out what was going on. For example, the following query causes isInvitable to be parsed as false instead of throwing an error. I personally feel we should change this behavior for the reasons tipok mentioned.

    2021-06-23 02:52:16.340 [eventLoopGroupProxy-7-1] DEBUG notprivacysafe.graphql.GraphQL - Executing request. operation name: 'null'. query: '
                mutation SetPublicity($chatId: Int!, $isInvitable: Boolean!) {
                  setPublicity(chatId: $chatId, isInvitable: $isInvitable) {
                    ... on InvalidChatId {
                      __typename
                    }
                  }
                }
              '. variables '{chatId=7, isInvitable=INVITABLE}'
    
  6. changed the title [-]Mutations with non matching Boolean type variables handled wrong[/-] [+]Boolean scalars accept strings outside `true` and `false` (unlike literals)[/+] on Jul 3, 2021
  7. added this to the 17.0 milestone on Jul 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions