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

升级OpenAPI后出现isListContainer未解析引用错误求助

问题:升级OpenAPI后CodegenAmender中isListContainer引用报错

升级OpenAPI后,自定义实现CodegenAmender接口的PropertyGeneralizer类里,使用it.isListContainer时出现编译错误:error: unresolved reference: isListContainer,但it.complexType能正常被识别。我发现新版本的pojoVars.moustache文件有变更,自己的包也包含该文件,尝试修改moustache文件添加相关代码块,还在自有文件里补充了isListContainer相关代码,但问题依然没解决。

自定义PropertyGeneralizer类代码

import org.openapitools.codegen.CodegenModel

class PropertyGeneralizer : CodegenAmender {
    override fun amendModels(models: List<CodegenModel>) {
        val generalizations = findGeneralizations(models)

        models.flatMap { it.vars }
                .forEach {
                    if (it.dataType in generalizations) {
                        it.generalizedType = "${it.dataType}View"
                    }

                    if (it.isListContainer && it.complexType in generalizations) {
                        it.generalizedType = "${it.complexType}View"
                    }
                }
    }
}

尝试修改的moustache代码

{{#isListContainer}}
  public {{classname}} add{{nameInCamelCase}}Item({{{items.datatypeWithEnum}}} {{name}}Item) {
    if (this.{{name}} == null) {
      this.{{name}} = {{{defaultValue}}};
    }

    this.{{name}}.add({{name}}Item);
    return this;
  }

  public {{classname}} remove{{nameInCamelCase}}Item({{{items.datatypeWithEnum}}} {{name}}Item) {
    if ({{name}}Item != null && this.{{name}} != null) {
      this.{{name}}.remove({{name}}Item);
    }

    return this;
  }
  {{/isListContainer}}

自有文件中添加的代码

{{! Adder for Generalized List types }}
    public {{classname}} {{vendorExtensions.x-list-adder-function-name}}({{{vendorExtensions.generalizedType}}} {{items.vendorExtensions.singleElementName}}) {
        if (this.{{name}} == null) {
            this.{{name}} = new ArrayList<>();
        }

        this.{{name}}.add({{items.vendorExtensions.singleElementName}}.as{{items.complexType}}
        this.{{name}}.add({{items.vendorExtensions.singleElementName}}.as{{items.isListContainer }}
());
        return this;
    }
解决方案

1. 适配OpenAPI Codegen版本的API变更

OpenAPI Codegen升级后,CodegenProperty类的字段逻辑发生了变化:原isListContainer字段已被替换,需要通过isContainer结合containerType来判断列表类型。修改代码中的判断逻辑:

// 替换原有it.isListContainer的判断逻辑
if ((it.isContainer && it.containerType == "LIST") && it.complexType in generalizations) {
    it.generalizedType = "${it.complexType}View"
}

2. 确认CodegenProperty可用字段

直接查看当前版本OpenAPI Codegen的CodegenProperty源码,确认可用的字段:

  • 容器类型判断依赖isContainer(布尔值)和containerType(可选值包括LIST、SET、MAP等)两个字段。

3. 修正模板代码的语法错误

你在自有文件中添加的代码存在语法错误,模板变量使用方式不正确,修正后的示例:

{{! Adder for Generalized List types }}
{{#isContainer}}
{{#containerTypeIsLIST}}
    public {{classname}} {{vendorExtensions.x-list-adder-function-name}}({{{vendorExtensions.generalizedType}}} {{items.vendorExtensions.singleElementName}}) {
        if (this.{{name}} == null) {
            this.{{name}} = new ArrayList<>();
        }
        this.{{name}}.add({{items.vendorExtensions.singleElementName}}.as{{items.complexType}}());
        return this;
    }
{{/containerTypeIsLIST}}
{{/isContainer}}

4. 确保自定义模板被正确加载

检查自定义pojoVars.moustache文件的配置:

  • 放置路径需为src/main/resources/openapi-generator/templates(默认路径)。
  • 在OpenAPI生成配置中明确指定使用自定义模板,避免默认模板覆盖修改。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 19:40:16