5

I am using OpenAPI Generator to generate Spring code based on the YAML file as below. But I noticed that Spring Boot validation doesn't work for required properties.

OpenAPI Generator CLI version: 5.2.1

OpenAPI spec file:

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
Helen
  • 87,344
  • 17
  • 243
  • 314
Rajdip Patel
  • 541
  • 1
  • 7
  • 19

1 Answers1

0

Do you have validation dependencies on your classpath? If Spring Boot sees any validation mechanism on your classpath, it will use it. Otherwise, it will launch the server with no validation.

If you're using Maven, you can specify Spring validation with this dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jersey</artifactId>
</dependency>

Or you can use Hibernate validation with this.

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.1.2.Final</version>
</dependency>

(Be flexible on the version here.)

I doubt this is an exhaustive list. These are just the ones I've tried. They both work.

DharmanBot
  • 1,066
  • 2
  • 6
  • 10
MiguelMunoz
  • 4,548
  • 3
  • 34
  • 51