Skip to content
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

Closed
wants to merge 3 commits into from

Conversation

julien98Qc
Copy link

@julien98Qc julien98Qc commented Jul 13, 2024

  • 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.

Fixes #104700

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jul 13, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

@julien98Qc
Copy link
Author

@julien98Qc please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@dotnet-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@dotnet-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@dotnet-policy-service agree company="Microsoft"

Contributor License Agreement

@dotnet-policy-service agree

@julien98Qc julien98Qc marked this pull request as draft July 14, 2024 03:37
@julien98Qc julien98Qc marked this pull request as ready for review July 14, 2024 12:27
Assert.Contains(nameof(ClassWithRequiredCollectionPropertyWithoutGetter.TestCollectionPropertyWithoutGetter), exception.Message);
}

public class ClassWithRequiredCollectionPropertyWithoutGetter
Copy link
Member

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>(
Copy link
Member

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.
Copy link
Member

@eiriktsarpalis eiriktsarpalis left a 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.

@eiriktsarpalis
Copy link
Member

@julien98Qc Thank you for taking the time to contribute a fix!

@julien98Qc
Copy link
Author

@eiriktsarpalis Thank you for reviewing :D

@julien98Qc julien98Qc deleted the issue-104700 branch August 3, 2024 17:34
@github-actions github-actions bot locked and limited conversation to collaborators Sep 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to deserialize JSON into a collection property without a getter (with a misleading error message)
2 participants