Commit 888e71bd authored by Jon Skeet's avatar Jon Skeet

Prohibit null values in repeated and map fields in JSON

parent 1a34ac03
...@@ -170,6 +170,36 @@ namespace Google.Protobuf ...@@ -170,6 +170,36 @@ namespace Google.Protobuf
AssertRoundtrip(message); AssertRoundtrip(message);
} }
[Test]
public void RepeatedField_NullElementProhibited()
{
string json = "{ \"repeated_foreign_message\": [null] }";
Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
}
[Test]
public void RepeatedField_NullOverallValueAllowed()
{
string json = "{ \"repeated_foreign_message\": null }";
Assert.AreEqual(new TestAllTypes(), TestAllTypes.Parser.ParseJson(json));
}
[Test]
[TestCase("{ \"mapInt32Int32\": { \"10\": null }")]
[TestCase("{ \"mapStringString\": { \"abc\": null }")]
[TestCase("{ \"mapInt32ForeignMessage\": { \"10\": null }")]
public void MapField_NullValueProhibited(string json)
{
Assert.Throws<InvalidProtocolBufferException>(() => TestMap.Parser.ParseJson(json));
}
[Test]
public void MapField_NullOverallValueAllowed()
{
string json = "{ \"mapInt32Int32\": null }";
Assert.AreEqual(new TestMap(), TestMap.Parser.ParseJson(json));
}
[Test] [Test]
public void IndividualWrapperTypes() public void IndividualWrapperTypes()
{ {
......
...@@ -239,6 +239,10 @@ namespace Google.Protobuf ...@@ -239,6 +239,10 @@ namespace Google.Protobuf
return; return;
} }
tokenizer.PushBack(token); tokenizer.PushBack(token);
if (token.Type == JsonToken.TokenType.Null)
{
throw new InvalidProtocolBufferException("Repeated field elements cannot be null");
}
list.Add(ParseSingleValue(field, tokenizer)); list.Add(ParseSingleValue(field, tokenizer));
} }
} }
...@@ -270,7 +274,10 @@ namespace Google.Protobuf ...@@ -270,7 +274,10 @@ namespace Google.Protobuf
} }
object key = ParseMapKey(keyField, token.StringValue); object key = ParseMapKey(keyField, token.StringValue);
object value = ParseSingleValue(valueField, tokenizer); object value = ParseSingleValue(valueField, tokenizer);
// TODO: Null handling if (value == null)
{
throw new InvalidProtocolBufferException("Map values must not be null");
}
dictionary[key] = value; dictionary[key] = value;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment