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

如何用OpenAPI 3.1和Nelmio正确渲染UUID键的HashMap?

问题:在Symfony API文档中渲染UUID键的HashMap(Schema和示例)

期望在API文档的Response schema和Response samples区域正确渲染一个以UUID为键、对象为值的HashMap,目标效果如下:

{
  "objects": {
    "3a34655e-9566-4d5e-bce7-8fa71670993b": {
      "title": "Foo"
    },
    "4bb806a9-fcd1-4a32-b2f0-6cbb45cfc894": {
      "title": "Bar"
    }
  }  
}

开发环境

  • Symfony 6 项目
  • 依赖库:
    • zircote/swagger-php(版本:4.7.10)
    • nelmio/api-doc-bundle(版本:v4.11.1)
  • 兼容OpenAPI 3.0/3.1规范

尝试过的方案及问题

方案1 - 使用AdditionalProperties

/**
 * @var array<string, ObjectDto>
 *
 * @OA\Property(
 *     type="object",
 *     @OA\AdditionalProperties(
 *         type="object",
 *         ref=@Model(type=ObjectDto::class),
 *     ),
 *     description="The objects indexed by ID"
 * )
 */

问题
无法修改Response schema里默认生成的HashMap键名property name*,也没法替换Response samples中的"property1"和"property2",无法自定义键的展示信息。

方案2 - 使用patternProperties

/**
 * @var array<string, ObjectDto>
 *
 * @OA\Property(
 *     type="object",
 *     patternProperties={
 *         "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"=@OA\Property(
 *             type="object",
 *             ref=@Model(type=ObjectDto::class),
 *         )
 *     },
 *     description="The objects indexed by ID"
 * )
 */

问题
Response schema显示正常,但Response samples生成的示例为空:

"objects": { }

疑问

是否遗漏了配置项?能否通过OpenAPI 3.1规范完全控制HashMap的渲染?

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 08:07:21