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

Default values should also be serialized for json map key/value #5643

Merged
merged 1 commit into from
Jan 28, 2019
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
1 change: 0 additions & 1 deletion conformance/failure_list_php_c.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Required.DurationProtoInputTooLarge.JsonOutput
Required.DurationProtoInputTooSmall.JsonOutput
Required.TimestampProtoInputTooLarge.JsonOutput
Required.TimestampProtoInputTooSmall.JsonOutput
Required.Proto3.JsonInput.BoolMapField.JsonOutput
Required.Proto3.JsonInput.DoubleFieldMaxNegativeValue.JsonOutput
Required.Proto3.JsonInput.DoubleFieldMaxNegativeValue.ProtobufOutput
Required.Proto3.JsonInput.DoubleFieldMinPositiveValue.JsonOutput
Expand Down
5 changes: 2 additions & 3 deletions php/ext/google/protobuf/encode_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,7 @@ static void put_optional_value(const void* memory, int len,
#define T(upbtypeconst, upbtype, ctype, default_value) \
case upbtypeconst: { \
ctype value = DEREF(memory, 0, ctype); \
if (value != default_value) { \
if (is_json || value != default_value) { \
upb_selector_t sel = getsel(f, upb_handlers_getprimitivehandlertype(f)); \
upb_sink_put##upbtype(sink, sel, value); \
} \
Expand All @@ -1189,8 +1189,7 @@ static void put_optional_value(const void* memory, int len,
#undef T
case UPB_TYPE_STRING:
case UPB_TYPE_BYTES:
putrawstr(memory, len, f, sink,
is_json && is_wrapper_msg(upb_fielddef_containingtype(f)));
putrawstr(memory, len, f, sink, is_json);
break;
case UPB_TYPE_MESSAGE: {
#if PHP_MAJOR_VERSION < 7
Expand Down
13 changes: 13 additions & 0 deletions php/tests/encode_decode_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1135,4 +1135,17 @@ public function testDecodeEmptyFieldMask()
$this->assertEquals("", $m->serializeToString());
}

public function testJsonDecodeMapWithDefaultValueKey()
{
$m = new TestMessage();
$m->getMapInt32Int32()[0] = 0;
$this->assertSame("{\"mapInt32Int32\":{\"0\":0}}",
$m->serializeToJsonString());

$m = new TestMessage();
$m->getMapStringString()[""] = "";
$this->assertSame("{\"mapStringString\":{\"\":\"\"}}",
$m->serializeToJsonString());
}

}