Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use enum properties in OpenApi 3.0.1

I have an enum like this:

@Getter
public enum ErrorCode implements BaseError {
    // @formatter:off
    FOO(100001, "Foo message.",
    BAR(100002, "Bar message."), 
.
.
.

    FOOBAR(300003, "FOOBAR message.");

    public final int errorId;
    public final String errorMsg;

    ErrorCode(int errorId, String errorMsg) {
        this.errorId = errorId;
        this.errorMsg = errorMsg;
    }
}

I know, I can use @Schema(implementation = ErrorCode.class) to display the enum in SqggerUI but it displays the name. Ist there a way to display the error code (or any other defined properties of an enum)?

In this case I would like to display the list of errorCodes instead of the default which displays the list of the enum names.

like image 979
du-it Avatar asked Nov 17 '25 04:11

du-it


1 Answers

As I suspected, OpenApi uses enum's toString(), which is by default implemented as calling name(). If you override the enum's toString(). you will reach your goal.

I can't see any reason why you should not redefine toString() for a Java enum, I am not aware of any contract which this should go against.

like image 188
Honza Zidek Avatar answered Nov 18 '25 19:11

Honza Zidek



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!