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

时间:2019-06-17 13:22:58

标签: swagger swagger-ui openapi swagger-3.0

我有以下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模式是在单独的yaml文件(称为components.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时,出现以下错误:

路径/users.get.responses.403.schema.$ref的解析器错误 无法解析参考:试图解析相对URL,而没有basePath。路径:“ components.yaml” basePath:“未定义”

我在这里想念什么?

1 个答案:

答案 0 :(得分:0)

所以我设法用this great resource解决了这个问题:

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

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

components:
  $ref: './components.yaml'