Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the useBeanValidation option when generating code in Swagger?

Swagger's code generation for a Spring server has an option called useBeanValidation, but I can't figure out how to use it. I couldn't find any documentation telling me which validations it supports, so I decided to try it out myself. The OpenAPI spec's description of the properties of a schema object lists these properties:

title
multipleOf
maximum
exclusiveMaximum
minimum
exclusiveMinimum
maxLength
minLength
pattern
maxItems
minItems
uniqueItems
maxProperties
minProperties
required
enum

So I tried adding some of these properties to fields of an Object I created. Here's the relevant portion of my .yaml file:

components:
  schemas:
    Dummy:
      type: object
      properties:
        iMinMax:
          type: integer
          format: int32
          minimum: 0
          maximum: 100
        dMinMaxEx:
          type: number
          format: int32
          minimum: 5.0
          maximum: 10.0
          exclusiveMinimum: false
          exclusiveMaximum: true
        dMinExMaxEx:
          type: number
          format: int32
          minimum: 5.0
          maximum: 10.0
          exclusiveMinimum: true
          exclusiveMaximum: true
        dMinExMax:
          type: number
          format: int32
          minimum: 5.0
          maximum: 10.0
          exclusiveMinimum: true
          exclusiveMaximum: false
        sArray:
          type: array
          items:
            type: string
          minItems: 5
          maxItems: 10
          uniqueItems: true
        sLen:
          type: string
          format: text
          minLength: 5
          maxLength: 10

I turned on the bean validation option of the Spring code generator and generated server code, but it didn't have any effect. The code it generated was exactly the same as with the option turned off. Does anyone know how to use Swagger's Bean Validation option?

like image 569
MiguelMunoz Avatar asked Nov 23 '18 09:11

MiguelMunoz


People also ask

Which of the following are the features of Swagger codegen?

Write, host, and version Swagger definitions. Document new and existing APIs with the Swagger (OpenAPI) specification. Quickly generate code in the language of their choosing, and push to source control repositories. Securely work on APIs across teams, with control over who can access API definitions.


1 Answers

There are 2 properties that influence bean validation within the latest version of the generator (3.3.4 last I checked). These properties are performBeanValidation and useBeanValidation (both false by default). To understand how they work you should look at the mustache templates that the generator uses in conjunction with the generator properties. These can be found in the JavaSpring Mustache files.

For example, you will see different behavior with and without performBeanValidation if your API yaml contains an attribute defined with format: email. With performBeanValidation=true the generator outputs an @Email validation annotation. With performBeanValidation=false you will not see this annotation. This can be understood by looking at the following mustache file: beanValidationCore

You can override any of these Mustache templates by copying the original Mustache file from the source location to your own project location and modify it as required. You then supply the project location as a parameter or property to the generator. e.g templateDirectory=src/main/resources/mustache

Blockquote

        <plugin>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <version>${openapi-codegen-version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <inputSpec>api.yaml</inputSpec>
                        <output>target/generated-sources</output>
                        <apiPackage>my.package.api</apiPackage>
                        <modelPackage>my.package.api.model</modelPackage>
                        <generatorName>spring</generatorName>
                        <templateDirectory>src/main/resources/mustache</templateDirectory>
                        <!--<configHelp>true</configHelp>-->
                        <!--<verbose>true</verbose>-->
                        <configOptions>
                            <dateLibrary>java8-localdatetime</dateLibrary>
                            <java8>false</java8>
                            <interfaceOnly>true</interfaceOnly>
                            <performBeanValidation>true</performBeanValidation>
                            <useBeanValidation>true</useBeanValidation>
                        </configOptions>
                    </configuration>
                </execution>
            </executions>
        </plugin>
like image 91
Codesnooper Avatar answered Sep 20 '22 15:09

Codesnooper