瀏覽代碼

JSON conformance test fixes

- Spot an Any without a type URL
- In the conformance test runner, catch exceptions due to generally-invalid JSON
Jon Skeet 9 年之前
父節點
當前提交
f2fe50bfc5

+ 4 - 0
csharp/src/Google.Protobuf.Conformance/Program.cs

@@ -101,6 +101,10 @@ namespace Google.Protobuf.Conformance
             {
             {
                 return new ConformanceResponse { ParseError = e.Message };
                 return new ConformanceResponse { ParseError = e.Message };
             }
             }
+            catch (InvalidJsonException e)
+            {
+                return new ConformanceResponse { ParseError = e.Message };
+            }
             switch (request.RequestedOutputFormat)
             switch (request.RequestedOutputFormat)
             {
             {
                 case global::Conformance.WireFormat.JSON:
                 case global::Conformance.WireFormat.JSON:

+ 7 - 0
csharp/src/Google.Protobuf.Test/JsonParserTest.cs

@@ -762,6 +762,13 @@ namespace Google.Protobuf
             Assert.Throws<InvalidOperationException>(() => Any.Parser.ParseJson(json));
             Assert.Throws<InvalidOperationException>(() => Any.Parser.ParseJson(json));
         }
         }
 
 
+        [Test]
+        public void Any_NoTypeUrl()
+        {
+            string json = "{ \"foo\": \"bar\" }";
+            Assert.Throws<InvalidProtocolBufferException>(() => Any.Parser.ParseJson(json));
+        }
+
         [Test]
         [Test]
         public void Any_WellKnownType()
         public void Any_WellKnownType()
         {
         {

+ 5 - 0
csharp/src/Google.Protobuf/JsonParser.cs

@@ -464,6 +464,11 @@ namespace Google.Protobuf
             {
             {
                 tokens.Add(token);
                 tokens.Add(token);
                 token = tokenizer.Next();
                 token = tokenizer.Next();
+
+                if (tokenizer.ObjectDepth < typeUrlObjectDepth)
+                {
+                    throw new InvalidProtocolBufferException("Any message with no @type");
+                }
             }
             }
 
 
             // Don't add the @type property or its value to the recorded token list
             // Don't add the @type property or its value to the recorded token list