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

13,176

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
Share:
13,176
John
Author by

John

Updated on June 13, 2022

Comments

  • John
    John almost 2 years

    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: []