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

Stoplight Prism引用同目录JSON文件返回Null问题求助

问题解决方案

1. 修正$ref引用路径与指向

你的request_payload.json外层是包含users数组的对象,若你的/users接口响应是用户对象数组,需要用JSON Pointer语法精准引用到数组内的对象结构,而非整个文件:

paths:
  /users:
    get:
      responses:
        '200':
          description: 获取用户列表
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: './request_payload.json#/users/0' # 指向JSON中第一个用户对象作为schema模板

如果接口响应需要返回包含users字段的完整对象,则直接引用整个JSON文件:

paths:
  /users:
    get:
      responses:
        '200':
          description: 获取用户列表
          content:
            application/json:
              schema:
                $ref: './request_payload.json'

2. 验证Mock服务的文件解析能力

部分Mock服务(如Prism)对外部JSON的$ref解析依赖正确的启动路径,确保从api_spec.yaml所在目录启动服务:

prism mock api_spec.yaml

若仍有问题,可先将JSON内容直接嵌入YAML测试,排除文件引用问题:

paths:
  /users:
    get:
      responses:
        '200':
          description: 获取用户列表
          content:
            application/json:
              schema:
                type: array
                items:
                  type: object
                  properties:
                    id: { type: integer }
                    name: { type: string }
              example:
                - id: 1
                  name: "John Doe"
                - id: 2
                  name: "Jame Smith"

3. 调整JSON文件结构(可选)

若接口响应本身就是纯用户数组,可修改request_payload.json为数组格式:

[
  {
    "id": 1,
    "name": "John Doe"
  },
  {
    "id": 2,
    "name": "Jame Smith"
  }
]

再在YAML中直接引用整个文件作为数组schema:

schema:
  $ref: './request_payload.json'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 21:13:14