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

System.Text.Json Serialize ignores Properties from base interfaces #73021

Closed
jogibear9988 opened this issue Jul 28, 2022 · 2 comments
Closed

Comments

@jogibear9988
Copy link

jogibear9988 commented Jul 28, 2022

Description

If you have a Interface wich inherits from a Basic one, System.Text.Json only serializes the Properties from the Interface specified.
This has nothing to do with polymorphism, cause I've got a instance of the derived interface.
(Look, my Object Variable is of Type B, and Type B should include the Properties of Type A)

Reproduction Steps

Try this Code:

    interface A
    {
        string NameA { get; }
    }

    interface B : A
    {
        string NameB { get; }
    }

    public class C : B
    {
        public string NameA => "A";
        public string NameB => "B";
    }

    [Test]
    public void TestSystemTextJson()
    {
        B obj = new C();
        var text = System.Text.Json.JsonSerializer.Serialize(obj);
    }

Expected behavior

The result should be:

    {"NameA":"A","NameB":"B"}

Actual behavior

The result is:

{"NameB":"B"}

Regression?

No response

Known Workarounds

No response

Configuration

.Net6 latest Service Pack
Windows 10
x64

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jul 28, 2022
@ghost
Copy link

ghost commented Jul 28, 2022

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

If you have a Interface wich inherits from a Basic one, System.Text.Json only serializes the Properties from the Interface specified.
This has nothing to do with polymorphism, cause I've got a instance of the derived interface.

Reproduction Steps

Try this Code:

    interface A
    {
        string NameA { get; }
    }

    interface B : A
    {
        string NameB { get; }
    }

    public class C : B
    {
        public string NameA => "A";
        public string NameB => "B";
    }

    [Test]
    public void TestSystemTextJson()
    {
        B obj = new C();
        var text = System.Text.Json.JsonSerializer.Serialize(obj);
    }

Expected behavior

The result should be:

    {"NameA":"A","NameB":"B"}

Actual behavior

The result is:

{"NameB":"B"}

Regression?

No response

Known Workarounds

No response

Configuration

.Net6 latest Service Pack
Windows 10
x64

Other information

No response

Author: jogibear9988
Assignees: -
Labels:

area-System.Text.Json

Milestone: -

@jogibear9988 jogibear9988 changed the title System.Text.Json Serialize ignores Properties from inherited interfaces System.Text.Json Serialize ignores Properties from base interfaces Jul 28, 2022
@eiriktsarpalis
Copy link
Member

Thanks for reporting. I believe this is a duplicate of #41749.

@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Jul 28, 2022
jogibear9988 added a commit to jogibear9988/runtime that referenced this issue Jul 31, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Aug 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants