+38
-4
lines changedFilter options
+38
-4
lines changed Original file line number Diff line number Diff line change
@@ -33,7 +33,4 @@ Recommended.Editions_Proto3.ValueRejectInfNumberValue.JsonOutput
33
33
Recommended.Editions_Proto3.ValueRejectNanNumberValue.JsonOutput
34
34
Required.Editions_Proto2.ProtobufInput.UnknownOrdering.ProtobufOutput
35
35
Required.Editions_Proto3.ProtobufInput.UnknownOrdering.ProtobufOutput
36
-
Required.*.JsonInput.AnyWithNoType.* # Failed to parse input or produce output.
37
-
Required.*.JsonInput.AnyWktRepresentationWithEmptyTypeAndValue # Should have failed to parse, but didn't.
38
-
Required.*.JsonInput.AnyWktRepresentationWithBadType # Should have failed to parse, but didn't.
39
36
Original file line number Diff line number Diff line change
@@ -591,6 +591,13 @@ public void AnyUnknownType()
591
591
Assert.Throws<InvalidOperationException>(() => JsonFormatter.Default.Format(any));
592
592
}
593
593
594
+
[Test]
595
+
public void AnyEmpty()
596
+
{
597
+
var any = new Any();
598
+
AssertJson("{ }", JsonFormatter.Default.Format(any));
599
+
}
600
+
594
601
[Test]
595
602
[TestCase(typeof(BoolValue), true, "true")]
596
603
[TestCase(typeof(Int32Value), 32, "32")]
Original file line number Diff line number Diff line change
@@ -840,7 +840,17 @@ public void Any_UnknownType()
840
840
[Test]
841
841
public void Any_NoTypeUrl()
842
842
{
843
+
// Currently if we're missing the type URL (@type) we return an empty Any message
844
+
// regardless of other fields. In the future we could potentially fail if there's no
845
+
// type URL but other fields are present.
843
846
string json = "{ \"foo\": \"bar\" }";
847
+
Assert.AreEqual(new Any(), Any.Parser.ParseJson(json));
848
+
}
849
+
850
+
[Test]
851
+
public void Any_BadTypeUrl()
852
+
{
853
+
string json = "{ \"@type\": \"no-slash\" }";
844
854
Assert.Throws<InvalidProtocolBufferException>(() => Any.Parser.ParseJson(json));
845
855
}
846
856
Original file line number Diff line number Diff line change
@@ -471,6 +471,13 @@ private void WriteAny(TextWriter writer, IMessage value, int indentationLevel) {
471
471
472
472
string typeUrl =
473
473
(string)value.Descriptor.Fields[Any.TypeUrlFieldNumber].Accessor.GetValue(value);
474
+
// If no type URL has been specified, just return an empty JSON object.
475
+
if (typeUrl == "") {
476
+
WriteBracketOpen(writer, ObjectOpenBracket);
477
+
WriteBracketClose(writer, ObjectCloseBracket, false, indentationLevel);
478
+
return;
479
+
}
480
+
474
481
ByteString data =
475
482
(ByteString)value.Descriptor.Fields[Any.ValueFieldNumber].Accessor.GetValue(value);
476
483
string typeName = Any.GetTypeName(typeUrl);
Original file line number Diff line number Diff line change
@@ -516,9 +516,13 @@ private void MergeAny(IMessage message, JsonTokenizer tokenizer)
516
516
tokens.Add(token);
517
517
token = tokenizer.Next();
518
518
519
+
// If we get to the end of the object and haven't seen a type URL, just return.
520
+
// (The message will be empty at this point.)
521
+
// We could potentially make this more conservative, failing if there are
522
+
// other properties but no type URL.
519
523
if (tokenizer.ObjectDepth < typeUrlObjectDepth)
520
524
{
521
-
throw new InvalidProtocolBufferException("Any message with no @type");
525
+
return;
522
526
}
523
527
}
524
528
@@ -530,6 +534,15 @@ private void MergeAny(IMessage message, JsonTokenizer tokenizer)
530
534
}
531
535
string typeUrl = token.StringValue;
532
536
string typeName = Any.GetTypeName(typeUrl);
537
+
// If we don't find a slash, GetTypeName returns an empty string. An empty string can
538
+
// never be a valid type name (whether that was through @type="", @type="blah" or
539
+
// @type="blah/") so we fail. This is InvalidProtocolBufferException rather than
540
+
// the InvalidOperationException used below, as it's the data that's invalid rather than
541
+
// the context in which we're parsing it.
542
+
if (typeName == "")
543
+
{
544
+
throw new InvalidProtocolBufferException("Invalid Any.@type value");
545
+
}
533
546
534
547
MessageDescriptor descriptor = settings.TypeRegistry.Find(typeName);
535
548
if (descriptor == null)
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4