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

Connexion响应验证未检测额外键值对:配置问题还是机制限制?

Connexion响应验证未检测到额外键值对问题

使用最新稳定版Connexion(2.14.2),其文档说明响应验证可通过jsonschema校验所有响应,在开发阶段尤为实用。但通过以下基础应用及OpenAPI规范测试时,验证机制未检测到响应中包含Schema未定义的JSON键值对'foo':'bar':

OpenAPI规范文件

openapi: "3.0.0"

info:
  title: Hello World
  version: "1.0"
servers:
  - url: /openapi

paths:
  /version:
    get:
      operationId: app.version
      responses:
        200:
          description: Version
          content:
            application/json:
              schema:
                type: object
                properties:
                  version:
                    description: Version
                    example: '1.2.3'
                    type: string

测试代码

import connexion

def version() -> dict:
  return {
    'foo': 'bar',
    'version': '1.2.3'
  }

app = connexion.FlaskApp(__name__)
app.add_api('openapi.yaml', validate_responses=True)
app.run(port=3000)

验证机制整体可正常工作:当version函数返回version为数字5时,会触发如下验证错误:

错误场景代码

def version() -> dict:
  return {
    'foo': 'bar',
    'version': 5
  }

触发的错误响应

{
  "detail": "5 is not of type 'string'\n\nFailed validating 'type' in schema['properties']['version']:\n    {'description': 'Version', 'example': '1.2.3', 'type': 'string'}\n\nOn instance['version']:\n    5",
  "status": 500,
  "title": "Response body does not conform to specification",
  "type": "about:blank"
}

请问是配置有误,还是Connexion/jsonschema本身不校验额外(未知)键值对?


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 16:34:57