27

I want to deploy an API which is having query string.This is the API

v1/products?q=circuit breaker&locale=en-GB&pageSize=8&pageNo=1&project=GLOBAL

Here is how i am implementing

"/v1/products?q={searchText}&locale={ctrCode}&pageSize={pageSize}&pageNo={pageNo}&project={project}&country={country}":{  
         "get":{  
            "tags":[  
               "Search Text"
            ],
            "summary":"Get Products by searching text, countrycode, page number, pagesize, project and country(optional)",
            "description":"Get Products by searching text, countrycode, page number, pagesize, project and country(optional)",
            "operationId":"getProductName",
            "produces":[  
               "application/json",
               "application/xml"
            ],
            "parameters":[  
               {  
                  "name":"searchText",
                  "in":"path",
                  "description":"The Product that needs to be fetched",
                  "required":true,
                  "type":"string"
               },
               {  
                  "name":"ctrCode",
                  "in":"path",
                  "description":"The Product locale needs to be fetched. Example=en-GB, fr-FR, etc.",
                  "required":true,
                  "type":"string"
               },
               {  
                  "name":"pageSize",
                  "in":"path",
                  "description":"The Product PageSize that needs to be fetched. Example=10, 20 etc.",
                  "required":true,
                  "type":"number"
               },
               {  
                  "name":"pageNo",
                  "in":"path",
                  "description":"The Product pageNo that needs to be fetched. Example=1,2 etc.",
                  "required":true,
                  "type":"number"
               },
               {  
                  "name":"project",
                  "in":"path",
                  "description":"The Project that needs to be fetched. Example=Mypact, DSL etc.",
                  "required":true,
                  "type":"string"
               },
               {  
                  "name":"country",
                  "in":"header",
                  "description":"The Country that needs to be fetched. Example=France, India etc.",
                  "required":false,
                  "type":"string"
               }
            ],
            "responses":{  
               "200":{  
                  "description":"successful operation",
                  "schema":{  
                     "$ref":"#/definitions/Products"
                  }
               },
               "400":{  
                  "description":"Invalid Product_id supplied"
               },
               "404":{  
                  "description":"Product not found"
               }
            }
         }
      }

The country is optional parameter in this. I want the URL should display country only when if user enter some value, else it should not be displayed in the URL.

1
  • 1
    same problem...swagger is stupid here... Commented Jan 12, 2017 at 7:18

3 Answers 3

30

You can't describe query parameters as part of the path in Swagger. You have to declare those explicitly as query parameters.

"/v1/products":{  
         "get":{  
            "tags":[  
               "Search Text"
            ],
            "summary":"Get Products by searching text, countrycode, page number, pagesize, project and country(optional)",
            "description":"Get Products by searching text, countrycode, page number, pagesize, project and country(optional)",
            "operationId":"getProductName",
            "produces":[  
               "application/json",
               "application/xml"
            ],
            "parameters":[  
               {  
                  "name":"searchText",
                  "in":"query",
                  "description":"The Product that needs to be fetched",
                  "required":true,
                  "type":"string"
               },
               {  
                  "name":"ctrCode",
                  "in":"query",
                  "description":"The Product locale needs to be fetched. Example=en-GB, fr-FR, etc.",
                  "required":true,
                  "type":"string"
               },
               {  
                  "name":"pageSize",
                  "in":"query",
                  "description":"The Product PageSize that needs to be fetched. Example=10, 20 etc.",
                  "required":true,
                  "type":"number"
               },
               {  
                  "name":"pageNo",
                  "in":"query",
                  "description":"The Product pageNo that needs to be fetched. Example=1,2 etc.",
                  "required":true,
                  "type":"number"
               },
               {  
                  "name":"project",
                  "in":"query",
                  "description":"The Project that needs to be fetched. Example=Mypact, DSL etc.",
                  "required":true,
                  "type":"string"
               },
               {  
                  "name":"country",
                  "in":"query",
                  "description":"The Country that needs to be fetched. Example=France, India etc.",
                  "required":false,
                  "type":"string"
               }
            ],
            "responses":{  
               "200":{  
                  "description":"successful operation",
                  "schema":{  
                     "$ref":"#/definitions/Products"
                  }
               },
               "400":{  
                  "description":"Invalid Product_id supplied"
               },
               "404":{  
                  "description":"Product not found"
               }
            }
         }
      }
Sign up to request clarification or add additional context in comments.

2 Comments

that is not my problem , the problem is if user does not give anything for country(parameter) the country={country} should not be displayed in the URL
You are right, I forget a certain fix. Edited to reflect that.
10

Your IN parameter needs to be "query" not "path"

This should work:

"parameters": [
  {  
    "name":"country",
    "in":"query",
    "description":"The Country that needs to be fetched. Example=France, India etc.",
    "required":false,
    "type":"string"
  }
]

Comments

2

In editor.swagger.io you can do it like that:

  • no params in your routing
  • params' attribute 'in' has to be 'query'
/v1/products:  
  get:
    tags:
       - Search Text       
    summary: Get Products by searching text, countrycode, page number, pagesize, project and country(optional)
    description: Get Products by searching text, countrycode, page number, pagesize, project and country(optional)
    operationId: getProductName
    parameters: 
      - name: searchText
        in: query
        description: The Product that needs to be fetched
        required: true
        schema:
          type: string
      - name: ctrCode
        in: query
        description: The Product locale needs to be fetched. Example=en-GB, fr-FR, etc.
        required: true
        schema:
          type: string
      - name: pageSize
        in: query
        description: The Product PageSize that needs to be fetched. Example=10, 20 etc.
        required: true
        schema:        
          type: number
      - name: pageNo
        in: query
        description: The Product pageNo that needs to be fetched. Example=1,2 etc.
        required: true
        schema:
          type: number
      - name: project
        in: query
        description: The Project that needs to be fetched. Example=Mypact, DSL etc.
        required: true
        schema:
          type: string

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.