Skip to content

Constructor parameters should match property names #33796

Open

Description

@terrajobst

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

Activity

  1. added
    api-suggestion
    code-analyzer
    on Mar 19, 2020
  2. added this to the 5.0 milestone on Mar 20, 2020
  3. added
    code-fixer
    on Mar 21, 2020
  4. jeffhandley commented on Mar 21, 2020

    @jeffhandley
    Member

    Estimates:

    • Analyzer: Medium
    • Fixer: Small
  5. jeffhandley commented on Mar 23, 2020

    @jeffhandley
    Member

    We need to detect an inclusion approach; we want to focus on the JSON deserialization scenario.

  6. added
    api-approved
    and removed
    api-suggestion
    on Mar 23, 2020
  7. bartonjs commented on Mar 24, 2020

    @bartonjs
    Member

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

  8. jeffhandley commented on May 26, 2020

    @jeffhandley
    Member

    For existing violations in runtime, we likely would not apply the fix since changing parameter names would often be a breaking change.

  9. swtrse commented on Jun 24, 2020

    @swtrse

    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.

  10. danmoseley commented on Jul 30, 2020

    @danmoseley
    Contributor

    @jeffhandley is this 5.0 work?

  11. modified the milestones: 5.0.0, Future on Jul 30, 2020
  12. 33 remaining items

  13. modified the milestones: Future, 8.0.0 on Nov 9, 2022
  14. eiriktsarpalis commented on Jul 5, 2023

    @eiriktsarpalis
    Member

    @carlossanlop was the in-pr label applied correctly? I couldn't a find a PR linked to this issue.

  15. buyaa-n commented on Jul 5, 2023

    @buyaa-n
    Contributor

    The PR is in roslyn-analyzers repo: dotnet/roslyn-analyzers#4877

  16. eiriktsarpalis commented on Jul 5, 2023

    @eiriktsarpalis
    Member

    @buyaa-n could you link the PR to this issue? I don't seem to have permissions in the roslyn-analyzers repo.

  17. carlossanlop commented on Jul 5, 2023

    @carlossanlop
    Contributor
  18. carlossanlop commented on Jul 5, 2023

    @carlossanlop
    Contributor

    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:

    image

  19. eiriktsarpalis commented on Jul 5, 2023

    @eiriktsarpalis
    Member

    Yeah I think it's only possible if you explicitly add a Fix <link> statement in the OP of the PR.

  20. buyaa-n commented on Aug 14, 2023

    @buyaa-n
    Contributor

    As there were no progress in the PR moving into future milestone, CC @psxvoid.

  21. modified the milestones: 8.0.0, Future on Aug 14, 2023
  22. modified the milestones: Future, 9.0.0 on Aug 14, 2023
  23. modified the milestones: 9.0.0, Future on Nov 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Text.Jsoncode-analyzercode-fixerin-pr

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions