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

PHP - Inconsistent mergeFromJsonString behavior #20042

Open
FabioBatSilva opened this issue Jan 20, 2025 · 0 comments
Open

PHP - Inconsistent mergeFromJsonString behavior #20042

FabioBatSilva opened this issue Jan 20, 2025 · 0 comments
Labels
untriaged auto added to all issues by default when created.

Comments

@FabioBatSilva
Copy link

FabioBatSilva commented Jan 20, 2025

The PHP C extension implementation native PHP implementation have different behavior when deserializing a empty JSON object/array.

The native php implementation threats both '{}' and [] as valid inputs.
And the C extension allow only {} (I believe that is the correct behaviour)

This happens BC In php the function json_encode will encode a empty associative array as []

php -r "var_dump(json_encode(array_filter(['foo' => null])));"
string(2) "[]"

And we also get back the same php value when decoding both {} and []:

$php -r "var_dump(json_decode('[]', true, 512, JSON_BIGINT_AS_STRING));"
array(0) {
}

$ php -r "var_dump(json_decode('{}', true, 512, JSON_BIGINT_AS_STRING));"
array(0) {
}

PHP Implementation :

$array = json_decode($input->getData(), true, 512, JSON_BIGINT_AS_STRING);


What version of protobuf and what language are you using?
Version: main
Language: php
OS: Linux

What did you do?

<?php

require_once (dirname(__DIR__) . '/vendor/autoload.php');
require_once (__DIR__ . '/test_util.php');

use Foo\TestMessage;

$message = new TestMessage();
$values = array_filter([
    'true_optional_string' => null,
]);

$message->mergeFromJsonString(json_encode($values));
//$message->mergeFromJsonString('[]');

var_dump($message->hasTrueOptionalString());
#### Compile the extension 
./php/tests/compile_extension.sh

# Run script
php -d display_errors=on -dextension=../ext/google/protobuf/modules/protobuf.so json_encode_test.php

What did you expect to see ?

Error parsing JSON

What did you see instead?
C extension:

Fatal error: Uncaught Exception: Error occurred during parsing: Error parsing JSON @1:0: Expected: '{'

PHP implementation:

bool(false)
@FabioBatSilva FabioBatSilva added the untriaged auto added to all issues by default when created. label Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
untriaged auto added to all issues by default when created.
Projects
None yet
Development

No branches or pull requests

1 participant