Wednesday, 18 March 2020

Swagger Editor: Add Query parameters to API

Using 'parameters' object, you can add query parameters to an API.

Example
paths:
  /employees:
      get:
        parameters: 
          - in: query
            name: from
            description: Page number to return
            required: true
            schema:
              type: integer
              example: 1
          - in: query
            name: size
            description: Number of elements to return
            required: false
            schema:
              type: integer
              example: 10
              minimum: 10
              maximum: 100

Above snippet add two parameters from and size to /employees API. Both the query parameters are of typeinteger and size has minimum value of 10 and maximum value of 100.

data.yaml
openapi: 3.0.0
info:
  title: Customer Data Aceess API
  description: API to expose all the CRUD operations on  customers
  contact:
    name: Krishna
    email: krishna123@abc.com
    url: https://self-learning-java-tutorial.blogspot.com/
  version: 1.0.0
paths:
  /employees:
      get:
        parameters: 
          - in: query
            name: from
            description: Page number to return
            required: true
            schema:
              type: integer
              example: 1
          - in: query
            name: size
            description: Number of elements to return
            required: false
            schema:
              type: integer
              example: 10
              minimum: 10
              maximum: 100
        responses:
          200:
            description: List of all the employees in organization
            content:
               application/json:
                  schema:
                    type: array
                    items:
                      properties:
                        id:
                          type: integer
                          example: 1234
                        name:
                          type: string
                          example: Krishna

Add content of ‘data.yaml’ to the swagger editor, you can see the corresponding API definition in right side of the window.



Previous                                                    Next                                                    Home

No comments:

Post a Comment