You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OpenAPI Generator生成Java Spring代码时未生效数组元素nullable: false约束

问题

使用OpenAPI Generator Maven插件生成Java Spring代码时,已在OpenAPI规范中定义Project schema的skills数组元素不可为null,但生成的Java代码中缺失数组元素的@NotNull注解,导致数组允许存在null值。

OpenAPI规范片段

components:
  schemas:
    Project:
      type: object
      properties:
        skills:
          type: array
          items:
            type: string
            nullable: false
            minLength: 1
            maxLength: 30

当前生成的Java代码

private List<@Size(min = 1, max = 30)String> skills;

期望生成的Java代码

private List<@NotNull @Size(min = 1, max = 30) String> skills;

使用的Maven插件配置

<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</packageName>
                <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>

解决方案

要让生成的代码为数组元素添加@NotNull注解,可通过以下方式实现:

1. 添加数组元素校验配置

在插件的configOptions中新增<validateNullableItems>true</validateNullableItems>配置,该参数会触发生成器识别数组元素的nullable: false约束,自动生成@NotNull注解。

修改后的configOptions片段:

<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>
    <!-- 新增数组元素非空校验配置 -->
    <validateNullableItems>true</validateNullableItems>
</configOptions>

2. 规范版本与注解库适配

  • 确保OpenAPI规范版本为3.0+,nullable字段是OpenAPI 3.0引入的特性,低版本规范无法识别该约束。
  • 若生成的代码未自动引入jakarta.validation.constraints.NotNull,可将annotationLibrary从none改为jakarta.validation,强制生成器关联Jakarta EE的Bean Validation注解库。

内容的提问来源于stack exchange,提问作者Muhammad Huzaifa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 13:27:12