Search code examples
spring-bootswaggermaven-pluginswagger-codegen

Swagger code generation for String's maxlength is not translated in model class generated


I have maxlength defined for the string attributes in swagger document as mentioned below for one property city for Address. But the code generated for the swagger for this class, does not have any validation for the length validation. As far as I know theoretically, the maxlength should be validated in the generated code with @Size annotation. I use maven plugin to generate the code from swagger.

           <plugin>
            <groupId>io.swagger.codegen.v3</groupId>
            <artifactId>swagger-codegen-maven-plugin</artifactId>
            <version>3.0.33</version>
           </plugin>

The swagger has the String properties like this:

      city:
       description: The city name.
       maxLength: 60
       type: string

The code generated has the model class Address with city as an attribute without any validation on length:

  @JsonProperty("city")
  private String city = null;

Please share your inputs how the maxlength can be translated/generated as code in the model class generated from swagger document.


Solution

  • I found the fix. I used maven plugin for the code generation. I added this property along with other properties and the annotations were generated for @Size etc. in the model classes. The annotations are generated at getters though but I assume the validations should be there while setting the data, but still I need to test that.

      <configOptions>
        <useBeanValidation>true</useBeanValidation>
      </configOptions>