Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add description to enum values in Swagger

I have a class that looks like this:

public class Rule {   
    private RuleType type; //enum
    private String value;
}

The enum is:

public enum RuleType {
    TYPE_A,
    TYPE_B,
    TYPE_C;
}

Now, for each rule type the values are different:

  • TYPE_A requires a number 1 through 10,
  • TYPE_B is true or false, and
  • TYPE_C is a string.

What I tried

I try to add Swagger annotations to the enum so that the documentation can show this, something like this:

public enum RuleType {
    @ApiModelProperty(value = "1 - 10")
    TYPE_A, 
    @ApiModelProperty(value = "True or False")
    TYPE_B, 
    @ApiModelProperty(value = "String")
    TYPE_C;
}

Issue

But this doesn't work. The Swagger output just ignores the properties on the enums.

Is there a way to create documentation like this?

like image 764
Juan Pablo Enrique Avatar asked Dec 09 '25 02:12

Juan Pablo Enrique


1 Answers

You cannot do this as you like.

But there is another simple solution in adding the annotation @ApiModelProperty with a summary description to the property:

public class Rule {

    @ApiModelProperty(value = 
            "TYPE_A must be 1 - 10\n" +
            "TYPE_B must be True or False\n" +
            "TYPE_C must be String")
    private RuleType type; //enum
    private String value;
}

Results in Swagger UI showing the description with enum values for the property type:

Swagger UI showing the description with enum values for the property

like image 143
Azamat Ziyadullayev Avatar answered Dec 11 '25 17:12

Azamat Ziyadullayev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!