无法引用 Swagger 中单独文件中定义的组件架构

2024-01-10

我有以下api文档:

swagger: "3.0"
info:
  version: 0.0.1
  title: Test API
paths:
  /users:
    get:
      summary: Get all registered users
      produces:
      - application/json
      responses:
        200:
          description: Users successfully returned
        403:
          description: User not authorised to call this API
          schema:
            $ref: 'components.yaml#/components/schemas/AuthError'

其中 AuthError 架构是在名为 Components.yaml 的单独 yaml 文件中定义的:

components:
  schemas:
    AuthError:
      type: object
      properties:
        error:
          type: sting
          description: Error message

以及 Swagger 配置:

const swaggerDefinition = {
  info: {
    title: 'FlexiWAN REST API documentation',
    version: '1.0.0',
    description: 'This is the REST API for FlexiWAN management',
  },
  components: {},
  host: 'local.flexiwan.com:3443',
  basePath: '/api',
  securityDefinitions: {
    JWT: {
        type: 'apiKey',
        in: 'header',
        name: 'Authorization',
        description: "",
    }
  }

};
const options = {
  swaggerDefinition,
  apis: ['./swagger/**/*.yaml'],
};

const swaggerSpec = swaggerJSDoc(options);
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));

但是当我尝试访问 Swagger UI 时出现以下错误:

paths./users.get.responses.403.schema.$ref 处的解析器错误 无法解析引用:尝试解析相对 URL,但没有基本路径。路径:'components.yaml' 基本路径:'未定义'

我在这里缺少什么?


所以我设法使用解决这个问题这个很棒的资源 https://azimi.me/2015/07/16/split-swagger-into-smaller-files.html:

我所要做的就是在 API 文档文件末尾添加对组件的引用,并相应地更改架构引用:

  403:
    description: User not authorised to call this API
    schema:
      $ref: '#components/schemas/AuthError'

components:
  $ref: './components.yaml'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法引用 Swagger 中单独文件中定义的组件架构 的相关文章

随机推荐