首页 > 解决方案 > 大摇大摆的编辑器:无法使用 $ref 添加本地引用

问题描述

我正在通过swagger创建一些 API 。

我不明白如何$ref在同一个文件上使用引用...这是我当前的招摇文件:

openapi: 3.0.0
info:
  title: test API
  version: "1.0.0"
  description: This is a sample API.

servers:
  - url: http://api.internal.com/api
paths:
  /sources/:
    get:
      summary: returns all sources given a page and a size
      parameters:
        - name: page
          required: true
          in: query
          description: Limits the number of items on a page
          schema:
            type: integer
        - name: size
          required: true
          in: query
          description: Specifies a page size
          schema:
            type: integer
      responses:
        '200':
          $ref: '#/definitions/myElement'


definitions:
  myElement:
    "data": {
        "sources": [
          {
            "id": "fw5pQ08wMnFfbEE",
            "fileName": "test.csv",
            "size": 1000000,
            "annotatedDate": "2018-10-01 12:00:00",
            "metadataContact": "test@test.com",
            "canBeIngested": true 
          }
        ],
        "stats": {
            "total": 4000,
            "page": 1,
            "size": 20
        }
    }

现在,问题是编辑器向我抛出了这个错误:

Errors
Schema error should NOT have additional properties
additionalProperty: definitions
Jump to line 0

$ref的文档中,我真的找不到任何有用的东西......

我怎样才能解决这个问题?

标签: swagger

解决方案


In OpenAPI 3.0, definitions were replaced with components/schemas. This means you need to use:

      responses:
        '200':
          $ref: '#/components/schemas/myElement'

and

components:
  schemas:
    myElement:
      ...

Also, your model definition for myElement is not valid. See this guide to learn how to describe objects, properties, etc.


推荐阅读