Skip to content

Commit 919bd7d

Browse files
committed
Disallow random properties
1 parent 74451ed commit 919bd7d

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/Components/Components/src/ElementReference.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,21 @@ public override ElementReference Read(ref Utf8JsonReader reader, Type typeToConv
6464
string id = null;
6565
while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
6666
{
67-
if (reader.ValueTextEquals(IdProperty.EncodedUtf8Bytes))
67+
if (reader.TokenType == JsonTokenType.PropertyName)
6868
{
69-
reader.Read();
70-
id = reader.GetString();
69+
if (reader.ValueTextEquals(IdProperty.EncodedUtf8Bytes))
70+
{
71+
reader.Read();
72+
id = reader.GetString();
73+
}
74+
else
75+
{
76+
throw new JsonException($"Unexpected JSON property '{reader.GetString()}'.");
77+
}
78+
}
79+
else
80+
{
81+
throw new JsonException($"Unexcepted JSON Token {reader.TokenType}.");
7182
}
7283
}
7384

src/Components/Components/test/ElementReferenceTest.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,27 @@ public void Deserializing_WithFormatting_Works()
5454
}
5555

5656
[Fact]
57-
public void Deserializing_Throws_IfIdIsNotSpecified()
57+
public void Deserializing_Throws_IfUnknownPropertyAppears()
5858
{
5959
// Arrange
6060
var json = "{\"id\":\"some-value\"}";
6161

6262
// Act
6363
var ex = Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<ElementReference>(json, JsonSerializerOptionsProvider.Options));
6464

65+
// Assert
66+
Assert.Equal("Unexpected JSON property 'id'.", ex.Message);
67+
}
68+
69+
[Fact]
70+
public void Deserializing_Throws_IfIdIsNotSpecified()
71+
{
72+
// Arrange
73+
var json = "{}";
74+
75+
// Act
76+
var ex = Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<ElementReference>(json, JsonSerializerOptionsProvider.Options));
77+
6578
// Assert
6679
Assert.Equal("__internalId is required.", ex.Message);
6780
}

0 commit comments

Comments
 (0)