Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
desio05 opened this issue Jun 8, 2018 · 5 comments · Fixed by #10856
Closed

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

desio05 opened this issue Jun 8, 2018 · 5 comments · Fixed by #10856
Assignees

Comments

@desio05
Copy link

desio05 commented Jun 8, 2018

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

@joistick11
Copy link

joistick11 commented Jun 8, 2018

+

@cnuonline
Copy link

I too face similar issue.
on a post request consisting of variable as Path and body will not work together.
It is ignoring path while generating the swagger schema validations also.

@desio05
Copy link
Author

desio05 commented Oct 10, 2018

I too face similar issue.
on a post request consisting of variable as Path and body will not work together.
It is ignoring path while generating the swagger schema validations also.

OpenApi Generator doesn't have this issue.
I have started use this tool
https://github.com/OpenAPITools/openapi-generator

@HugoMario HugoMario self-assigned this Oct 10, 2018
@fscarlato
Copy link

fscarlato commented Jul 3, 2019

In my case the plugin won't generate the validation code for path Parameter if their schema is descripted through a ref, e.g.:

components:
  parameters:
    idDominioPath:
      in: path
      name: 'idDominio'
      required: true
      example: '01234567890'
      schema:
        $ref: '#/components/schemas/identificativoDominio'     

Won't generate the validation annotations, e.g. @SiZe(max=255)
Instead, if I do:

 ....
 schema:
     maxLength: 255
      type: "string"

The generator correctly generates the validation annotations in code.

@hleb-kastseika
Copy link

I also faced with the issue - when I described regular expression pattern for path parameter in OpenAPI, it's ignored by codegen. Any ideas how to solve it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants