You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Constructor parameters should match property names #33796
api-approvedAPI was approved in API review, it can be implementedarea-System.Text.Jsoncode-analyzerMarks an issue that suggests a Roslyn analyzercode-fixerMarks an issue that suggests a Roslyn code fixerin-prThere is an active PR which will close this issue when it is merged
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.
This should only apply to constructors painted with the [JsonConstructor] attribute, but should be prepared to account for other enrollment scenarios.
For each parameter name in the parameter list of an included constructor
If there is not a property on the type hierarchy at that level whose name is OrdinalIgnoreCase equal to the parameter name, report a diagnostic against the parameter (ideally just the name).
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).
It should also considered that the JsonPropertyAttribute should be able to decorate constructor parameters.
This way the exact name in case sensitive format can be defined for the constructor parameters and the actual parameter name does not matter at all.
If done in an other way this would be fine too but there should be a way to exacly define the json names for the parameters in an case sensitive way.
@bartonjs
I strongly opose the option where the match is done with an IgnoreCase equal at least when the global options are not set to case insensitive name resolution.
api-approvedAPI was approved in API review, it can be implementedarea-System.Text.Jsoncode-analyzerMarks an issue that suggests a Roslyn analyzercode-fixerMarks an issue that suggests a Roslyn code fixerin-prThere is an active PR which will close this issue when it is merged
Activity
jeffhandley commentedon Mar 21, 2020
Estimates:
jeffhandley commentedon Mar 23, 2020
We need to detect an inclusion approach; we want to focus on the JSON deserialization scenario.
bartonjs commentedon Mar 24, 2020
This should only apply to constructors painted with the
[JsonConstructor]attribute, but should be prepared to account for other enrollment scenarios.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).
jeffhandley commentedon May 26, 2020
For existing violations in
runtime, we likely would not apply the fix since changing parameter names would often be a breaking change.swtrse commentedon Jun 24, 2020
It should also considered that the JsonPropertyAttribute should be able to decorate constructor parameters.
This way the exact name in case sensitive format can be defined for the constructor parameters and the actual parameter name does not matter at all.
If done in an other way this would be fine too but there should be a way to exacly define the json names for the parameters in an case sensitive way.
@bartonjs
I strongly opose the option where the match is done with an IgnoreCase equal at least when the global options are not set to case insensitive name resolution.
danmoseley commentedon Jul 30, 2020
@jeffhandley is this 5.0 work?
33 remaining items
eiriktsarpalis commentedon Jul 5, 2023
@carlossanlop was the
in-prlabel applied correctly? I couldn't a find a PR linked to this issue.buyaa-n commentedon Jul 5, 2023
The PR is in roslyn-analyzers repo: dotnet/roslyn-analyzers#4877
eiriktsarpalis commentedon Jul 5, 2023
@buyaa-n could you link the PR to this issue? I don't seem to have permissions in the roslyn-analyzers repo.
carlossanlop commentedon Jul 5, 2023
@eiriktsarpalis dotnet/roslyn-analyzers#4877
carlossanlop commentedon Jul 5, 2023
Someone please correct me if I'm wrong: I don't think we can link issues to PRs across repos. That's why I had to add the label manually.
Edit: I might be wrong, I can see this in the PR:
eiriktsarpalis commentedon Jul 5, 2023
Yeah I think it's only possible if you explicitly add a
Fix <link>statement in the OP of the PR.buyaa-n commentedon Aug 14, 2023
As there were no progress in the PR moving into future milestone, CC @psxvoid.