redoc-cli v0.13.20中Discriminator功能失效问题求助
问题解决:redoc-cli构建OpenAPI 3.1文档时Discriminator功能不生效
可能的原因及解决方法
1. 版本兼容性问题
redoc-cli 0.13.20属于较早版本,对OpenAPI 3.1.0的多态(discriminator)支持不完善。
- 方案1:升级redoc-cli到最新版本(目前已归为Redocly生态,新版本对OpenAPI 3.1支持更完善),执行命令:
npm install -g redoc-cli@latest - 方案2:将OpenAPI文档版本降级到3.0.3,旧版redoc-cli对3.0规范的支持更稳定。
2. 补充Schema的枚举约束
旧版redoc-cli需要明确指定每个子Schema中鉴别器字段的固定值,才能触发选择框功能。修改你的Schema定义:
components: schemas: test1: title: Test type: object properties: testType: type: string enum: ["ONE"] # 明确指定对应discriminator的映射值 testOne: type: string test2: title: Test 2 type: object properties: testType: type: string enum: ["TWO"] # 明确指定对应discriminator的映射值 testTwo: type: number test: title: Test Polymorph oneOf: - $ref: '#/components/schemas/test1' - $ref: '#/components/schemas/test2' discriminator: propertyName: testType mapping: ONE: '#/components/schemas/test1' TWO: '#/components/schemas/test2'
3. 验证文档合规性
检查OpenAPI文档是否存在语法问题,确保discriminator的mapping路径正确,oneOf引用的Schema无拼写错误。
内容的提问来源于stack exchange,提问作者Ondřej Navrátil
相关产品推荐
相关产品推荐

