Issue 104700: Clarify error message for collection property with no getter#104861
Issue 104700: Clarify error message for collection property with no getter#104861julien98Qc wants to merge 3 commits intodotnet:mainfrom
Conversation
|
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis |
@dotnet-policy-service agree |
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonPropertyInfo.cs
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonPropertyInfo.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/tests/Common/RequiredKeywordTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonPropertyInfo.cs
Outdated
Show resolved
Hide resolved
| Assert.Contains(nameof(ClassWithRequiredCollectionPropertyWithoutGetter.TestCollectionPropertyWithoutGetter), exception.Message); | ||
| } | ||
|
|
||
| public class ClassWithRequiredCollectionPropertyWithoutGetter |
There was a problem hiding this comment.
Just to make sure we're not regressing things here, could you also add a test for class like so:
class ClassWithCollectionCtorParam(Dictionary<string, JsonElement> value)
{
public Dictionary<string, JsonElement> Value { get; } = value;
}And then try to deserialize it using an options instance with RespectRequiredConstructorParameters set to true.
| public async Task RequiredCollectionPropertyWithoutGetterThrows() | ||
| { | ||
| string json = """{"Foo":"foo","Bar":"bar"}"""; | ||
| InvalidOperationException exception = await Assert.ThrowsAsync<InvalidOperationException>( |
There was a problem hiding this comment.
I don't think this is the correct way to address the problem. A fix should instead try to make the scenario work as expected. If you remove JsonRequired from the repro in #104700 then deserialization works as expected and this should too.
…etter - Added verification to distinguish between different error types. - Renamed existing test to more accurately reflect that it tests required properties without a setter. - Added a new test to ensure that a collection property without a getter throws an InvalidOperationException.
- Logic back inline and adjusted to not expand failure.
eiriktsarpalis
left a comment
There was a problem hiding this comment.
Per https://github.com/dotnet/runtime/pull/104861/files#r1692841347 I don't believe clarifying the error message is the correct way to address the issue. We should instead make the impact scenario work similar to how it does when [JsonRequired] isn't being specified.
|
@julien98Qc Thank you for taking the time to contribute a fix! |
|
@eiriktsarpalis Thank you for reviewing :D |
Fixes #104700