Skip to content

Add @JsonProperty support for enum values #1533

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

Open
evser opened this issue Oct 20, 2016 · 19 comments
Open

Add @JsonProperty support for enum values #1533

evser opened this issue Oct 20, 2016 · 19 comments

Comments

@evser
Copy link

evser commented Oct 20, 2016

Hi, starting from 2.6.0 version enums are showing so weird:
contactType (string, optional) = ['skype', 'email', 'phone']stringEnum:"skype", "email", "phone",

Fix it please.

@dilipkrish
Copy link
Member

Not sure what that means. Please take time to report a bug with additional details. Here are the issue submission guidelines

@dilipkrish dilipkrish added this to the 2.7.0 milestone Oct 20, 2016
@evser
Copy link
Author

evser commented Oct 20, 2016

Just try to create swagger docs with field of enum type and you will see that strage "stringEnum" with duplicate of enum values.

@dilipkrish
Copy link
Member

There are demos and functional tests for enums that seem to suggest otherwise. Could you perhaps create a breaking test?

@evser
Copy link
Author

evser commented Oct 20, 2016

I'll show you:

I have a such DTO:

public class LoginDTO implements Serializable {
    private UserTypeEnum userType;
//other fields, getters/setters
}

UserTypeEnum.java :

public enum UserTypeEnum {
    DRIVER, PASSENGER;
    @JsonCreator
    public static UserTypeEnum fromString(String name) {
        return UserTypeEnum.valueOf(name.toUpperCase());
    }
    @JsonValue
    public String getName() {
        return name().toLowerCase();
    }
}

Actually, JSON annotations here are for lowercase view, but the same bug is without them

The result:
image

Duplication of enum and weird "stringEnum" string.

This bug is not reproducable for 2.5.0

@evser
Copy link
Author

evser commented Oct 20, 2016

BTW, is there another way of lowercase view of enum?

@dilipkrish
Copy link
Member

Couple of things,

  • Could you try with 2.6.0 to see if its fixed
  • Could you please post the json version of the definition (as its shown in the api docs (/v2/api-docs)) of the LoginDTO

@evser
Copy link
Author

evser commented Oct 20, 2016

Please, read more careful, I wrote that this bug is only for 2.6.0, but not 2.5.0.
As for json version:

{
    "swagger": "2.0",
    "info": 
    {
        "description": "Api Documentation",
        "version": "1.0",
        "title": "Api Documentation",
        "termsOfService": "urn:tos",
        "contact": 
        {

        },

        "license": 
        {
            "name": "Apache 2.0",
            "url": "http://www.apache.org/licenses/LICENSE-2.0"
        }
    },

    "host": "HOST",
    "basePath": "/",
    "tags": 
    [
        {
            "name": "login-controller",
            "description": "Login Controller"
        }
    ],

    "paths": 
    {

        "/login": 
        {
            "post": 
            {
                "tags": 
                [
                    "login-controller"
                ],

                "summary": "User login.",
                "operationId": "loginUsingPOST",
                "consumes": 
                [
                    "application/json"
                ],

                "produces": 
                [
                    "*/*"
                ],

                "parameters": 
                [
                    {
                        "in": "body",
                        "name": "login",
                        "description": "login",
                        "required": true,
                        "schema": 
                        {
                            "$ref": "#/definitions/LoginDTO"
                        }
                    }
                ],

                "responses": 
                {
                    "200": 
                    {
                        "description": "OK",
                        "schema": 
                        {
                            "$ref": "#/definitions/LoginDTO"
                        }
                    },

                    "201": 
                    {
                        "description": "Created"
                    },

                    "401": 
                    {
                        "description": "Unauthorized"
                    },

                    "403": 
                    {
                        "description": "Forbidden"
                    },

                    "404": 
                    {
                        "description": "Not Found"
                    }
                }
            }
        },

    "definitions": 
    {


        "LoginDTO": 
        {
            "type": "object",
            "properties": 
            {
                "deviceToken": 
                {
                    "type": "string"
                },

                "login": 
                {
                    "type": "string"
                },

                "password": 
                {
                    "type": "string"
                },

                "userType": 
                {
                    "type": "string",
                    "enum": 
                    [
                        "driver",
                        "passenger"
                    ]
                }
            }
        }
    }
}

And answer please to the previous comment

@dilipkrish
Copy link
Member

dilipkrish commented Oct 20, 2016

Ok I don't see any stringEnum in the json definition. So it must be a bug in swagger-ui

@evser
Copy link
Author

evser commented Oct 20, 2016

Oh, so it's not a fault of springfox?

@evser
Copy link
Author

evser commented Oct 20, 2016

@dilipkrish is there another way of getting enum lowercased using springfox?

@dilipkrish
Copy link
Member

You can try setting the allowable values in an @ApiModel annotation for UserTypeEnum

@evser
Copy link
Author

evser commented Oct 21, 2016

It's not really a dynamic option (I mean it's not tolerant to changes). Do you have any plans to add support of @JsonProperty to emuns?
See: FasterXML/jackson-databind#1322
and FasterXML/jackson-databind#677

@dilipkrish dilipkrish modified the milestones: Someday, 2.7.0 Oct 21, 2016
@dilipkrish dilipkrish changed the title Bug with enums in 2.6.0 Add @JsonProperty support for enum values Oct 21, 2016
@dilipkrish dilipkrish removed the ready label Feb 15, 2017
@Syntobe
Copy link

Syntobe commented Aug 29, 2017

I'd be happy to contribute. Please let me know if i can help.

Best Regards

@dilipkrish
Copy link
Member

@Syntobe absolutely! Would be awesome if you can help with this.

@solo-yolo
Copy link

@Syntobe @dilipkrish
Hi guys!
Is there any progress regarding support of @JsonProperty for enum values?

@Syntobe
Copy link

Syntobe commented Jun 22, 2018 via email

@solo-yolo
Copy link

Cool! Thanks
We currently using a workaround with param/model plugins but would like to see that feature in nearest release :)

@magicbyte-fe
Copy link

@solo-yolo Hi! What kind of workaround?

@solo-yolo
Copy link

@doctorstrange85 sorry, but I don't work on that project anymore, and unfortunately can't remember what we did to workaround this issue :(

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

No branches or pull requests

5 participants