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

OpenAPI Generator Maven插件处理含相对路径$ref的YAML无法生成Java代码如何解决

问题根因

你遇到的StringIndexOutOfBoundsException异常是5.1.0版本openapi-generator-maven-plugin内置的swagger-parser组件存在Windows绝对路径解析缺陷:当inputSpec使用盘符开头的本地绝对路径时,解析同目录下的相对路径$ref会触发字符串下标计算错误。

解决方法

按优先级从高到低选择即可:

  • 方案1:升级插件版本
    直接升级openapi-generator-maven-plugin到最新稳定版(7.x及以上),官方已修复该路径解析bug,无需修改其他配置即可正常解析相对路径$ref。
  • 方案2:修改inputSpec为相对路径
    若因项目限制不能升级插件版本,将inputSpec的绝对路径改为相对于maven执行根目录的相对路径,示例:
    <inputSpec>${project.basedir}/src/main/resources/openapi/provMnS.yaml</inputSpec>
    
  • 方案3:显式指定引用根目录
    必须使用绝对路径的场景下,在configuration节点下添加basePath配置指定YAML文件的根目录:
    <basePath>C:/Users/xxxxx/Documents/Docs/Project/xxxxyy/workspace1/MnS-Rel-16/MnS-Rel-16/OpenAPI/</basePath>
    
完整配置示例
<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>7.6.0</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/openapi/provMnS.yaml</inputSpec>
                <generatorName>spring</generatorName>
                <modelPackage>com.xxx.xxx.dto.etsi.moi</modelPackage>
                <resolveFully>true</resolveFully>
                <configOptions>
                    <interfaceOnly>true</interfaceOnly>
                    <skipDefaultInterface>true</skipDefaultInterface>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>
验证注意事项
  • 确认所有$ref引用的文件路径与实际存储路径完全匹配,大小写一致
  • 确认引用的YAML文件语法合法,components/schemas节点结构正确

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 08:06:02