Невозможно ссылаться на схему компонента, определенную в отдельном файле в Swagger - PullRequest
0 голосов
/ 17 июня 2019

У меня есть следующая документация по 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, я получаю следующую ошибку:

Ошибка решателя в paths./users.get.responses.403.schema.$ref Не удалось разрешить ссылку: Попытался разрешить относительный URL без basePath.путь: 'components.yaml' basePath: 'undefined'

Что мне здесь не хватает?

1 Ответ

0 голосов
/ 17 июня 2019

Так что мне удалось решить эту проблему с помощью этого замечательного ресурса :

Все, что мне нужно было сделать, это добавить ссылку на компоненты в конце файла документации API и изменитьссылка на схему соответственно:

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

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