浏览代码

Fix empty FieldMask json encoding/decoding (#5605)

* Fix empty FieldMask json encoding/decoding

* Add failed test to python's conformance failure list
Paul Yang 6 年之前
父节点
当前提交
7f42d6d0bc

+ 4 - 0
conformance/binary_json_conformance_suite.cc

@@ -2042,6 +2042,10 @@ void BinaryAndJsonConformanceSuite::RunSuiteImpl() {
       "FieldMask", REQUIRED,
       "FieldMask", REQUIRED,
       R"({"optionalFieldMask": "foo,barBaz"})",
       R"({"optionalFieldMask": "foo,barBaz"})",
       R"(optional_field_mask: {paths: "foo" paths: "bar_baz"})");
       R"(optional_field_mask: {paths: "foo" paths: "bar_baz"})");
+  RunValidJsonTest(
+      "EmptyFieldMask", REQUIRED,
+      R"({"optionalFieldMask": ""})",
+      R"(optional_field_mask: {})");
   ExpectParseFailureForJson(
   ExpectParseFailureForJson(
       "FieldMaskInvalidCharacter", RECOMMENDED,
       "FieldMaskInvalidCharacter", RECOMMENDED,
       R"({"optionalFieldMask": "foo,bar_bar"})");
       R"({"optionalFieldMask": "foo,bar_bar"})");

+ 1 - 0
conformance/failure_list_python.txt

@@ -19,3 +19,4 @@ Required.Proto3.ProtobufInput.IllegalZeroFieldNum_Case_0
 Required.Proto3.ProtobufInput.IllegalZeroFieldNum_Case_1
 Required.Proto3.ProtobufInput.IllegalZeroFieldNum_Case_1
 Required.Proto3.ProtobufInput.IllegalZeroFieldNum_Case_2
 Required.Proto3.ProtobufInput.IllegalZeroFieldNum_Case_2
 Required.Proto3.ProtobufInput.IllegalZeroFieldNum_Case_3
 Required.Proto3.ProtobufInput.IllegalZeroFieldNum_Case_3
+Required.Proto3.JsonInput.EmptyFieldMask.ProtobufOutput

+ 1 - 0
conformance/failure_list_python_cpp.txt

@@ -20,3 +20,4 @@ Required.Proto3.JsonInput.FloatFieldTooLarge
 Required.Proto3.JsonInput.FloatFieldTooSmall
 Required.Proto3.JsonInput.FloatFieldTooSmall
 Required.Proto3.JsonInput.RepeatedFieldWrongElementTypeExpectingIntegersGotBool
 Required.Proto3.JsonInput.RepeatedFieldWrongElementTypeExpectingIntegersGotBool
 Required.Proto3.JsonInput.TimestampJsonInputLowercaseT
 Required.Proto3.JsonInput.TimestampJsonInputLowercaseT
+Required.Proto3.JsonInput.EmptyFieldMask.ProtobufOutput

文件差异内容过多而无法显示
+ 267 - 863
php/ext/google/protobuf/upb.c


+ 4 - 1
php/src/Google/Protobuf/Internal/GPBUtil.php

@@ -518,8 +518,11 @@ class GPBUtil
 
 
     public static function parseFieldMask($paths_string)
     public static function parseFieldMask($paths_string)
     {
     {
-        $path_strings = explode(",", $paths_string);
         $field_mask = new FieldMask();
         $field_mask = new FieldMask();
+        if (strlen($paths_string) === 0) {
+            return $field_mask;
+        }
+        $path_strings = explode(",", $paths_string);
         $paths = $field_mask->getPaths();
         $paths = $field_mask->getPaths();
         foreach($path_strings as &$path_string) {
         foreach($path_strings as &$path_string) {
             $field_strings = explode(".", $path_string);
             $field_strings = explode(".", $path_string);

+ 7 - 0
php/tests/encode_decode_test.php

@@ -1128,4 +1128,11 @@ class EncodeDecodeTest extends TestBase
         $this->assertSame("\"foo.barBaz,qux\"", $m->serializeToJsonString());
         $this->assertSame("\"foo.barBaz,qux\"", $m->serializeToJsonString());
     }
     }
 
 
+    public function testDecodeEmptyFieldMask()
+    {
+        $m = new FieldMask();
+        $m->mergeFromJsonString("\"\"");
+        $this->assertEquals("", $m->serializeToString());
+    }
+
 }
 }

部分文件因为文件数量过多而无法显示