-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue 104700: Clarify error message for collection property with no getter #104861
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
Outdated
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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