swagger-core icon indicating copy to clipboard operation
swagger-core copied to clipboard

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

Open mjustin opened this issue 4 years ago • 1 comments

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 avatar Aug 15 '21 04:08 mjustin

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;
    }
  }

jadrovski avatar Feb 04 '22 07:02 jadrovski

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

bkahlert avatar Oct 27 '22 13:10 bkahlert