Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Reflection/Reflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private function doGetMessage(object $message, ?\Closure $presence = null): Mess
if ($property->attributes->has(Field::class)) {
$field = $property->attributes->get(Field::class);

if ($presence($field, $value)) {
if (($property->default !== null && $property->default->value === null) || $presence($field, $value)) {
$descriptors[] = Protobuf\fieldOf(
$field->num,
$field->type->accept($this->valueVisitor)($value),
Expand Down
22 changes: 22 additions & 0 deletions tests/Encoder/Internal/ReflectionEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ public function testEncode(): void

self::assertSame('0a084a6f686e20446f651032', bin2hex($encoder->encode(new Request('John Doe', 50))));
}

public function testEncodeOptionalScalarZeroValues(): void
{
$encoder = Builder::buildDefault();

self::assertSame(
'080012001800',
bin2hex($encoder->encode(new OptionalScalarRequest(0, '', false))),
);
}
}

final readonly class Request
Expand All @@ -32,3 +42,15 @@ public function __construct(
public int $id,
) {}
}

final readonly class OptionalScalarRequest
{
public function __construct(
#[Reflection\Field(1, Reflection\Int32T::T)]
public ?int $oneofIndex = null,
#[Reflection\Field(2, Reflection\StringT::T)]
public ?string $label = null,
#[Reflection\Field(3, Reflection\BoolT::T)]
public ?bool $enabled = null,
) {}
}