对于 Swagger 多文件配置,可以使用 $ref 关键字来引用其他的 JSON 文件。例如,假设有一个 user.json 文件:

{
  "type": "object",
  "properties": {
    "id": {
      "type": "integer"
    },
    "name": {
      "type": "string"
    }
  }
}

然后可以在另一个主文件中使用 $ref 引用该文件:

{
  "swagger": "2.0",
  "info": {
    "title": "My API",
    "version": "1.0.0"
  },
  "paths": {
    "/users": {
      "get": {
        "summary": "Get a list of users",
        "responses": {
          "200": {
            "description": "OK",
            "schema": {
              "$ref": "user.json"
            }
          }
        }
      }
    }
  }
}

这样,Swagger 会自动读取 user.json 文件,并将其内容作为响应的 schema。

对于其他配置文件的格式,Swagger 支持多种格式,包括 YAML、XML 等。具体使用哪种格式取决于个人喜好和项目需求。在主文件中使用 $ref 引用其他文件时,只需指定文件的路径和文件名即可。例如:

{
  "$ref": "definitions.yaml#/User"
}

这将引用 definitions.yaml 文件中的名为 User 的定义。


原文地址: https://www.cveoy.top/t/topic/sBW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录