无法设置swagger -无法读取未定义的属性“parameters "

nue99wik  于 2023-11-18  发布在  其他
关注(0)|答案(1)|浏览(270)

我在node.js应用程序中设置swagger时遇到了问题。我使用swagger-jsdocswagger-ui-express创建文档。以下是版本
“swagger-jsdoc”:“3.5.0”,“swagger-ui-express”:“4.1.3”
下面是我传递给swagger-jsdoc的代码。

openapi: 3.0.0
info:
  description: test
  version: 3.0.0
  title: U-CRM api documentation
  termsOfService: http://swagger.io/terms/
servers:
  - url: 'https://localhost:5000'
    description: Local server
tags:
- name: U-CRM
  description: CRM for university
components:
  parameters:
    $ref: 'components/parameters/index.yml'
  schemas:
    $ref: 'components/schemas/index.yml'
paths:
  $ref: '#/paths/index.yml'

字符串
毕竟,我得到一个错误
无法读取未定义的属性“parameters "
事实上,当我仔细阅读swagger文档时,这对我来说很惊讶。可能是什么问题?

1l5u6lss

1l5u6lss1#

OpenAPI不支持$refeverywhere$ref只能在OpenAPI Specification明确声明字段的值可以是“引用对象”的特定位置使用。
例如,$ref不允许直接在pathscomponents/parameterscomponents/schemas下使用-您只能引用 * 单个 * 路径、参数和模式。
你的例子的正确版本是:

paths:
  /foo:
    $ref: '#/paths/index.yml#/~1foo'  # $ref to root node `/foo` in `paths/index.yml`
  /bar:
    $ref: '#/paths/index.yml#/~1bar'  # $ref to root node `/bar` in `paths/index.yml`

components:
  parameters:
    param1:
      $ref: 'components/parameters/index.yml#/param1'
    param2:
      $ref: 'components/parameters/index.yml#/param2'
  schemas:
    schema1:
      $ref: 'components/schemas/index.yml#/schema1'
    schema2:
      $ref: 'components/schemas/index.yml#/schema2'

字符串
如果你想在任意的地方使用$ref,你必须使用一个可以解析任意$refs的解析器/工具来预处理你的定义;这将给予你一个有效的OpenAPI文件,可以与OpenAPI兼容的工具一起使用。这样的预处理工具之一是json-refs,你可以在这里找到一个预处理的例子。

相关问题