OpenAPI 生成的 Spring Boot 服务器不验证所需的属性

Raj*_*tel 5 spring-boot openapi-generator

我正在使用 OpenAPI Generator 基于 YAML 文件生成 Spring 代码,如下所示。但我注意到 Spring Boot 验证不适用于所需的属性。

OpenAPI 生成器 CLI 版本:5.2.1

OpenAPI 规范文件:

openapi: "3.0.3"
info:
  title: Example API
  version: "0.1.0"

paths:
  # AUTH
  /auth/login:
    post:
      operationId: authLogin
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/AuthLoginRequest"
        required: true
      responses:
        "200":
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/AuthLoginResponse"
          description: Session created successfully
      security: []
      summary: Creates a new session
      tags:
        - AUTH - Session management

components:
  schemas:
    AuthLoginRequest:
      type: object
      properties:
        username:
          type: string
        password:
          type: string
      required:
        - username
        - password

    AuthLoginResponse:
      type: object
      properties:
        token:
          type: string
Run Code Online (Sandbox Code Playgroud)

Mig*_*noz 0

您的类路径是否有验证依赖项?如果 Spring Boot 在类路径上发现任何验证机制,它将使用它。否则,它将启动服务器而不进行验证。

如果您使用 Maven,则可以使用以下依赖项指定 Spring 验证:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

或者您可以使用 Hibernate 验证来实现这一点。

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.1.2.Final</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

(这里的版本要灵活。)

我怀疑这是一个详尽的清单。这些只是我尝试过的。他们都工作。