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

JsonSubtypes does not respect naming strategy for discriminator property value #80

Closed
egovova11 opened this issue Aug 5, 2019 · 3 comments
Assignees
Labels
Milestone

Comments

@egovova11
Copy link

egovova11 commented Aug 5, 2019

Description

The issue occurs when naming strategy other than CamelCase is used.

Source/destination types

public enum EnumType
{
    EnumMemberOne,
    EnumMemberTwo
}

public interface IMyType
{
    EnumType EnumValue {get;}
}

public class MyTypeOne : IMyType
{
    public EnumType EnumValue => EnumType.EnumMemberOne;
}

public class MyTypeTwo : IMyType
{
    public EnumType EnumValue => EnumType.EnumMemberTwo;
}
var serializerSettings = new JsonSerializerSettings
{
    ContractResolver = new DefaultContractResolver
            {
                NamingStrategy = new SnakeCaseNamingStrategy(),
            },
    Converters = new List<JsonConverter>
    {
          new StringEnumConverter
            {
                NamingStrategy = new SnakeCaseNamingStrategy()
            },
          JsonSubtypesConverterBuilder
                .Of(typeof(IMyType), nameof(IMyType.EnumValue))
                .RegisterSubtype(typeof(MyTypeOne), EnumType.EnumMemberOne)
                .RegisterSubtype(typeof(MyTypeTwo), EnumType.EnumMemberTwo)
                .Build()
    }
}

Source/destination JSON

{"enum_value":"enum_member_one"}

Expected behavior

JSON is deserialized correctly with regard to naming strategy settings.

Actual behavior

Deserialization fails with the following message:

System.ArgumentException: Could not convert 'enum_member_one' to EnumType. ---> Newtonsoft.Json.JsonSerializationException: Error converting value "enum_member_one" to type 'Namespace.EnumType'. Path 'enum_value', line 1, position 13. ---> System.ArgumentException: Requested value 'enum_member_one' was not found.

Steps to reproduce

var json = "{\"enum_value\":\"enum_member_one\"}";
var result = JsonConvert.DeserializeObject<IMyType>(jsonValue, serializerSettings);
@manuc66
Copy link
Owner

manuc66 commented Aug 5, 2019

Hi @egovova11

Thanks for submitting this issue !

Please note that the code provided is only compatible with Newtonsoft.Json version 12 and that
you need to specify the json property name on the line .Of(typeof(IMyType) as follow:

var serializerSettings = new JsonSerializerSettings
{
    ContractResolver = new DefaultContractResolver
            {
                NamingStrategy = new SnakeCaseNamingStrategy(),
            },
    Converters = new List<JsonConverter>
    {
          new StringEnumConverter
            {
                NamingStrategy = new SnakeCaseNamingStrategy()
            },
          JsonSubtypesConverterBuilder
                .Of(typeof(IMyType), "enum_value") // <--- here you have to provide the json property name
                .RegisterSubtype(typeof(MyTypeOne), EnumType.EnumMemberOne)
                .RegisterSubtype(typeof(MyTypeTwo), EnumType.EnumMemberTwo)
                .Build()
    }
}

I can reproduce the bug described, it's due to the fact that reading the discriminator property doesn't seem to use the converter provided for the property.

The problem is located here:
https://github.com/manuc66/JsonSubTypes/blob/master/JsonSubTypes/JsonSubtypes.cs#L363

But I have no idea how use JsonSerializerSettings at that place

@manuc66 manuc66 added the bug label Aug 5, 2019
@manuc66 manuc66 changed the title JsonSubtypes does not respect naming strategy JsonSubtypes does not respect naming strategy for discriminator property value Aug 5, 2019
@egovova11
Copy link
Author

Yeah, I used exactly the same approach to workaround the issue.
Also, the same applies to enum member values (i.e. EnumType.EnumMemberOne -> enum_member_one).

Thanks for pointing to the code location, I'll give it a thought. Maybe I will be able to come up with some solution for that.

@manuc66
Copy link
Owner

manuc66 commented Dec 27, 2019

I've finally found a fix, see PR #93

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

No branches or pull requests

2 participants