Rio85
Rio85

Reputation: 83

how can set properties swagger to list of String List<String>

I am trying to create springDoc swagger documentation, and I'd like to represent a data like this List<String> elements; having some properties like minSize and MaxSize / minLength Max length for values in list. I tried with this

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

and

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

result:

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

Any idea To have this result :

"Products": {
    "title": "Products",
    "type": "object",
    "properties": {
        "elements": {
            "type": "array",
            "items": {
                "type": "string",
                "maxLength": 20,
                "minLength": 5
            }
        }
    }
}

Upvotes: 1

Views: 2514

Answers (3)

andrey shirokov
andrey shirokov

Reputation: 1

I think use @ArraySchema

@ArraySchema(schema = @Schema(type="string", minLength = 5,maxLength = 20))
private List<String> elements;

Upvotes: 0

astar
astar

Reputation: 315

I think this is what you need :

@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<>();

Upvotes: 0

Jo&#227;o Dias
Jo&#227;o Dias

Reputation: 17460

I think @ArraySchema is what you are looking for as follows:

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

@Schema is to be used for non-array elements and @ArraySchema is to be used for array elements. Both cannot coexist.

Upvotes: 1

Related Questions