swagger-api / swagger-api/swagger-codegen

Validations are not working at runtime.

Open
#12,235 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Validations are not working at runtime.
I am using spring boot - 3.1.4 and swagger-codegen-maven-plugin - 2.4.32.
My configuration is as below :

               <groupId>io.swagger</groupId>
               <artifactId>swagger-codegen-maven-plugin</artifactId>
               <version>2.4.32</version>
               <executions>
                   <execution>
                       <id>swagger-codegen</id>
                       <phase>generate-sources</phase>
                       <goals>
                           <goal>generate</goal>
                       </goals>
                       <configuration>
                           <inputSpec>${project.build.directory}/BOOT-INF/classes/${serviceYamlName}.yaml</inputSpec>
                           <language>spring</language>
                           <apiPackage>com.k2.openapi.${serviceName}.api</apiPackage>
                           <modelPackage>com.k2.openapi.${serviceName}.model</modelPackage>
                           <configOptions>
                               <delegatePattern>true</delegatePattern>
                               <hideGenerationTimestamp>true</hideGenerationTimestamp>
                               <useBeanValidation>true</useBeanValidation>
                               <performBeanValidation>true</performBeanValidation>
                               <library>spring-mvc</library>
                           </configOptions>
                           <addCompileSourceRoot>true</addCompileSourceRoot>
                           <generateSupportingFiles>false</generateSupportingFiles>
                       </configuration>
                   </execution>
               </executions>
           </plugin>

However at runtime, bean validations are not performed for required fields.

Generated interface method has @valid annotation

ResponseEntity createUser(@ApiParam(value = "Created user object" ,required=true ) @Valid @RequestBody User body);

Generated POJO has @validated annotation :

public class User   {

However, I do not see any generated message for required fields :

  @NotNull
  public Long getId() {
    return id;
  }
Swagger-codegen version

2.4.32

Swagger declaration file content or url
swagger: "2.0"
info:
  title: Reflectoring
  description: "Tutorials on Spring Boot and Java, thoughts about the Software Craft, and relevant book reviews. Because it's just as important to understand the Why as it is to understand the How. Have fun!"
  version: 0.0.1-SNAPSHOT
termsOfService: http://swagger.io/terms/
contact:
  email: petros.stergioulas94@gmail.com
license:
  name: Apache 2.0
  url: http://www.apache.org/licenses/LICENSE-2.0.html
externalDocs:
  description: Find out more about Reflectoring
  url: https://reflectoring.io/about/
host: reflectoring.swagger.io
basePath: /v2
tags:
  - name: user
    description: Operations about user
paths:
  /user:
    post:
      tags:
        - user
      summary: Create user
      description: Create user functionality
      operationId: createUser
      consumes:
        - application/json
      produces:
        - application/json
      parameters:
        - in: body
          name: body
          description: Created user object
          required: true
          schema:
            $ref: '#/definitions/User'
      responses:
        "default":
          description: successful operation
  /user/{username}:
    get:
      tags:
        - user
      summary: Get user by user name
      description: Get user functionality
      operationId: getUserByName
      produces:
        - application/json
      parameters:
        - name: username
          in: path
          description: "The name that needs to be fetched. Use user1 for testing."
          required: true
          type: string
      responses:
        "200":
          description: successful operation
          schema:
            $ref: '#/definitions/User'
        "404":
          description: User not found
    put:
      tags:
        - user
      summary: Updated user
      description: This can only be done by the logged in user.
      operationId: updateUser
      consumes:
        - application/json
      produces:
        - application/json
      parameters:
        - name: username
          in: path
          description: name that need to be updated
          required: true
          type: string
        - in: body
          name: body
          description: Updated user object
          required: true
          schema:
            $ref: '#/definitions/User'
      responses:
        "200":
          description: successful operation
          schema:
            $ref: '#/definitions/User'
        "400":
          description: Invalid user supplied
        "404":
          description: User not found
    delete:
      tags:
        - user
      summary: Delete user
      description: This can only be done by the logged in user.
      operationId: deleteUser
      produces:
        - application/json
      parameters:
        - name: username
          in: path
          description: The name that needs to be deleted
          required: true
          type: string
      responses:
        "201":
          description: operation successful
        "400":
          description: Invalid username supplied
        "404":
          description: User not found
definitions:
  User:
    type: object
    required:
      - id
      - username
    properties:
      id:
        type: integer
        format: int64
      username:
        type: string
      firstName:
        type: string
      lastName:
        type: string
      email:
        type: string
      password:
        type: string
      phone:
        type: string
      userStatus:
        type: integer
        description: User Status
        format: int32

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the reported runtime behavior with swagger-codegen-maven-plugin 2.4.32, Spring Boot 3.1.4, and the supplied Swagger definition. Inspect the generated Spring interface and User POJO, then identify the relevant validation generation or runtime integration path; done means required fields trigger bean-validation errors at runtime.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.