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

Jackson annotations ignored on enum class #3691

Open
swarth100 opened this issue Sep 2, 2020 · 1 comment
Open

Jackson annotations ignored on enum class #3691

swarth100 opened this issue Sep 2, 2020 · 1 comment
Assignees

Comments

@swarth100
Copy link

I have a dataclass that is used as a field in complex request and response bodies. The class uses JsonFormat to have custom serialisation:

@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum User {
    FOO(1, "Bar");

    User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    private int id;
    private String name;
}

I would expect it to be serialised as:

"User" : {
    "properties" : {
        "id" : {
            "format" : "int32",
            "type" : "integer"
        },
        "name" : {
            "type" : "string"
        }
    },
    "type" : "object"
}

Unfortunately it gets serialised as:

"user" : {
    "enum" : [ "FOO" ],
    "type" : "string"
}

I have tried the workarounds listed in #2969, but it seems like most enum class-level Jackson annotations are currently ignored.

The only workaround I found was:

class UserSer {
    @JsonProperty
    private int id;

    @JsonProperty
    private String name;
}

@JsonFormat(shape = JsonFormat.Shape.OBJECT)
@Schema(implementation = UserSer.class)
public enum User {
   ...
}

I believe the current problem resides in this line of the ModelResolver:

if (model == null && type.isEnumType()) {
model = new StringSchema();
_addEnumProps(type.getRawClass(), model);
isPrimitive = true;
}

Any class with Java enum type will always be serialised as a String Schema.

I found this to be quite hard to work around as I wished to reuse ModelResolver's logic for bean serialisation and I did not wish to duplicate that logic in a custom ModelConverter.

tl;dr:

  1. Jackson annotations are disregarded when serializing enum classes [feature request]
  2. ModelResolver will serialize a Bean enum class always as a StringSchema disregarding all annotations (@Schema(type = "object") does not work) [a bug imo]

As a QoL change for (2) I would recommend splitting out the bean serialisation from the main resolve method of ModelResolver to allow it to be invoked by subclasses which could then specify a custom base schema.

@georgiivanov9
Copy link

Hi guys, any idea how to workaround this problem with this dependency as we dont have @Schema annotation ?

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

No branches or pull requests

3 participants