Skip to content

Treat constructor arguments as required properties #78151

Open

Description

@krwq

Compiler required keyword semantics

private class Test
{
    public required string Name { get; } // error

    public Test(string name)
    {
        Name = name;
    }
}

don't allow to compile above code because property doesn't have a setter - this makes sense because argument needs to be passed in the constructor anyway making required effectively redundant.

On the other hand when deserializing JSON Name does not have to be passed in the payload even though the only way to construct is through constructor - we will use default value (i.e. if value was int we'd pass 0 or null for string for you automatically):

var obj = System.Text.Json.JsonSerializer.Deserialize<Test>("{}"); // no error

public sealed class Test
{
    public string Name { get; }

    public Test(string name)
    {
        Name = name;
    }
}

Consider changing semantics here and make Name required instead making above code create error. Possibly there should be an option switch to opt-in to suggested behavior given this change will be breaking.

Example API suggestion (possibly consider enum and more generic name in case we need more related options in the future):

public partial class JsonSerializerOptions
{
    public bool RequireConstructorParameters { get; set; }
}

Related: #78098

Activity

  1. added
    api-suggestion
    on Nov 10, 2022
  2. ghost added
    untriaged
    on Nov 10, 2022
  3. changed the title [-]Treat constructor arguments as required properties by default[/-] [+]Treat constructor arguments as required properties[/+] on Nov 10, 2022
  4. krwq commented on Nov 10, 2022

    @krwq
    MemberAuthor

    alternative design for that would be to provide Modifier for contract resolver but it's currently tricky to implement as a separate library because we don't have parametrized ctor APIs available yet (so basically user has to repeat JsonConstructor logic).

  5. eiriktsarpalis commented on Nov 10, 2022

    @eiriktsarpalis
    Member

    Proposal looks good to me. We should also include provision for configuring requiredness on the type level:

    public sealed class Test
    {
        [JsonConstructor(RequireParameters = true)]
        public Test(int p1, int p2, int p3, int p4, ...)
        {
        }
    }

    and at the individual parameter level:

    public sealed class Test
    {
        public Test(int p1, [JsonRequired] int p2, int p3, int p4, ...)
        {
        }
    }
  6. krwq commented on Nov 10, 2022

    @krwq
    MemberAuthor

    Extra overload of JsonConstructorAttribute sound good but for JsonRequired - we put [JsonConverter(...)] and [JsonNumberHandling(...)] directly on the property so I'm not sure why we'd do it differently for [JsonRequired]...

  7. added this to the 8.0.0 milestone on Nov 23, 2022
  8. ghost removed
    untriaged
    on Nov 23, 2022
  9. krwq commented on Nov 23, 2022

    @krwq
    MemberAuthor

    marking as 8.0 because we should look at this in conjunction with other 8.0 items.

  10. modified the milestones: 8.0.0, Future on Jan 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionarea-System.Text.Json

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions