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

Duplicate Key if several type have the same discriminator #113

Open
Itori opened this issue Aug 27, 2022 · 1 comment · May be fixed by #114
Open

Duplicate Key if several type have the same discriminator #113

Itori opened this issue Aug 27, 2022 · 1 comment · May be fixed by #114

Comments

@Itori
Copy link

Itori commented Aug 27, 2022

If 2 types have the same discriminator, you will get a duplicate key error.

For reproduction add to DiscrinatorTests :

    public class BaseObjectResponse
    {
        public int Id { get; set; }
    }

    [JsonDiscriminator(12)]
    public class NameObjectResponse : BaseObjectResponse
    {
        public string Name { get; set; }
    }
        [Theory]
        [InlineData(DiscriminatorPolicy.Default, @"{""BaseObject"":{""$type"":12,""Name"":""foo"",""Id"":1},""NameObject"":{""Name"":""bar"",""Id"":2}}")]
        [InlineData(DiscriminatorPolicy.Auto, @"{""BaseObject"":{""$type"":12,""Name"":""foo"",""Id"":1},""NameObject"":{""Name"":""bar"",""Id"":2}}")]
        [InlineData(DiscriminatorPolicy.Never, @"{""BaseObject"":{""Name"":""foo"",""Id"":1},""NameObject"":{""Name"":""bar"",""Id"":2}}")]
        [InlineData(DiscriminatorPolicy.Always, @"{""BaseObject"":{""$type"":12,""Name"":""foo"",""Id"":1},""NameObject"":{""$type"":12,""Name"":""bar"",""Id"":2}}")]
        public void WritePolymorphicObjectDuplicateKey(DiscriminatorPolicy discriminatorPolicy, string expected)
        {
            JsonSerializerOptions options = new JsonSerializerOptions();
            options.SetupExtensions();
            DiscriminatorConventionRegistry registry = options.GetDiscriminatorConventionRegistry();
            registry.ClearConventions();
            registry.RegisterConvention(new DefaultDiscriminatorConvention<int>(options));
            registry.RegisterType<NameObject>();
            registry.RegisterType<NameObjectResponse>();
            registry.DiscriminatorPolicy = discriminatorPolicy;

            BaseObjectHolder obj = new BaseObjectHolder
            {
                BaseObject = new NameObject
                {
                    Id = 1,
                    Name = "foo"
                },
                NameObject = new NameObject
                {
                    Id = 2,
                    Name = "bar"
                }
            };

            string actual = JsonSerializer.Serialize(obj, options);

            Assert.Equal(expected, actual);
        }
@attiqeurrehman
Copy link

I have a similar situation, different models with different operations but a common discriminator e.g:

public class Create
{
}

[JsonDiscriminator("M")]
public class CreateProject
{
}

public class Update
{
}
[JsonDiscriminator("M")]
public class UpdateProject
{
}

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.

2 participants