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 source generator ignores properties from base records defined in separate assemblies #61772

Closed
Memoryfox opened this issue Nov 18, 2021 · 3 comments
Assignees
Milestone

Comments

@Memoryfox
Copy link

Description

Properties of a base record defined in a referenced NuGet package are ignored when creating a JsonSerializerContext.
When working with classes it is working as expected.

Reproduction Steps

Create a project with a record:

public record FooData
{
	public virtual int FooValue { get; set; }
}

Create a NuGet package.

Create a second project and reference the NuGet package:

public record BarData : Foo.FooData
{
	public int BarValue { get; set; }
}

Create a JsonSerializerContext:

[JsonSerializable(typeof(BarData))]
public partial class BarJsonSerializerContext : JsonSerializerContext { }

Expected behavior

The generated context (BarJsonSerializerContext) should handle FooValue and BarValue.

Actual behavior

The generated context (BarJsonSerializerContext) handles only BarValue.

Regression?

No response

Known Workarounds

Reference the project with the base types directly if possible instead of using a NuGet package.

Configuration

.net 6.0.100

Other information

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Nov 18, 2021
@ghost
Copy link

ghost commented Nov 18, 2021

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

Issue Details

Description

Properties of a base record defined in a referenced NuGet package are ignored when creating a JsonSerializerContext.
When working with classes it is working as expected.

Reproduction Steps

Create a project with a record:

public record FooData
{
	public virtual int FooValue { get; set; }
}

Create a NuGet package.

Create a second project and reference the NuGet package:

public record BarData : Foo.FooData
{
	public int BarValue { get; set; }
}

Create a JsonSerializerContext:

[JsonSerializable(typeof(BarData))]
public partial class BarJsonSerializerContext : JsonSerializerContext { }

Expected behavior

The generated context (BarJsonSerializerContext) should handle FooValue and BarValue.

Actual behavior

The generated context (BarJsonSerializerContext) handles only BarValue.

Regression?

No response

Known Workarounds

Reference the project with the base types directly if possible instead of using a NuGet package.

Configuration

.net 6.0.100

Other information

No response

Author: Memoryfox
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@eiriktsarpalis
Copy link
Member

I can reproduce, the issue specifically impacts records inherited across projects. It doesn't seem to impact regular classes. I can reproduce using p2p references, so this is not exclusive to NuGet dependencies. Minimal reproduction:

Class Library

public record LibRecord
{
	public int LibValue { get; set; }
}

Console App

using System;
using System.Text.Json;
using System.Text.Json.Serialization;

var value = new AppRecord { LibValue = 1, AppValue = 2 };
string json = JsonSerializer.Serialize(value, MyJsonSerializerContext.Default.AppRecord);
Console.WriteLine(json);

public record AppRecord : LibRecord
{
	public int AppValue { get; set; }
}

[JsonSerializable(typeof(AppRecord))]
public partial class MyJsonSerializerContext : JsonSerializerContext { }

@layomia @ericstj seems like something to consider for servicing?

@eiriktsarpalis eiriktsarpalis added this to the 6.0.x milestone Nov 18, 2021
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Nov 18, 2021
@eiriktsarpalis eiriktsarpalis added bug untriaged New issue has not been triaged by the area owner and removed untriaged New issue has not been triaged by the area owner labels Nov 18, 2021
@eiriktsarpalis eiriktsarpalis changed the title System.Text.Json source generator ignores properties from base records in NuGet packages System.Text.Json source generator ignores properties from base records defined in separate assemblies Nov 18, 2021
@layomia layomia self-assigned this Nov 18, 2021
@layomia
Copy link
Contributor

layomia commented Dec 22, 2021

Dupe - same underlying issue as #62374.

@layomia layomia closed this as completed Dec 22, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Jan 22, 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

3 participants