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

使用OpenAPI Generator生成代码时如何避免kotlin.Any类型?

问题:OpenAPI Generator生成Kotlin代码时所有类型均为kotlin.Any

我最近在学习OpenAPI Generator,想用它生成一个简单的API。配置并运行生成器后,整体功能正常,但生成的函数、实体等所有类型都变成了kotlin.Any。

我的Maven插件配置

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <!-- RELEASE_VERSION -->
    <version>6.6.0</version>
    <!-- /RELEASE_VERSION -->
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/openapi.yaml</inputSpec>
                <generatorName>kotlin-spring</generatorName>
                <configOptions>
                    <sourceFolder>src/gen/kotlin/main</sourceFolder>
                    <interfaceOnly>true</interfaceOnly>
                    <useSpringBoot3>true</useSpringBoot3>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

自动生成的/users端点代码

@Operation(
        summary = "Get user information",
        operationId = "usersGet",
        description = """This endpoint requires a valid bearer token""",
        responses = [
            ApiResponse(responseCode = "200", description = "OK", content = [Content(schema = Schema(implementation = kotlin.Any::class))]),
            ApiResponse(responseCode = "401", description = "")
        ],
        security = [ SecurityRequirement(name = "BearerAuth") ]
    )
    @RequestMapping(
            method = [RequestMethod.GET],
            value = ["/users"],
            produces = ["application/json"]
    )
    fun usersGet(): ResponseEntity<kotlin.Any> {
        return ResponseEntity(HttpStatus.NOT_IMPLEMENTED)
    }

OpenAPI YAML中/users端点的定义

/users:
    get:
      summary: Get user information
      description: This endpoint requires a valid bearer token
      security:
        - BearerAuth: []
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/UserResponse'
        "401":
          $ref: '#/components/responses/Unauthorized'
      tags:
        - user

UserResponse模型定义

UserResponse:
      example:
        lastName: Doe
        name: Jhon
        id: 1
        email: jhon@doe.com
        token: fkljasdhf9fjkfh23fhkhiafo9
      properties:
        id:
          description: "Auto generated by the api, not required for any transaction"
          example: 1
          type: integer
        email:
          example: jhon@doe.com
          type: string
        name:
          example: Jhon
          type: string
        lastName:
          example: Doe
          type: string
        token:
          description: Auto generated by the api once you create a user or login with
            valid credentials
          example: fkljasdhf9fjkfh23fhkhiafo9
          type: string
解决方案

检查以下几个关键点:

  • 确认OpenAPI YAML结构完整性
    确保UserResponse严格位于components/schemas节点下,完整结构示例:

    components:
      schemas:
        UserResponse:
          # 你的模型定义内容
    

    同时检查#/components/responses/Unauthorized是否存在且定义正确,无效的引用会导致生成器解析异常,影响类型推导。

  • 补充插件配置
    在configOptions中添加modelPackage指定模型生成的包路径,确保生成器能正确关联模型与接口:

    <configOptions>
      <sourceFolder>src/gen/kotlin/main</sourceFolder>
      <interfaceOnly>true</interfaceOnly>
      <useSpringBoot3>true</useSpringBoot3>
      <modelPackage>com.yourproject.model</modelPackage> <!-- 替换为你的实际包名 -->
    </configOptions>
    
  • 清理并重新生成
    删除已生成的src/gen/kotlin/main目录,执行mvn clean compile重新生成代码,避免旧代码残留干扰。

  • 验证OpenAPI版本
    在yaml开头声明正确的OpenAPI版本(推荐3.0.x以上),示例:

    openapi: 3.0.3
    info:
      title: 你的API名称
      version: 1.0.0
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 23:36:04