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

1

12

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?

Finely answered 23/11, 2018 at 9:18 Comment(2)
Give a look to this vojtechruzicka.com/…, it seems that JSR-303 is not working OOBKingcup
Thank you for the link. It clarified a few things, but I'm still confused. It talks about annotations in my model classes. But in my experience, it doesn't read my model classes. Is it talking about annotations that I add to the models that swagger generates? Or is there some other setting that tells it to read my Hibernate databeans?Finely
C
19

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>
Cuba answered 7/12, 2018 at 20:21 Comment(7)
Just adding, in order to add a custom bean validation it is needed to use the prefix "#vendorExtensions" on mustache template e.g {{#vendorExtensions.x-customValidation}}@com.my.CustomValidation{{/vendorExtensions.x-customValidation}}Jubilation
Thank you, this is helpful. But I was hoping for some page in their documentation that explains how to use it. I don't want to have to learn how to read a .mustache file just to find out what a configuration property is for. Isn't this documented somewhere? (I can't find it.)Finely
Not documented to my knowledge. Part of the description in github.com/OpenAPITools/openapi-generator/issues/5637 is in line with your comment.Smasher
"To understand how they work you should look at the mustache templates" - no, I should be able to find the behavior described in the documentation :-/Smasher
I did find some documentation at github.com/OpenAPITools/openapi-generator/blob/master/docs/…. For useBeanValidation it says "Use BeanValidation API annotations," and for performBeanValidation it says "Use Bean Validation Impl. to perform BeanValidation." But when I generate the code, it doesn't matter what I set for these options. The generated code is the same, so I'm still wondering how I can use them.Finely
you must pass these like this: ` --additional-properties=performBeanValidation=true` --additional-properties=useBeanValidation=trueAppropriate
"You can override any of these Mustache templates by copying the original Mustache file from the source location to…" If I'm copying a file and modifying it, what happens if the original file changes? This is not a reliable design. I hope they come up with something better. A build process that relies on modifying source files that may change is fraught with peril. Still, I'm going to try it. But I should say I found some simpler instructions at medium.com/@nh124001/…Finely

© 2022 - 2024 — McMap. All rights reserved.