Skip to content

[JsonSerializer] Verify null can't be assigned to non-nullable structs - #40446

Merged
layomia merged 1 commit into
dotnet:masterfrom
layomia:json_valuetype_null
Aug 7, 2020
Merged

[JsonSerializer] Verify null can't be assigned to non-nullable structs#40446
layomia merged 1 commit into
dotnet:masterfrom
layomia:json_valuetype_null

Conversation

@layomia

Copy link
Copy Markdown
Contributor

Closes#1037. Fixed during serializer refactoring - #2259.

We have additional tests for this scenario:

// Throw JsonException when null applied to types that can't be null. Behavior should align with properties deserialized with setters.
awaitAssert.ThrowsAsync<JsonException>(()=>Serializer.DeserializeWrapper<NullArgTester>(@"{""Point3DStruct"":null,""Int"":null,""ImmutableArray"":null}"));
awaitAssert.ThrowsAsync<JsonException>(()=>Serializer.DeserializeWrapper<NullArgTester_Mutable>(@"{""Point3DStruct"":null,""Int"":null,""ImmutableArray"":null}"));
awaitAssert.ThrowsAsync<JsonException>(()=>Serializer.DeserializeWrapper<NullArgTester>(@"{""Point3DStruct"":null}"));
awaitAssert.ThrowsAsync<JsonException>(()=>Serializer.DeserializeWrapper<NullArgTester_Mutable>(@"{""Point3DStruct"":null}"));
awaitAssert.ThrowsAsync<JsonException>(()=>Serializer.DeserializeWrapper<NullArgTester>(@"{""Int"":null}"));
awaitAssert.ThrowsAsync<JsonException>(()=>Serializer.DeserializeWrapper<NullArgTester_Mutable>(@"{""Int"":null}"));
awaitAssert.ThrowsAsync<JsonException>(()=>Serializer.DeserializeWrapper<NullArgTester>(@"{""ImmutableArray"":null}"));
awaitAssert.ThrowsAsync<JsonException>(()=>Serializer.DeserializeWrapper<NullArgTester_Mutable>(@"{""ImmutableArray"":null}"));
// Throw even when IgnoreNullValues is true for symmetry with property deserialization,
// until https://github.com/dotnet/runtime/issues/30795 is addressed.
varoptions=newJsonSerializerOptions{IgnoreNullValues=true};
awaitAssert.ThrowsAsync<JsonException>(()=>Serializer.DeserializeWrapper<NullArgTester>(@"{""Point3DStruct"":null,""Int"":null,""ImmutableArray"":null}",options));
awaitAssert.ThrowsAsync<JsonException>(()=>Serializer.DeserializeWrapper<NullArgTester_Mutable>(@"{""Point3DStruct"":null,""Int"":null,""ImmutableArray"":null}",options));
awaitAssert.ThrowsAsync<JsonException>(()=>Serializer.DeserializeWrapper<NullArgTester>(@"{""Point3DStruct"":null}",options));
awaitAssert.ThrowsAsync<JsonException>(()=>Serializer.DeserializeWrapper<NullArgTester_Mutable>(@"{""Point3DStruct"":null,""Int"":null,""ImmutableArray"":null}",options));
awaitAssert.ThrowsAsync<JsonException>(()=>Serializer.DeserializeWrapper<NullArgTester>(@"{""Int"":null}",options));
awaitAssert.ThrowsAsync<JsonException>(()=>Serializer.DeserializeWrapper<NullArgTester_Mutable>(@"{""Point3DStruct"":null,""Int"":null,""ImmutableArray"":null}",options));
awaitAssert.ThrowsAsync<JsonException>(()=>Serializer.DeserializeWrapper<NullArgTester>(@"{""ImmutableArray"":null}",options));
awaitAssert.ThrowsAsync<JsonException>(()=>Serializer.DeserializeWrapper<NullArgTester_Mutable>(@"{""Point3DStruct"":null,""Int"":null,""ImmutableArray"":null}",options));

@layomia
layomiaforce-pushed the json_valuetype_null branch from dd64841 to 409affaCompareAugust 6, 2020 21:16
@layomia
layomia merged commit 0302256 into dotnet:masterAug 7, 2020
@layomia
layomia deleted the json_valuetype_null branch August 7, 2020 15:36
Jacksondr5 pushed a commit to Jacksondr5/runtime that referenced this pull request Aug 10, 2020
@ghostghost locked as resolved and limited conversation to collaborators Dec 7, 2020
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid NRE by throwing JsonException when deserializing "null" into non-nullable types

2 participants

@layomia@steveharter