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

如何在Swagger(OpenAPI3)中正确声明Map of Map嵌套映射类型

OpenAPI 3 嵌套Map类型响应配置方案

org.hidetake.swagger.generator 2.19.2版本对内联编写的多层additionalProperties结构存在类型推导缺陷,直接嵌套编写配置时无法正确识别第二层Map的泛型参数,会生成缺失泛型的错误代码。

正确配置方式是将两层Map分别定义为命名Schema,通过$ref引用,让代码生成器可以完整识别类型链:

components:
  schemas:
    # 内层结构:Map<String, String>
    InnerStringMap:
      type: object
      additionalProperties:
        type: string
    # 外层结构:Map<String, Map<String, String>>
    NestedMapResponse:
      type: object
      additionalProperties:
        $ref: '#/components/schemas/InnerStringMap'

# 接口定义部分
paths:
  /content: # 替换为实际接口路径
    get:
      operationId: getContent
      responses:
        '200':
          description: successful operation
          content:
            '*/*':
              schema:
                $ref: '#/components/schemas/NestedMapResponse'

按上述配置生成的接口方法返回值为ResponseEntity<Map<String, Map<String, String>>>,无编译错误。

注意事项:

  • 不要直接内联编写两层及以上的additionalProperties结构,该版本插件对内联嵌套的泛型推导存在已知问题
  • 嵌套层级超过1层、或需要复用的对象结构,建议抽离为独立命名Schema通过引用方式使用,可最大程度避免代码生成的类型错误

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 19:03:41