我正在尝试创建 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
}
}
}
}
我认为
@ArraySchema
就是您正在寻找的东西,如下:
@ArraySchema(minItems = 12, maxItems = 20)
List<String> elements;
@Schema
用于非数组元素,@ArraySchema
用于数组元素。两者不能共存。
我想这就是你所需要的:
@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<>();