如何将属性swagger设置为字符串列表的列表<String>

问题描述 投票:0回答:2

我正在尝试创建 springDoc swagger 文档,并且我想表示这样的数据

List<String> elements;
具有一些属性,例如 minSize 和 MaxSize / minLength 列表中值的最大长度。 我尝试过这个

@Schema(required = true, minLength = 12,maxLength = 20)
List<String> elements;

@Size(min=10,max=20)
List<String> elements;

结果:

"Products": {
  "title": "Products",
  "type": "object",
  "properties": {
    "elements": {
      "type": "array",
      "items": {
        "type": "string"
      }
    }
  }
}

有什么想法可以得到这个结果:

"Products": {
    "title": "Products",
    "type": "object",
    "properties": {
        "elements": {
            "type": "array",
            "items": {
                "type": "string",
                "maxLength": 20,
                "minLength": 5
            }
        }
    }
}
spring-boot annotations swagger
2个回答
1
投票

我认为

@ArraySchema
就是您正在寻找的东西,如下:

@ArraySchema(minItems = 12, maxItems = 20)
List<String> elements;

@Schema
用于非数组元素,
@ArraySchema
用于数组元素。两者不能共存。


0
投票

我想这就是你所需要的:

@JsonProperty(value = "elements")
@ArraySchema(arraySchema = @Schema(description = "elements of the request"))
@NotEmpty(message = "Elements cannot be empty")  
private Set<@Pattern(regexp = "^\\d{12,20}$", message = "Invalid element no, valid element nos are between 12 and 20 digits") String> elements = new HashSet<>();

© www.soinside.com 2019 - 2024. All rights reserved.