Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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: []
like image 673
John Avatar asked Sep 08 '17 11:09

John


People also ask

Where do I put swagger Yaml?

The static yaml file is fetched from Swagger Editor, put it under the resources directory. info: description: "This is a sample server Petstore server.

What is Yaml in swagger?

YAML is a superset of JSON For example, the Swagger UI can read the openapi. json or openapi. yaml files equivalently. Pretty much any parser that reads JSON will also read YAML. However, some JSON parsers might not read YAML because there are a few features YAML has that JSON lacks (more on that below).

Is swagger a JSON or Yaml?

Swagger definitions can be written in JSON or YAML.


1 Answers

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
like image 179
Helen Avatar answered Sep 22 '22 21:09

Helen