1

I am getting the following error when trying to get the enum values for an allowableValues tag.

The value for annotation attribute ApiModelProperty.allowableValues must be a constant expression

What I am trying to do:

@ApiModelProperty(allowableValues = new Enums().enumToString(SomeEnum.class))
private String someString;

Here is the logic for Enums().enumToString

public class Enums {

public final <E extends Enum<E>> String enumToString(Class<E> inputEnum) {
    //inputEnum;
    for (Enum enumValues : EnumSet.allOf(inputEnum)) {
          //will create a string of the enum values
        }

    return "will be a formatted string of the enum values";
}

}

Is it possible to do what I am trying to accomplish? I know it is possible to just expose the enum in my API and swagger would then recognize the allowed values automatically, however the field in question needs to be exposed as a string, even though our internal logic has it as an enum.

Thanks in advance for your help!

Brian
  • 556
  • 6
  • 26
  • 1
    See if this question helps : https://stackoverflow.com/questions/23936140/enum-in-swagger – Arnaud Jun 22 '18 at 13:48

2 Answers2

2

Your problem is not related to Swagger at all but basic Java.

In Java annotations, you can have constant expressions, meaning you can't do method calls there, any other things but only use constant values.

Arnold Galovics
  • 3,246
  • 3
  • 22
  • 33
2
@ApiModelProperty(value = "embed", dataType = "[Lmodel.request.Embed;")
private final List<String> embed;

the Embed path is a list of enums. This show you something like this in the swagger documentation:

type: "object"
properties:
  embed:
    type: "array"
    description: "embed"
    items:
      type: "string"
      enum:
        -SOME
        -SOME
        -SOME
        -SOME
  • 2
    @R Soans, please explain the codes and answer to the question. you should tell why this error raised and how to fixed it. thanks for helping – Mehdi Yeganeh Aug 22 '19 at 21:28
  • @MehdiYeganeh, galovics answer is spot on. My answer just shows how Brian could avoid explicitly mentioning all the values of the enum in the "allowableValues" property. – Rawlin Soans Oct 19 '19 at 06:23