Skip to content

[JAVA] Path variable validation doesn't work #8296

@desio05

Description

@desio05
Description

Hello, at the moment swagger-codegen has incomplete validation. Body of the request is validated normally, but path validation doesn't work at all.
The root cause of the problem is that controller (or controller interface) doesn't have annotation @validated. There is workaround if I extends controller directly, but then a need to repeat all parameters annotations to make it works.
Good decision for me is to use delegates, but then I can't manually add @validated annotation to controller bean.

Swagger-codegen version

2.3.1

Swagger declaration file content or url
swagger: '2.0'
info:
  version: 0.0.1
  title: swagger-codegen-validation
paths:
  '/swagger/validation/{id}':
    post:
      consumes:
        - application/json
      produces:
        - application/json
      parameters:
        - in: path
          name: id
          required: true
          type: string
          pattern: '\d*'
        - in: body
          name: RequestBody
          required: true
          schema:
            $ref: '#/definitions/SwaggerRequestBody'
      responses:
        default:
          schema:
             type: object
             properties:
               answer:
                 type: string

definitions:
  SwaggerRequestBody:
    type: object
    properties:
      name:
        type: string
    required:
      - name
swagger-codegen-maven plugin configuration
<plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>2.3.1</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <!-- specify the swagger yaml -->
                <inputSpec>${project.basedir}/src/main/resources/swagger.yaml
                </inputSpec>
                <!-- target to generate -->
                <output>${project.basedir}/target/generated-sources/swagger</output>
                <!-- pass any necessary config options -->
                <configOptions>
                    <dateLibrary>java8</dateLibrary>
                    <serializableModel>true</serializableModel>
                    <generateApis>true</generateApis>
                    <generateApiTests>false</generateApiTests>
                    <generateModels>true</generateModels>
                    <generateModelTests>false</generateModelTests>
                    <basePackage>${default.package}</basePackage>
                    <configPackage>${default.package}.config</configPackage>
                    <sourceFolder>api</sourceFolder>
                    <useTags>true</useTags>
                    <delegatePattern>true</delegatePattern>
                </configOptions>
                <library>spring-boot</library>
                <apiPackage>${default.package}.controller</apiPackage>
                <modelPackage>${default.package}.model</modelPackage>
                <invokerPackage>${default.package}.handler</invokerPackage>
                <ignoreFileOverride>${project.basedir}/.swagger-codegen-ignore</ignoreFileOverride>
            </configuration>
        </execution>
    </executions>
</plugin>
Suggest a fix/enhancement

Add @validated annotation to spring controller's moustache if useValidation flag is true

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions