簡體   English   中英

如何在 Swagger 中指定不區分大小寫的枚舉?

[英]How can I specify a case-insensitive enum in Swagger?

I have an enum in Java and I want to use it as parameter in a GET request in my REST API - where the swagger docs are generated from annotations in the Java controller.

枚舉本身能夠使用不區分大小寫的值,但根據 Java 約定使用大寫值定義。

public enum SortDirection {

  ASC, DESC;

  public static SortDirection fromString(String value) { 
    try {
      return SortDirection.valueOf(value.toUpperCase(Locale.US));
    } catch(Exception e) {
      throw new IllegalArgumentException("Invalid value " + value + ". Valid values are 'asc' or 'desc' (case insensitive).", e);
    }
  }
}

fromString允許將小寫字母作為有效選項傳遞,因為 URL 使用小寫字母更為常見。

如果我使用默認值asc swagger 將引發驗證錯誤:

默認值必須存在於enum

如何指定小寫和大寫都是有效選項並設置默認值asc

您可以嘗試在方法簽名中使用 io.swagger.annotations.ApiParam :

public void controllerMethod(
       @ApiParam(allowableValues="asc,desc") @RequestParam SortDirection sortDirection);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM