Disallow random properties
This commit is contained in:
parent
74451ed348
commit
919bd7db06
|
|
@ -64,10 +64,21 @@ namespace Microsoft.AspNetCore.Components
|
||||||
string id = null;
|
string id = null;
|
||||||
while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
|
while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
|
||||||
{
|
{
|
||||||
if (reader.ValueTextEquals(IdProperty.EncodedUtf8Bytes))
|
if (reader.TokenType == JsonTokenType.PropertyName)
|
||||||
{
|
{
|
||||||
reader.Read();
|
if (reader.ValueTextEquals(IdProperty.EncodedUtf8Bytes))
|
||||||
id = reader.GetString();
|
{
|
||||||
|
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]
|
[Fact]
|
||||||
public void Deserializing_Throws_IfIdIsNotSpecified()
|
public void Deserializing_Throws_IfUnknownPropertyAppears()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var json = "{\"id\":\"some-value\"}";
|
var json = "{\"id\":\"some-value\"}";
|
||||||
|
|
@ -62,6 +62,19 @@ namespace Microsoft.AspNetCore.Components
|
||||||
// Act
|
// Act
|
||||||
var ex = Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<ElementReference>(json, JsonSerializerOptionsProvider.Options));
|
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
|
||||||
Assert.Equal("__internalId is required.", ex.Message);
|
Assert.Equal("__internalId is required.", ex.Message);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue