GraphQLBundle icon indicating copy to clipboard operation
GraphQLBundle copied to clipboard

`isset($args[$field])` returns true when the value is `null` contrary to a native PHP array

Open mathroc opened this issue 4 years ago • 1 comments

Q A
Bug report? not sure
Feature request? no
BC Break report? no
RFC? no
Version/Branch 0.13+

the Argument::offsetExists() method uses array_key_exists instead of isset on the wrapped array, this causes isset($args[$something]) to return true when the value is null. This is surprising because the behavior is different from a native PHP array

And it cause an issue with the Paginator::auto() method because if the client sends last: null it will take the backward path

My suggestion would be to change the Argument::offsetExists(), if that’s not wanted then Paginator::auto() should be handle this. (and maybe other places checking for nullable fields or fields having a default null value)

mathroc avatar Aug 23 '21 21:08 mathroc

This issue in the counterpart of #829 which was solved in #830.

It's a bit hard to make a decision on it, because even the native PHP implementation is inconsistent:

<?php

$arrayObject = new ArrayObject(array('argument'=>null));

echo json_encode([
    'offsetExists' => $arrayObject->offsetExists('argument'),
    'isset' => isset($arrayObject['argument']),
    'empty' => empty($arrayObject['argument']),
], 128);

Outputs:

{
    "offsetExists": true,
    "isset": false,
    "empty": true
}

Even though ArrayObject is supposed to be "a basic implementation" of it, the fact that it's inconsistent by itself demonstrates an internal PHP issue: offsetExists should correspond to isset() , but it is often implemented with array_key_exists, which is inconsistent...

Pierstoval avatar Apr 07 '25 12:04 Pierstoval