ServiceStack.Java Deserialization problem for 'integer enum'

Hello,

I configured my application with JsConfig.TreatEnumAsInteger = false; to see the text of an enumeration instead of the number and the generated code works great except for the ‘integer enumerations’. For example, I have a 3rd party “integer enum” that I am returning in my request and on the wire (using Fiddler) I can see the text of the enumeration but in Java the property is null.

I suspect that the generated Java code expects an integer value since this is an ‘integer enum’ but having JsConfig.TreatEnumAsInteger set to false seems to break the feature. The TreatTypesAsStrings feature could have helped if I had only a handful of enumerations but I have a few dozen of these ‘integer enumerations’ so it would take a long time to recreate them all manually in Java not to mention that I would have to sync the changes manually.

Is there a way to tell the Java generator to always treat enum as ‘text’? Something similar to JsConfig.TreatEnumAsInteger but for the code generator. I can also create a new feature request for this.

Best regards

JsConfig.TreatEnumAsInteger already defaults to false, so enums are returned as strings and the Java JsonServiceClient should automatically support string enums.

Do you have the JSON response and generated code that it’s trying to deserialize into?

Here’s the code I was able to reproduce the issue with:

.Net:
    public class HelloResponse
    {
        public EnumWithValues Value { get; set; }
    }
    public enum EnumWithValues
    {
        One = 1, // if I remove the numbers ( = 1 and = 2) from the enums the issue does not happen but I cannot remove them in my production code
        Two = 2
    }

Java:
public static enum EnumWithValues
    {
        @SerializedName("1") One(1),
        @SerializedName("2") Two(2);

        private final int value;
        EnumWithValues(final int intValue) { value = intValue; }
        public int getValue() { return value; }
    }

    public static class HelloResponse 
    {
        public EnumWithValues value = null;
        
        public EnumWithValues getValue() { return value; }
        public HelloResponse setValue(EnumWithValues value) { this.value = value; return this; }
    }

Json response:
{"value":"One"}

This should now be resolved with this commit.

This change is available from v4.0.45 that’s now available on MyGet. If you already have v4.0.45 installed you’ll need to delete your /packages folder to fetch the latest version.

Thanks for that quick fix!