Spring 3.4.1, java 21.0.5, openapi-generator-maven-plugin 7.10.0. Now I have a really simple .yaml as follows:
Role:
type: object
properties:
id:
type: integer
format: int64
description:
type: string
minLength: 3
maxLength: 20
users:
type: array
items:
type: string
required:
- description
The result is:
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2025-02-05T10:09:59.616364500+01:00[Europe/Rome]", comments = "Generator version: 7.10.0")
public class Role {
private Long id;
private String description;
@Valid //jakarta.validation.Valid
private List<String> users = new ArrayList<>();
// getter & setter with JsonProperty and more
...................
}
Is that @Valid
annotation correct above a String array? You can obtain the sane result creating classes from the online generator clicking generate server -> spring.
Spring 3.4.1, java 21.0.5, openapi-generator-maven-plugin 7.10.0. Now I have a really simple .yaml as follows:
Role:
type: object
properties:
id:
type: integer
format: int64
description:
type: string
minLength: 3
maxLength: 20
users:
type: array
items:
type: string
required:
- description
The result is:
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2025-02-05T10:09:59.616364500+01:00[Europe/Rome]", comments = "Generator version: 7.10.0")
public class Role {
private Long id;
private String description;
@Valid //jakarta.validation.Valid
private List<String> users = new ArrayList<>();
// getter & setter with JsonProperty and more
...................
}
Is that @Valid
annotation correct above a String array? You can obtain the sane result creating classes from the online generator clicking generate server -> spring.
1 Answer
Reset to default 0In your case the @Valid
is not needed.
This is because List<String>
is a simple(singular) object.
If let's say you were to use something like List<User>
then in this case we are calling several objects inside the List and the @Valid
notation would be acceptable in this case. This ensures that each user object in the list is validated. We usually use @Validate
when you are triggering validation of nested objects.
So it can be something like this,
@Valid
private List<User> users = new ArrayList<>();