Disallow random properties

This commit is contained in:
Pranav K 2019-07-17 15:23:24 -07:00
parent 74451ed348
commit 919bd7db06
No known key found for this signature in database
GPG Key ID: 1963DA6D96C3057A
2 changed files with 28 additions and 4 deletions

View File

@ -64,10 +64,21 @@ namespace Microsoft.AspNetCore.Components
string id = null;
while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
{
if (reader.ValueTextEquals(IdProperty.EncodedUtf8Bytes))
if (reader.TokenType == JsonTokenType.PropertyName)
{
reader.Read();
id = reader.GetString();
if (reader.ValueTextEquals(IdProperty.EncodedUtf8Bytes))
{
reader.Read();
id = reader.GetString();
}
else
{
throw new JsonException($"Unexpected JSON property '{reader.GetString()}'.");
}
}
else
{
throw new JsonException($"Unexcepted JSON Token {reader.TokenType}.");
}
}

View File

@ -54,7 +54,7 @@ namespace Microsoft.AspNetCore.Components
}
[Fact]
public void Deserializing_Throws_IfIdIsNotSpecified()
public void Deserializing_Throws_IfUnknownPropertyAppears()
{
// Arrange
var json = "{\"id\":\"some-value\"}";
@ -62,6 +62,19 @@ namespace Microsoft.AspNetCore.Components
// Act
var ex = Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<ElementReference>(json, JsonSerializerOptionsProvider.Options));
// Assert
Assert.Equal("Unexpected JSON property 'id'.", ex.Message);
}
[Fact]
public void Deserializing_Throws_IfIdIsNotSpecified()
{
// Arrange
var json = "{}";
// Act
var ex = Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<ElementReference>(json, JsonSerializerOptionsProvider.Options));
// Assert
Assert.Equal("__internalId is required.", ex.Message);
}