Skip to content

Commit 4629255

Browse files
committed
[php][ext] Support NULL values conversion
* Handle NULL values in the PHP extension's conversion functions. * Tests that verify the behavior of converting supported types. Should fix : protocolbuffers#20000
1 parent 1a58e5d commit 4629255

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

php/ext/google/protobuf/convert.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ static bool to_double(zval* php_val, double* dbl) {
281281

282282
static bool to_bool(zval* from, bool* to) {
283283
switch (Z_TYPE_P(from)) {
284+
case IS_NULL:
285+
*to = false;
286+
return true;
284287
case IS_TRUE:
285288
*to = true;
286289
return true;
@@ -315,6 +318,9 @@ static bool to_string(zval* from) {
315318
switch (Z_TYPE_P(from)) {
316319
case IS_STRING:
317320
return true;
321+
case IS_NULL:
322+
ZVAL_EMPTY_STRING(from);
323+
return true;
318324
case IS_TRUE:
319325
case IS_FALSE:
320326
case IS_LONG:

php/tests/GeneratedClassTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,63 @@ public function testOptionalInt32Field()
209209
$this->assertSame(0, $m->getTrueOptionalInt32());
210210
}
211211

212+
#########################################################
213+
# Test values that are converted by setter
214+
#########################################################
215+
216+
public function testConvertValueSetter()
217+
{
218+
// convert null
219+
$m_null = new TestMessage();
220+
221+
$m_null->setOptionalBool(null);
222+
$m_null->setOptionalBytes(null);
223+
$m_null->setOptionalString(null);
224+
$m_null->setTrueOptionalBool(null);
225+
$m_null->setTrueOptionalBytes(null);
226+
$m_null->setTrueOptionalString(null);
227+
// allow null message
228+
$m_null->setOptionalMessage(null);
229+
$m_null->setTrueOptionalMessage(null);
230+
231+
$this->assertSame(false, $m_null->getOptionalBool());
232+
$this->assertSame('', $m_null->getOptionalString());
233+
$this->assertSame('', $m_null->getOptionalBytes());
234+
$this->assertNull($m_null->getOptionalMessage());
235+
236+
$this->assertSame(false, $m_null->getTrueOptionalBool());
237+
$this->assertSame('', $m_null->getTrueOptionalString());
238+
$this->assertSame('', $m_null->getTrueOptionalBytes());
239+
$this->assertNull($m_null->getTrueOptionalMessage());
240+
241+
// Convert int
242+
$m_number = new TestMessage();
243+
244+
$m_number->setOptionalBool(0);
245+
$m_number->setOptionalBytes(0);
246+
$m_number->setOptionalString(0);
247+
$m_number->setTrueOptionalBool(1);
248+
$m_number->setTrueOptionalBytes(1);
249+
$m_number->setTrueOptionalString(1);
250+
251+
$this->assertSame(false, $m_number->getOptionalBool());
252+
$this->assertSame('0', $m_number->getOptionalString());
253+
$this->assertSame('0', $m_number->getOptionalBytes());
254+
255+
$this->assertSame(true, $m_number->getTrueOptionalBool());
256+
$this->assertSame('1', $m_number->getTrueOptionalString());
257+
$this->assertSame('1', $m_number->getTrueOptionalBytes());
258+
259+
// Convert str
260+
$m_number_str = new TestMessage();
261+
262+
$m_number_str->setOptionalBool('');
263+
$m_number_str->setTrueOptionalBool('STR');
264+
265+
$this->assertSame(false, $m_number_str->getOptionalBool());
266+
$this->assertSame(true, $m_number_str->getTrueOptionalBool());
267+
}
268+
212269
#########################################################
213270
# Test uint32 field.
214271
#########################################################

0 commit comments

Comments
 (0)