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












3















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?










share|improve this question

























  • Give a look to this vojtechruzicka.com/…, it seems that JSR-303 is not working OOB

    – Raffaele
    Dec 6 '18 at 15:18











  • 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?

    – MiguelMunoz
    Dec 9 '18 at 9:46
















3















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?










share|improve this question

























  • Give a look to this vojtechruzicka.com/…, it seems that JSR-303 is not working OOB

    – Raffaele
    Dec 6 '18 at 15:18











  • 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?

    – MiguelMunoz
    Dec 9 '18 at 9:46














3












3








3








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?










share|improve this question
















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?







swagger code-generation bean-validation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 30 '18 at 5:16







MiguelMunoz

















asked Nov 23 '18 at 9:18









MiguelMunozMiguelMunoz

1,8641619




1,8641619













  • Give a look to this vojtechruzicka.com/…, it seems that JSR-303 is not working OOB

    – Raffaele
    Dec 6 '18 at 15:18











  • 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?

    – MiguelMunoz
    Dec 9 '18 at 9:46



















  • Give a look to this vojtechruzicka.com/…, it seems that JSR-303 is not working OOB

    – Raffaele
    Dec 6 '18 at 15:18











  • 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?

    – MiguelMunoz
    Dec 9 '18 at 9:46

















Give a look to this vojtechruzicka.com/…, it seems that JSR-303 is not working OOB

– Raffaele
Dec 6 '18 at 15:18





Give a look to this vojtechruzicka.com/…, it seems that JSR-303 is not working OOB

– Raffaele
Dec 6 '18 at 15:18













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?

– MiguelMunoz
Dec 9 '18 at 9:46





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?

– MiguelMunoz
Dec 9 '18 at 9:46












1 Answer
1






active

oldest

votes


















1














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 (this one is true by default). To undestand 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 with a "format: email". With performBeanValidation=true the generator outputs an @Email validation annotation. With the performBeanValidation=false you will not see this annotation. This can be understood by looking at the following mustache file: beanValidationCore



You can override any 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>





share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443712%2fhow-can-i-use-the-usebeanvalidation-option-when-generating-code-in-swagger%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    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 (this one is true by default). To undestand 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 with a "format: email". With performBeanValidation=true the generator outputs an @Email validation annotation. With the performBeanValidation=false you will not see this annotation. This can be understood by looking at the following mustache file: beanValidationCore



    You can override any 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>





    share|improve this answer






























      1














      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 (this one is true by default). To undestand 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 with a "format: email". With performBeanValidation=true the generator outputs an @Email validation annotation. With the performBeanValidation=false you will not see this annotation. This can be understood by looking at the following mustache file: beanValidationCore



      You can override any 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>





      share|improve this answer




























        1












        1








        1







        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 (this one is true by default). To undestand 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 with a "format: email". With performBeanValidation=true the generator outputs an @Email validation annotation. With the performBeanValidation=false you will not see this annotation. This can be understood by looking at the following mustache file: beanValidationCore



        You can override any 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>





        share|improve this answer















        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 (this one is true by default). To undestand 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 with a "format: email". With performBeanValidation=true the generator outputs an @Email validation annotation. With the performBeanValidation=false you will not see this annotation. This can be understood by looking at the following mustache file: beanValidationCore



        You can override any 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>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 7 '18 at 20:28

























        answered Dec 7 '18 at 20:21









        CodesnooperCodesnooper

        6617




        6617
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443712%2fhow-can-i-use-the-usebeanvalidation-option-when-generating-code-in-swagger%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()