【问题标题】:swagger split definition yaml - model structure not shown大摇大摆的拆分定义yaml - 模型结构未显示
【发布时间】:2016-03-17 10:09:33
【问题描述】:

我正在使用 swagger 0.7.5 并尝试将 swagger.yaml 文件拆分为多个。我使用swagger project create sample-api 命令创建项目。选择的框架是express。执行这些命令创建了一个带有 ExpressJS 设置和默认 swagger.yaml 文件的 nodejs 项目。由于我的目标是大型应用程序,因此我想将 yaml 文件拆分为多个。根据文档,我已经开始将响应模型外部化到外部文件,这就是它的外观

swagger.yaml

swagger: "2.0"
info:
  version: "0.0.1"
  title: Hello Service
host: localhost:10010
basePath: /
... #skipping code for brevity
paths:
  /hello:
    x-swagger-router-controller: user
    get:
      description: Say hello
      operationId: hello
      parameters:
        - name: name
          in: query
          description: The name of the person to whom to say hello
          required: false
          type: string
      responses:
        "200":
          description: Success
          schema:
            # a pointer to a definition
            $ref: "#/definitions/HelloResponse"
        default:
          description: Error
          schema:
            $ref: "#/definitions/ErrorResponse"
  /swagger:
    x-swagger-pipe: swagger_raw
definitions:
  HelloResponse:
    $ref : models/response/hello_response.yaml
  ErrorResponse:
    required:
      - message
    properties:
      message:
        type: string

models/response/hello_response.yaml

type: string
properties:
  message:
    type: string

在项目目录的根目录执行swagger project start命令后,服务启动,并在浏览器中显示文档。问题是,swagger doc 不显示外部化 yaml 文件的结构并显示以下错误消息。

项目的目录结构如下所示

- api  
-- swagger
--- swagger.yaml
- config
-- config.yaml
- models
-- response
--- hello_response.yaml
- app.js

注意:我尝试在swagger.yaml 级别复制models 目录,但也没有用。

【问题讨论】:

    标签: yaml swagger swagger-ui swagger-2.0


    【解决方案1】:

    没关系,在尝试了很多选项后,我终于通过在app.js 中添加以下行来解决这个问题

    app.use("/models", express.static("models"));
    

    【讨论】:

      【解决方案2】:

      补充 CuriousMind 的回应

      const appRoot = require('app-root-path');
      
      app.use("/paths", express.static(`${appRoot}/api/swagger/paths`));
      app.use("/models", express.static(`${appRoot}/api/swagger/models`));
      

      尝试后:http://localhost:port/paths/yourfile.yaml

      【讨论】:

        猜你喜欢
        • 2021-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-05
        • 1970-01-01
        • 2018-08-17
        • 1970-01-01
        相关资源
        最近更新 更多