In swagger, is it possible import multiple yaml files in one single file?

John picture John · Sep 8, 2017 · Viewed 9.2k times · Source

For the Client this one is inside of 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: []

For the USER this is inside of 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: []

Answer

Helen picture Helen · Sep 8, 2017

You can't $ref whole paths, but you can $ref the contents of individual paths. In your example, you can use:

paths:
  /clients:
    $ref: clients.yaml#/~1clients
  /users:
    $ref: users.yaml#/~1users

clients.yaml#/~1clients means we take the clients.yaml file, then read the contents of the /clients node in that file and substitute the $ref with that contents. ~1clients is /clients with / escaped as ~1 according to JSON Pointer rules.


To simplify the references, you can remove the /clients: and /users: nodes from your clients.yaml and users.yaml files

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

and then use

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