Disallow random properties
This commit is contained in:
parent
74451ed348
commit
919bd7db06
|
|
@ -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}.");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue