2

I am using Swagger-UI with Java spring/hibernate. The Swagger UI version is 2.

I am having hard time in using enums.

I have a POJO model in which I have following enum property declared.

@Enumerated(EnumType.STRING)
@Column(name = "gender")
@Access(AccessType.FIELD)
private Gender gender;

and the Gender is enum as follows:

public enum Gender {

    M("Male"), F("Female");

    private String displayValue;

    private Gender(String displayValue) {
        this.displayValue = displayValue;
    }

    @Override
    public String toString() {
        return displayValue;
    }

    public String getDisplayValue() {
        return displayValue;
    }

    public String getName() {
        return name();
    }   
}

But my swagger displays only display values (male or female) in UI as well as the dropdown's display value. I want Swagger UI to display something similar like below which is HTML equivalent:

<select name="gender">
<option value="M">Male</option>
<option value="F">Female</option>
</select>

How to achieve this? Reading up the Swagger UI docs, its so much confusing for me, unable to understand it. please help.

jarvo69
  • 7,908
  • 2
  • 18
  • 28
  • Possible duplicate of [How to define enum in swagger.io?](http://stackoverflow.com/questions/27603871/how-to-define-enum-in-swagger-io) – Asthor Aug 06 '16 at 12:01
  • 1
    @Asthor Its not duplicate one. I have asked this question in context to "Java programming language" while that answer you pointed out is given in "Javascript" context. That doesn't solve my query as java and Javascript are different, specially Java spring and Javascript. – jarvo69 Aug 06 '16 at 12:07
  • Meant it as a duplicate as it shows how enums are defined in Swagger. I might however not be reading your questions correctly. – Asthor Aug 06 '16 at 12:16
  • 1
    I mentioned clearly "Java" – jarvo69 Aug 06 '16 at 12:17
  • Your question is that you are having issues with displaying enum in Swagger-UI. The question I said it was a duplicate of answers exactly that question. Swagger-UI uses Javascript and not Java assets so there is a clear crossover from Java to Javascript here. – Asthor Aug 06 '16 at 12:29
  • 1
    Question title "Java Swagger UI - How to display enums?" first line says **"I am using Swagger-UI with Java spring/hibernate. The Swagger UI version is 2."** Kindly read the questions very carefully before pointing out anything. Thanks. – jarvo69 Aug 06 '16 at 13:02

1 Answers1

2

You need to create a custom class (AllowableMapValues) by implementing the interface springfox.documentation.service.AllowableValues that would return Map type instead of List type. (See class springfox.documentation.service.AllowableListValues for reference).

After that you need to play with the static methods in springfox.documentation.schema.Enums class by hiding the static method (AllowableValues allowableValues(Class<?> type)) to return the newly created AllowableMapValues class with something like

public static AllowableValues allowableValues(Class<?> type) {
    if (type.isEnum()) {
      Map<String, String> enumValues = getEnumMapValues(type);
      return new AllowableMapValues(enumValues, "MAP");
    }
    return null;
  }

Where <"K","V"> pair be like <"M", "Male">

you should create static method getEnumMapValues which should work in the same way as getEnumValues method in Enums class.

Now swagger will be generated to the desired Type.

Remember to put the @JsonValue annotation on the method in you Gender enum that you like to show on the UI. Swagger by default picks up the value that is annotated with @JsonValue. If it is not found, it takes the input directly as a String.

I haven't tried this all but, i was able to get the Enum name instead of Enum value in swagger dropdown by annotating the following method.

@JsonValue  
public String enumName() {
    return name();
}

This returns the following M

You can try the above if it solves the problem

Udit Goel
  • 21
  • 2