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 Extension - Cannot convert optional to string #20000

Open
FabioBatSilva opened this issue Jan 14, 2025 · 1 comment
Open

PHP Extension - Cannot convert optional to string #20000

FabioBatSilva opened this issue Jan 14, 2025 · 1 comment
Assignees

Comments

@FabioBatSilva
Copy link

We are trying to migrate an application from the PHP Protobuf implementation to the C extension.
And found some inconsistency on the optional behaviour.

With the php implementation optional string values are converted to a empty string ''.
With the C extension a PHP Fatal error is triggered.

PHP Fatal error:  Uncaught Exception: Cannot convert '' to string

This is what the setter looks like for a string optional.

public function setTrueOptionalString($var)
{
    GPBUtil::checkString($var, True);
    $this->true_optional_string = $var;

    return $this;
}

The php implementation of GPBUtil::checkString the value is passed as a reference and converted from null to string(0) ""
But the C implementation of GPBUtil::checkString is empty..
So null is never converted to string before assign the message value and triggering the fatal error when the value is converted to_string


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

What did you do?

<?php
# optional_test.php
require_once('../vendor/autoload.php');
require_once('test_util.php');

use Foo\TestMessage;

$m = new TestMessage([]);

$m->setTrueOptionalString(null);

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

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

What did you expect to see

string(0) ""

What did you see instead?

PHP Fatal error:  Uncaught Exception: Cannot convert '' to string
@FabioBatSilva FabioBatSilva added the untriaged auto added to all issues by default when created. label Jan 14, 2025
@FabioBatSilva FabioBatSilva changed the title Cannot convert '' to string PHP Extension Cannot convert optional to string Jan 14, 2025
@esrauchg
Copy link

Thanks for the report. I'm following up internally about what the expected behavior is in this case.

@esrauchg esrauchg self-assigned this Jan 15, 2025
@esrauchg esrauchg removed the untriaged auto added to all issues by default when created. label Jan 15, 2025
@FabioBatSilva FabioBatSilva changed the title PHP Extension Cannot convert optional to string PHP Extension - Cannot convert optional to string Jan 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants