aboutsummaryrefslogtreecommitdiffhomepage
path: root/csharp/src/Google.Protobuf/JsonParser.cs
diff options
context:
space:
mode:
authorGravatar Jon Skeet <jonskeet@google.com>2016-01-15 13:54:17 +0000
committerGravatar Jon Skeet <jonskeet@google.com>2016-01-15 13:54:17 +0000
commit8866d6a80ed30e1ff32f1c5420f1a803c26fa13a (patch)
treea8022daaeddffe7bec332e14538b1cbff490b312 /csharp/src/Google.Protobuf/JsonParser.cs
parent52db5139c4f122bbd34ef34b4103d0b650e7c218 (diff)
Reject JSON containing the same oneof field twice
Diffstat (limited to 'csharp/src/Google.Protobuf/JsonParser.cs')
-rw-r--r--csharp/src/Google.Protobuf/JsonParser.cs15
1 files changed, 15 insertions, 0 deletions
diff --git a/csharp/src/Google.Protobuf/JsonParser.cs b/csharp/src/Google.Protobuf/JsonParser.cs
index 92029e06..b1a24800 100644
--- a/csharp/src/Google.Protobuf/JsonParser.cs
+++ b/csharp/src/Google.Protobuf/JsonParser.cs
@@ -168,6 +168,10 @@ namespace Google.Protobuf
}
var descriptor = message.Descriptor;
var jsonFieldMap = descriptor.Fields.ByJsonName();
+ // All the oneof fields we've already accounted for - we can only see each of them once.
+ // The set is created lazily to avoid the overhead of creating a set for every message
+ // we parsed, when oneofs are relatively rare.
+ HashSet<OneofDescriptor> seenOneofs = null;
while (true)
{
token = tokenizer.Next();
@@ -183,6 +187,17 @@ namespace Google.Protobuf
FieldDescriptor field;
if (jsonFieldMap.TryGetValue(name, out field))
{
+ if (field.ContainingOneof != null)
+ {
+ if (seenOneofs == null)
+ {
+ seenOneofs = new HashSet<OneofDescriptor>();
+ }
+ if (!seenOneofs.Add(field.ContainingOneof))
+ {
+ throw new InvalidProtocolBufferException($"Multiple values specified for oneof {field.ContainingOneof.Name}");
+ }
+ }
MergeField(message, field, tokenizer);
}
else