Skip to content
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

JsonConstructorAttribute not working for private/internal ctor in .NET 8.0 preview7 #90577

Closed
TanvirArjel opened this issue Aug 15, 2023 · 8 comments · Fixed by #90612
Closed
Assignees
Milestone

Comments

@TanvirArjel
Copy link

Description

Hello @eiriktsarpalis,

I have tested with .NET 8.0 Preview7 SDK with Rider. It's not working. It's throwing the following exception:

image

My SDKs:

image

Debug Log:

image

Employee class:

image

Reproduction Steps

Try in Rider with .NET 8.0 preview 7 SDK.

Expected behavior

It should have worked as mentioned here: #88452

Actual behavior

Not working. Throwing Unsupported exception.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Aug 15, 2023
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Aug 15, 2023
@eiriktsarpalis
Copy link
Member

Hi @TanvirArjel, would it be possible to share your reproduction in text? It's difficult to transcribe using screenshots.

@eiriktsarpalis eiriktsarpalis added area-System.Text.Json and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Aug 15, 2023
@ghost
Copy link

ghost commented Aug 15, 2023

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

Hello @eiriktsarpalis,

I have tested with .NET 8.0 Preview7 SDK with Rider. It's not working. It's throwing the following exception:

image

My SDKs:

image

Debug Log:

image

Employee class:

image

Reproduction Steps

Try in Rider with .NET 8.0 preview 7 SDK.

Expected behavior

It should have worked as mentioned here: #88452

Actual behavior

Not working. Throwing Unsupported exception.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Author: TanvirArjel
Assignees: -
Labels:

area-System.Text.Json, untriaged, needs-area-label

Milestone: -

@huoyaoyuan
Copy link
Member

The exception message says "parameterless constructor, a singular parameterized constructor or a parameterized constructor annotated with 'JsonConstructorAttribute'".

JsonConstructorAttribute is meant to be used on the constructor Employee(int id, string name).

@TanvirArjel
Copy link
Author

@eiriktsarpalis Here it is:

The Program class:

using System;
using System.Text.Json;
using ConsoleApp2;

internal class Program
{
    public static void Main(string[] args)
    {
        Employee employee = new(1, "Tanvir");
        string jsonString = JsonSerializer.Serialize(employee);

        Employee deEmployee = JsonSerializer.Deserialize<Employee>(jsonString);
    }
}

The employee class:

public class Employee
{
    [JsonConstructor]
    private Employee()
    {
    }

    public Employee(int id, string name)
    {
        this.Id = id;
        this.Name = name;
    }
    
    public int Id { get; set; }

    public string Name { get; set; }
}

@TanvirArjel
Copy link
Author

TanvirArjel commented Aug 15, 2023

@huoyaoyuan For Employee(int id, string name) ctor, you don't even need JsonConstructorAttribute.

@huoyaoyuan
Copy link
Member

For Employee(int id, string name) ctor, you don't even need JsonConstructorAttribute.

Right, sometimes it can't be determined and needs to be specified.
JsonConstructorAtttribute is used for a constructor accepting all properties, not parameterless constructor.

@TanvirArjel
Copy link
Author

@huoyaoyuan Primary need of JsonConstructorAtttribute is to be used in private parameterless ctor. If it does not work then it is useless.

@eiriktsarpalis
Copy link
Member

Appears to be a bug specific to applying the attribute to parameterless constructors. We should fix this.

@eiriktsarpalis eiriktsarpalis added bug and removed untriaged New issue has not been triaged by the area owner labels Aug 15, 2023
@eiriktsarpalis eiriktsarpalis added this to the 8.0.0 milestone Aug 15, 2023
@eiriktsarpalis eiriktsarpalis self-assigned this Aug 15, 2023
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Aug 15, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Aug 16, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Sep 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants