swagger-api / swagger-api/swagger-codegen

Not validated inner arrays in generated code

Open
#9,602 2 comments 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

Broken validation code generated from spec

Swagger-spec
swagger: '2.0'
info:
  version: 0.0.1
  title: test
  description: test
schemes:
- https
host: localhost
consumes:
- application/x-yaml
- application/json
produces:
- application/json
securityDefinitions:
  basicAuth:
    type: basic
  jwtAuth:
    type: apiKey
    in: header
    name: jwt  
paths:
  /exp:
    post:
      description: |
        get vcsp flags of account
      security:
      - jwtAuth: []
      consumes: 
        - application/json
      parameters:
        - in: body
          name: collection
          required: true
          schema:
            $ref: "#/definitions/Collection"                
      responses:
        '200':
          description: purchases information    
          schema:
            $ref: '#/definitions/GlobalResponse'      
        default:
          description: unexpected error
          schema:
            $ref: '#/definitions/GlobalResponse'
definitions:
  GlobalResponse:
    type: object
    required:
    - isSuccess
    - message    
    properties:
      isSuccess:
        type: boolean        
      result:
        type: object          
        example: null
      message:
        type: string
        example: OK
  Collection:
    type: object
    properties:
      collection:
        type: array
        minItems: 2
        items:
          type: object
          properties:
            strings:
              type: array
              minItems: 2
              items:
                type: string
                minLength: 10
        
Swagger-codegen version

swagger codegen maven plugin

<plugin>
				<groupId>io.swagger</groupId>
				<artifactId>swagger-codegen-maven-plugin</artifactId>
				<version>2.4.4</version>
				<executions>
					<execution>
						<goals>
							<goal>generate</goal>
						</goals>
						<configuration>
							<!-- specify the swagger yaml -->
							<inputSpec>swagger.yaml</inputSpec>
							<!-- target to generate java client code -->
							<language>spring</language>
							<!-- hint: if you want to generate java server code, e.g. based on 
								Spring Boot, you can use the following target: <language>spring</language> -->
							<library>spring-boot</library>
							<configHelp>false</configHelp>
							<configOptions>false</configOptions>
							<output>${basedir}/..</output>
							<!-- <templateDirectory>${basedir}/templates</templateDirectory> -->
							<!-- pass any necessary config options -->
							<configOptions>
								<!-- this option allow use you own models in spec -->
								<!-- <import-mappings>ValidatedGeneratorsMap=com.veeam.esb.model.components.ValidatedGeneratorsMap</import-mappings> -->
								<apiPackage>com.veeam.esb.api.generated</apiPackage>
								<artifactDescription>service</artifactDescription>
								<artifactId>test</artifactId>
								<artifactVersion>0.0.1-SNAPSHOT</artifactVersion>
								<basePackage>com.config</basePackage>
								<configPackage>com.config.generated</configPackage>
								<dateLibrary>java8</dateLibrary>
								<delegatePattern>false</delegatePattern>
								<groupId>com.config</groupId>
								<interfaceOnly>true</interfaceOnly>
								<invokerPackage>com.config</invokerPackage>
								<java8>true</java8>
								<!-- <withXml>true</withXml> -->
								<modelPackage>com.config.esb.model.generated</modelPackage>
								<serializableModel>true</serializableModel>
								<!-- <sourceFolder>src/main/java</sourceFolder> -->
								<title>test</title>
								<useBeanValidation>true</useBeanValidation>
								<useOptional>true</useOptional>
								<useTags>true</useTags>
							</configOptions>
						</configuration>
					</execution>
				</executions>
			</plugin>
Steps to reproduce

generated code do not contain validation annotations for primitive types. Example:

/**
 * CollectionCollection
 */
@Validated
@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2019-07-26T17:26:42.200+03:00")

public class CollectionCollection  implements Serializable {
  private static final long serialVersionUID = 1L;

  @JsonProperty("strings")
  @Valid
  private List<String> strings = null;
Suggest a fix/enhancement

Need to improve codegenerator to add validation annotations (in example @Size(min=10) ) to collections:

/**
 * CollectionCollection
 */
@Validated
@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2019-07-26T17:26:42.200+03:00")

public class CollectionCollection  implements Serializable {
  private static final long serialVersionUID = 1L;

  @JsonProperty("strings")
  @Valid
  private List<@Size(min=10) String> strings = null;

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 generated Spring server code with the supplied Swagger specification and Maven plugin configuration. Then inspect the Spring generator's validation handling and templates for nested arrays. Done means the generated model includes the expected collection and element validation annotations, with a regression test covering the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
backend-api-design, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.