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?
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With