swagger - 在 swagger 中,是否可以在一个文件中导入多个 yaml 文件?

标签 swagger swagger-editor openapi

对于客户端,这是在 client.yaml 中

/clients:
    get:
      tags:
      - "Clients"
      description: "List Clients The list capability"
      produces:
      - "application/json"
      parameters:
      - name: "tenantIdentifier"
        in: "query"
        required: true
        type: "array"
        items:
          type: "string"
          enum:
          - "default"
      responses:
        200:
          description: "successful operation"
        400:
          description: "Invalid status value"
      security:
      - basicAuth: []
    post:
      tags:
      - "Clients"
      summary: "Create client if address is enabled"
      description: ""
      operationId: "addClient"
      consumes:
      - "application/json"
      produces:
      - "application/json"
      parameters:
      - name: "tenantIdentifier"
        in: "query"
        description: ""
        required: true
        type: "array"
        items:
          type: "string"
          enum:
          - "default"
      - in: "body"
        name: "body"
        description: "Add what do you wnat to add "
        required: true
        schema:
          allOf:
            - $ref: '#/definitions/ClientStructure1'
            - $ref: '#/definitions/ClientStructure2'
            - $ref: '#/definitions/ClientStructure3'
      responses:
        405:
          description: "Invalid input"
      security:
      - basicAuth: []

对于 USER,这是在 user.yaml 中
  /users:
    get:
      tags:
      - "Users"
      summary: "Retrieve list of users"
      produces:
      - "application/json"
      parameters:
      - name: "tenantIdentifier"
        in: "query"
        required: true
        type: "array"
        items:
          type: "string"
          enum:
          - "default"
      responses:
        200:
          description: "successful operation"
        400:
          description: "Invalid status value"
      security:
      - basicAuth: []
    post:
      tags:
      - "Users"
      summary: "Adds new application user."
      description: "Note: Password information is not required (or processed). Password details at present are auto-generated and then sent to the email account given (which is why it can take a few seconds to complete)."
      consumes:
      - "application/json"
      produces:
      - "application/json"
      parameters:
      - name: "tenantIdentifier"
        in: "query"
        description: ""
        required: true
        type: "array"
        items:
          type: "string"
          enum:
          - "default"
      - in: "body"
        name: "body"
        description: "Mandatory Fields: username, firstname, lastname, email, officeId, roles, sendPasswordToEmail"
        required: true
        schema:
           $ref: "#/definitions/StructureForCreateUSer"          
      responses:
        400:
          description: ""
        404:
          description: ""
      security:
      - basicAuth: []

最佳答案

你不能$ref整个路径,但您可以 $ref各个路径的内容。在您的示例中,您可以使用:

paths:
  /clients:
    $ref: clients.yaml#/~1clients
  /users:
    $ref: users.yaml#/~1users
clients.yaml#/~1clients意味着我们采用 clients.yaml文件,然后读取 /clients 的内容该文件中的节点并替换 $ref有了那个内容。 ~1clients/clients/转义为 ~1根据 JSON 指针规则。

为了简化引用,您可以删除 /clients:/users:来自您的 clients.yaml 的节点和 users.yaml文件

# clients.yaml
get:
  description: "List Clients The list capability"
  ...
post:
  summary: "Create client if address is enabled"
  ...

然后使用

paths:
  /clients:
    $ref: clients.yaml
  /users:
    $ref: users.yaml

关于swagger - 在 swagger 中,是否可以在一个文件中导入多个 yaml 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46115805/

相关文章:

json - 没有固定属性列表的对象的 Swagger Yaml 模式定义

datetime - 带有时区信息的 OpenAPI DateTime

Swagger UI - 如何自己显示模型列表

swagger - OpenAPI 中的 'required' 到底是什么意思

java - 为什么 openapi-generator-maven-plugin 忽略我的 xml 标签名称?

swagger - 修改 NestJS 中 OpenAPI (Swagger) 模式中出现的 DTO 名称

java - 如何在java中使用swagger生成开放API规范时排除父类(super class)的属性

typescript - 从 typescript 接口(interface)生成 swagger 文档

c# - 如何在 Nancy.Swagger 包构建的 Swagger-UI 中添加参数示例值?

Swagger 模式不适用于 Swagger UI