Skip to content

Conversation

@joschi
Copy link
Contributor

@joschi joschi commented May 7, 2020

This PR adds support for overriding enum values with the Jackson-specific annotations @JsonProperty or @JsonValue.

Those annotations allow customizing how enum values are serialized and deserialized by Jackson and this PR consolidates the generated OpenAPI specification with this customization.

Java example enums (both serializing and deserializing identically when using Jackson):

public enum SomeEnum {
  @JsonProperty("num-one")
  NUMONE,
	
  @JsonProperty("num-two")
  NUMTWO
}
public enum SomeEnum {
  NUMONE("num-one"), NUMTWO("num-two");

  private final String value;

  public SomeEnum(String value) {
    this.value = value;
  }

  @JsonValue
  public String getValue() {
    return value;
  }
}

Results in the generated OpenAPI specification:

someEnum:
  type: string
  enum:
    - num-one
    - num-two

Closes #3552

@jlous
Copy link

jlous commented May 24, 2020

Need this! Please merge!

@frantuma frantuma added this to the M3 milestone Jun 3, 2020
@joschi
Copy link
Contributor Author

joschi commented Jun 15, 2020

@frantuma Anything I can help you with for reviewing this PR?

final boolean useToString = _mapper.isEnabled(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);


Optional<Method> jsonValueMethod = Arrays.stream(propClass.getMethods())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@frantuma @joschi
It might be a better idea to utilize findJsonValueAccessor from BeanDescriptor since it also supports JsonValue annotations specified in interfaces.

The current implementation fails on

public interface ApiEnum {
    @JsonValue
    String jsonValue();
}
public enum ResponseType implements ApiEnum {
    Http("http"),
    Websocket("websocket");

    private String description;

    ResponseType(String description) {
        this.description = description;
    }

    @Override
    public String jsonValue() {
        return description;
    }
}

while we're able to retrieve the actual jsonValueAccessor via

var typeFactory = TypeFactory.defaultInstance();
var type = typeFactory.constructFromCanonical(ResponseType.class.getCanonicalName());
var beanDesc = Json.mapper().getSerializationConfig().introspect(type);
var accessor = beanDesc.findJsonValueAccessor();

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

Successfully merging this pull request may close these issues.

Enum names not overriden with @JsonProperty or @JsonValue

4 participants