neo4j-php / neo4j-php-client

Php client and driver for neo4j database
https://neo4j.com/developer/php/
MIT License
163 stars 40 forks source link

Weird behavior on empty arrays #43

Closed giudicelli closed 3 years ago

giudicelli commented 3 years ago

Hello, I'm using version 1.3.1.

When runing a simple update with an empty array field, the client transforms an empty array into a stdClass. Which gets rejected with the following message: "Property values can only be of primitive types or arrays thereof"

Query is: MATCH (from:Dog), (to:Dog) WHERE id(from) = $from_id AND id(to) = $to_id CREATE (from)-[breeded:BREEDED]->(to) SET breeded += $breeded_properties RETURN id(breeded) AS id

Parameters are: [ 'from_id' => 1, 'to_id' => 2, 'breeded_properties' => [ 'issues' => [] ] ]

The issue comes from: ParameterHelper::emptyDictionaryToStdClass `

    if (($value instanceof Map && $value->count() === 0) ||
        (is_array($value) && count($value) === 0)
    ) {
        return new stdClass();
    }
    return null;

`

Why would an empty array be transformed into a stdClass and not kept as an array?

transistive commented 3 years ago

Hello Giudicelli,

Thank you for raising this issue. This was a conscious design decision I had to make when developing the library. It is explained by example here.

It is because a php array can either represent a map or list in neo4j. When the array is filled it is easy to guess: 0 indexed arrays are lists and string indexed arrays are objects. The question arises: what does an empty array represent?

Thus, the ParameterHelper was born. Using ParamaterHelper::asList([]) makes the intent explicit and is guaranteed to work.

That being said: I think you have a point. An empty array shouldn't default to an empty map, but to an empty list. I will fix this :+1:

Have a nice sunday!

transistive commented 3 years ago

This behaviour is now enforced in version 1.3.2 :)

giudicelli commented 3 years ago

Thanks :)