-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Constructor parameters should match property names #33796
Constructor parameters should match property names #33796
Comments
Estimates:
|
We need to detect an inclusion approach; we want to focus on the JSON deserialization scenario. |
This should only apply to constructors painted with the
If the engine already has a "close match" name detector, then a fixer could suggest correcting a typo; but otherwise a fixer seems too open of a problem (unless other bright ideas surface). |
For existing violations in |
It should also considered that the JsonPropertyAttribute should be able to decorate constructor parameters. @bartonjs |
@jeffhandley is this 5.0 work? |
@danmosemsft Moved to Future. Thanks. We will target delivering this off-cycle soon after 5.0 since it will be good to get this out before folks start producing code that will violate the rule. |
Hi, May I "book" this issue as "up-for-grabs"? |
That is right |
Thank you. I've spent some time on initial planning, and I'm wondering whether the following questions can be addressed before the implementation:
Notes:
(2) I'm assuming this analyzer should also be applied to VB. (3) This Migrate from Newtonsoft.Json to System.Text.Json - .NET | Microsoft Docs doc says:
(4) It seems like uninitialized public properties are not a responsibility of this analyzer. Example:
(5) It seems like this analyzer belongs to the "Design" category. As far as I can see, most of them reside in:
but this issue is also marked as
|
If a property is tagged with the JsonPropertyName which has a value matching the constructor param name, should this not find a match? public class Example
{
[JsonConstructor]
public Example(string Name)
{
FirstName = Name;
}
[JsonPropertyName("Name")]
public string FirstName { get; }
}
public class ExampleTest
{
[Test]
public void JsonRoundTrip()
{
var test = new Example("Joe");
string output = JsonSerializer.Serialize(test);
var result = JsonSerializer.Deserialize<Example>(output);
Assert.Equals(test.FirstName, result.FirstName);
}
} Running the test above gives: I'm trying to add json attributes to an existing library in which the models (combination of structs and classes), don't always have the property name matching the constructor parameter. |
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsIn general, constructor arguments who initialize should match properties (casing aside). There are also cases where serializers will match properties to constructor parameters in order to construct immutable types. This would address that too. We should limit this to types/constructs we consider being used by the JSON serializer. Category: Design
|
@carlossanlop was the |
The PR is in roslyn-analyzers repo: dotnet/roslyn-analyzers#4877 |
@buyaa-n could you link the PR to this issue? I don't seem to have permissions in the roslyn-analyzers repo. |
Yeah I think it's only possible if you explicitly add a |
In general, constructor arguments who initialize should match properties (casing aside). There are also cases where serializers will match properties to constructor parameters in order to construct immutable types. This would address that too.
We should limit this to types/constructs we consider being used by the JSON serializer.
Category: Design
The text was updated successfully, but these errors were encountered: