I'm using the OpenAPI Generator with the Maven plugin to generate Java Spring code. In my OpenAPI spec, I have a Project schema with a skills array. I want non null items in this array.
The problem is that in the generated Java code, the @NotNull annotation for the array items is missing, which allows null values inside the skills array, which is not the expected behavior.
Here’s the relevant section from my api-spec.yml:
components:
schemas:
Project:
type: object
properties:
skills:
type: array
items:
type: string
nullable: false
minLength: 1
maxLength: 30
In the generated javacode the skills field does not include the @NotNull annotation for array items:
private List<@Size(min = 1, max = 30)String> skills;
I expected it to generate something as below:
private List<@NotNull @Size(min = 1, max = 30) String> skills;
I am using bean validation with Jakarta EE and it is all configured in maven plugin as below.
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>7.7.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/api/api-spec.yml</inputSpec>
<generatorName>spring</generatorName>
<output>${project.build.directory}/generated-sources/openapi</output>
<cleanupOutput>true</cleanupOutput>
<packageName>com.company.project.api</packageName>
<generateSupportingFiles>false</generateSupportingFiles>
<generateModels>true</generateModels>
<generateModelTests>false</generateModelTests>
<generateModelDocumentation>false</generateModelDocumentation>
<modelNamePrefix>Api</modelNamePrefix>
<modelPackage>com.company.project.api.model</modelPackage>
<generateApis>true</generateApis>
<generateApiTests>false</generateApiTests>
<generateApiDocumentation>false</generateApiDocumentation>
<apiPackage>com.company.project.api</apiPackage>
<configOptions>
<sourceFolder>/</sourceFolder>
<dateLibrary>java8</dateLibrary>
<openApiNullable>false</openApiNullable>
<useResponseEntity>true</useResponseEntity>
<interfaceOnly>true</interfaceOnly>
<useSpringBoot3>true</useSpringBoot3>
<skipDefaultInterface>true</skipDefaultInterface>
<annotationLibrary>none</annotationLibrary>
<documentationProvider>none</documentationProvider>
<useBeanValidation>true</useBeanValidation>
<performBeanValidation>true</performBeanValidation>
<useTags>true</useTags>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
How can I ensure that the generated code enforces the non-null constraint for array items?
nullableis the same asnullable: false. the definition oftype: stringis the constraint applied to the value. It can't be any other type, such as: null, boolean, object, array, number