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

[7392] fix conformance test in PHP JSON parser #16743

Closed
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
10 changes: 0 additions & 10 deletions conformance/failure_list_php.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@ Recommended.Proto2.JsonInput.FieldNameExtension.Validator
Recommended.Proto3.JsonInput.BytesFieldBase64Url.JsonOutput
Recommended.Proto3.JsonInput.BytesFieldBase64Url.ProtobufOutput
Recommended.Proto3.JsonInput.FieldMaskInvalidCharacter
Recommended.Proto3.JsonInput.IgnoreUnknownEnumStringValueInMapValue.ProtobufOutput
Recommended.Proto3.JsonInput.IgnoreUnknownEnumStringValueInRepeatedField.ProtobufOutput
Recommended.Editions_Proto2.JsonInput.IgnoreUnknownEnumStringValueInRepeatedPart.ProtobufOutput
Recommended.Editions_Proto3.JsonInput.IgnoreUnknownEnumStringValueInRepeatedPart.ProtobufOutput
Recommended.Proto2.JsonInput.IgnoreUnknownEnumStringValueInRepeatedPart.ProtobufOutput
Recommended.Proto3.JsonInput.IgnoreUnknownEnumStringValueInRepeatedPart.ProtobufOutput
Recommended.Editions_Proto2.JsonInput.IgnoreUnknownEnumStringValueInMapPart.ProtobufOutput
Recommended.Editions_Proto3.JsonInput.IgnoreUnknownEnumStringValueInMapPart.ProtobufOutput
Recommended.Proto2.JsonInput.IgnoreUnknownEnumStringValueInMapPart.ProtobufOutput
Recommended.Proto3.JsonInput.IgnoreUnknownEnumStringValueInMapPart.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataOneofBinary.MESSAGE.Merge.ProtobufOutput
Required.Proto2.JsonInput.StoresDefaultPrimitive.Validator
Required.Proto3.JsonInput.DoubleFieldTooSmall
Expand Down
22 changes: 22 additions & 0 deletions php/src/Google/Protobuf/Internal/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -1255,6 +1255,18 @@ private function mergeFromArrayJsonImpl($array, $ignore_unknown)
$tmp_value,
$value_field,
$ignore_unknown);

// Mapped unknown enum string values should be silently
// ignored if ignore_unknown is set.
if ($value_field->getType() == GPBType::ENUM &&
is_string($tmp_value) &&
is_null(
$value_field->getEnumType()->getValueByName($tmp_value)
) &&
$ignore_unknown) {
continue;
}

self::kvUpdateHelper($field, $proto_key, $proto_value);
}
} else if ($field->isRepeated()) {
Expand All @@ -1270,6 +1282,16 @@ private function mergeFromArrayJsonImpl($array, $ignore_unknown)
$tmp,
$field,
$ignore_unknown);

// Repeated unknown enum string values should be silently
// ignored if ignore_unknown is set.
if ($field->getType() == GPBType::ENUM &&
is_string($tmp) &&
is_null($field->getEnumType()->getValueByName($tmp)) &&
$ignore_unknown) {
continue;
}

self::appendHelper($field, $proto_value);
}
} else {
Expand Down
50 changes: 50 additions & 0 deletions php/tests/EncodeDecodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,56 @@ public function testDecodeRepeatedStringValue()
$this->assertSame("a", $m->getRepeatedField()[0]->getValue());
}

public function testDecodeEnumMapWithUnknownStringValueThrows()
{
$this->expectException(Exception::class);

$m = new TestMessage();
$m->mergeFromJsonString("{\"map_int32_enum\":{\"1\": \"UNKNOWN_ENUM\"}}");
}

public function testDecodeEnumMapWithUnknownStringValueIgnored()
{
$m = new TestMessage();
$m->mergeFromJsonString(
"{\"map_int32_enum\":{
\"1\": \"ONE\",
\"2\": \"UNKNOWN_ENUM\",
\"3\": \"ZERO\"
}}",
true
);

$this->assertSame(TestEnum::ONE, $m->getMapInt32Enum()["1"]);
$this->assertSame(TestEnum::ZERO, $m->getMapInt32Enum()["3"]);
$this->assertFalse($m->getMapInt32Enum()->offsetExists(2));
}

public function testDecodeRepeatedEnumWithUnknownStringValueThrows()
{
$this->expectException(Exception::class);

$m = new TestMessage();
$m->mergeFromJsonString("{\"repeated_enum\":[\"UNKNOWN_ENUM\"]}");
}

public function testDecodeRepeatedEnumWithUnknownStringValueIgnored()
{
$m = new TestMessage();
$m->mergeFromJsonString(
"{\"repeated_enum\":[
\"ONE\",
\"UNKNOWN_ENUM\",
\"ZERO\"
]}",
Comment on lines +245 to +249
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we using values of ONE and ZERO in some tests, and using ONLY the UNKNOWN_ENUM in others? Let's keep this consistent, or test using both if that's what's desired. Otherwise, changing the JSON string input betwen tests is confusing (as what we are actually testing is the second parameter being true).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch! I now made the JSON string inputs equal in each test pair.

true
);

$this->assertSame(2, count($m->getRepeatedEnum()));
$this->assertSame(TestEnum::ONE, $m->getRepeatedEnum()[0]);
$this->assertSame(TestEnum::ZERO, $m->getRepeatedEnum()[1]);
}

public function testDecodeMapStringValue()
{
$m = new TestStringValue();
Expand Down
Loading