Skip to content

Default value for optional ctor params should be used when matching prop is ignored #60082

Closed

Description

@layomia

Repro:

public class Point
{
    public int X { get; }

    [JsonIgnore]
    public int Y { get; }

    public Point(int x, int y = 5) => (X, Y) = (x, y);
}

string json = "{}";
var point = JsonSerializer.Deserialize<Point>(json);
Console.WriteLine(point.Y);
// Expected: 5
// Actual: 0

The behavior should be consistent with then the matching prop is not ignored:

public class Point
{
    public int X { get; }

    public int Y { get; }

    public Point(int x, int y = 5) => (X, Y) = (x, y);
}

string json = "{}";
var point = JsonSerializer.Deserialize<Point>(json);
Console.WriteLine(point.Y);
// Expected: 5
// Actual: 5

Activity

  1. added this to the 7.0.0 milestone on Oct 6, 2021
  2. self-assigned this
    on Oct 6, 2021
  3. ghost added
    untriaged
    on Oct 6, 2021
  4. removed
    untriaged
    on Oct 6, 2021
  5. krwq commented on Oct 7, 2021

    @krwq
    Member

    should we consider this for 6.0? it might lead to data loss

  6. eiriktsarpalis commented on Oct 7, 2021

    @eiriktsarpalis
    Member

    should we consider this for 6.0? it might lead to data loss

    Is this a regression from 5.0 behavior?

  7. added
    Priority:1
    on Oct 7, 2021
  8. layomia commented on Oct 7, 2021

    @layomia
    ContributorAuthor

    Is this a regression from 5.0 behavior?

    No this behavior shipped in 5.0. A fix for this can be considered a breaking change. I recommend addressing early in 7.0 as a bug fix so we have time to react to any feedback.

  9. removed
    Priority:1
    on Jul 9, 2022
  10. modified the milestones: 7.0.0, Future on Jul 9, 2022
  11. modified the milestones: Future, 8.0.0 on Aug 1, 2022
  12. modified the milestones: 8.0.0, Future on Sep 28, 2022
  13. ghost added
    in-pr
    on Feb 8, 2023
  14. ghost removed
    in-pr
    on Feb 9, 2023
  15. ghost locked as resolved and limited conversation to collaborators on Mar 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions