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

Inheritance with RegisterAssembly depends on class order #112

Open
ypPascal opened this issue Aug 26, 2022 · 0 comments · May be fixed by #115
Open

Inheritance with RegisterAssembly depends on class order #112

ypPascal opened this issue Aug 26, 2022 · 0 comments · May be fixed by #115

Comments

@ypPascal
Copy link

When using Assembly registration, the order in which class are handle has an effect in the result.
If you take the exemple that follow, AFoo declaration is handled before Foo, so the serializer will register without discriminator convention.
Then when Foo is handled, it won't override AFoo.
As a result you get an error when trying to deserialize.
If I simply move AFoo declaration after Foo declaration, then it work properly.

// See https://aka.ms/new-console-template for more information

using System.Text.Json;
using Dahomey.Json;
using Dahomey.Json.Attributes;

var json = """
            {
"MyProp": {
    "$type": "Foo",
    "Id": 1
}
}
""";

var jsonSerializerOptions = new JsonSerializerOptions();
jsonSerializerOptions.SetupExtensions();
var registry = jsonSerializerOptions.GetDiscriminatorConventionRegistry();

registry.RegisterAssembly(typeof(Encapsulator).Assembly);

var item = JsonSerializer.Deserialize<Encapsulator>(json, jsonSerializerOptions);

Console.WriteLine(item);

public class Encapsulator
{
    public AFoo MyProp { get; set; }
}

public abstract class AFoo
{
    public int Id { get; set; }
}

[JsonDiscriminator("Foo")]
public class Foo : AFoo
{

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant