我正在通过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的文档中我真的找不到任何有用的东西......
我该如何解决这个问题?
答案 0 :(得分:1)
在OpenAPI 3.0中,definitions
已替换为components/schemas
。这意味着您需要使用:
responses:
'200':
$ref: '#/components/schemas/myElement'
和
components:
schemas:
myElement:
...
此外,myElement
的模型定义无效。请参阅this guide以了解如何描述对象,属性等。