最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

spring boot - openapi-generator strange Validation behaviour - Stack Overflow

programmeradmin5浏览0评论

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.

Share Improve this question asked Feb 5 at 9:46 CoderJammerCoderJammer 7154 gold badges11 silver badges38 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

In 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<>();
发布评论

评论列表(0)

  1. 暂无评论