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

OpenAPI Java生成器大小写敏感与保留词冲突问题咨询

OpenAPI Java生成器问题解决方案

问题1:大小写差异的Schema导致编译错误

仅大小写不同的ExtendedBasicdata和Extendedbasicdata,生成器默认大小写不敏感,会将两者合并处理,最终生成的文件名和类名不匹配,触发Class 'Extendedbasicdata' is public, should be declared in a file named 'Extendedbasicdata.java'编译错误。

解决办法:开启生成器的大小写敏感模式,通过case-sensitive=true配置参数实现:

  • 命令行方式:
    openapi-generator generate -i your-spec.yaml -g java --additional-properties case-sensitive=true
    
  • Maven插件方式,在插件配置中添加:
    <configuration>
      <additionalProperties>
        <case-sensitive>true</case-sensitive>
      </additionalProperties>
    </configuration>
    

开启后,生成器会区分大小写不同的Schema,分别生成ExtendedBasicdata.java和Extendedbasicdata.java文件,类名与文件名严格匹配,解决编译错误。

问题2:自定义Objects类与java.util.Objects冲突

生成器默认导入java.util.Objects,导致无法生成同名的自定义类,可通过生成时重命名该Schema对应的类解决:

使用model-renames配置参数指定重命名规则:

  • 命令行方式:
    openapi-generator generate -i your-spec.yaml -g java --additional-properties model-renames=objects:CustomObjects
    
  • Maven插件方式,在additionalProperties中添加:
    <configuration>
      <additionalProperties>
        <model-renames>objects:CustomObjects</model-renames>
      </additionalProperties>
    </configuration>
    

配置后,原objects Schema会生成名为CustomObjects的Java类,避免与java.util.Objects冲突。

涉及的Schema片段

"ExtendedBasicdata": {
      "type": "object",
      "properties": {
        "ExtendedBasicData": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Extendedbasicdata"
          }
        }
      }
    },
    "Extendedbasicdata": {
      "type": "object",
      "properties": {
        "ID": {
          "type": "string"
        },
        "Country": {
          "type": "string"
        }
      }
    },
   "objects": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string"
        },
        "label": {
          "type": "string"
        },
        "entityId": {
          "type": "string"
        }
      }
    }

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 04:10:22