Skip to content

Jackson enum value is not used if @JsonValue is on an enum field or nonpublic method #3998

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

Closed
mjustin opened this issue Aug 15, 2021 · 3 comments

Comments

@mjustin
Copy link

mjustin commented Aug 15, 2021

When Jackson's @JsonValue is used on a public enum method, the Swagger enum list properly uses this value (thanks to #3553). However, when it is used on an enum field instead, or on a non-public enum method, the Swagger enum list does not. This causes a discrepancy between what the REST API actually accepts (as defined by Jackson) and what the Swagger definition states it does.

A real quick way to verify this is to update the JacksonValueEnum in the swagger-core test. The EnumPropertyTest.testExtractJacksonEnumFields test will then fail.

public enum JacksonValueEnum {
    FIRST("one"),
    SECOND("two"),
    THIRD("three");

    @JsonValue
    private final String value;

    JacksonValueEnum(String value) {
        this.value = value;
    }
}

OR

public enum JacksonValueEnum {
    FIRST("one"),
    SECOND("two"),
    THIRD("three");

    private final String value;

    JacksonValueEnum(String value) {
        this.value = value;
    }

    @JsonValue
    private String getValue() {
        return value;
    }
}
@mjustin mjustin changed the title Jackson enum value is not used if @JsonValue is on an enum field Jackson enum value is not used if @JsonValue is on an enum field or a nonpublic method Aug 17, 2021
@mjustin mjustin changed the title Jackson enum value is not used if @JsonValue is on an enum field or a nonpublic method Jackson enum value is not used if @JsonValue is on an enum field or nonpublic method Aug 17, 2021
JasonVoss pushed a commit to simranBhamra/swagger-core that referenced this issue Nov 20, 2021
@jadrovski
Copy link

And also @JsonValue not used when on interface method declaration.

public interface ErrorCode {
  @JsonValue
  String code();
}

public enum Code implements ErrorCode {
    SOME_ERROR_CODE("actual_error_code");

    private final String code;

    Code(String code) {
      this.code = code;
    }

    @Override
    public String code() {
      return code;
    }
  }

JasonVoss pushed a commit to JasonVoss/swagger-core that referenced this issue Apr 20, 2022
JasonVoss pushed a commit to JasonVoss/swagger-core that referenced this issue Apr 20, 2022
@bkahlert
Copy link

Had the same issue. Making the @JsonValue field / method public resolved the issue for me.

frantuma pushed a commit to JasonVoss/swagger-core that referenced this issue Nov 2, 2022
@T3rm1
Copy link

T3rm1 commented Oct 25, 2024

@frantuma Do you remember why you switched to getDeclaredMethods?
This does not return methods on interfaces. See the example from @jadrovski.
It ignores the annotation on the interface method and uses the normal string representation of the enum constant.

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

4 participants