Top Swagger Interview Questions (2021) | CodeUsingJava
















Most frequently Asked Swagger Interview Questions


  1. What is swagger?
  2. What is API Testing?
  3. What are the tools used for API testing?
  4. What is Swagger Editor?
  5. How to Add API in swagger?
  6. How to add new Operation for an API?
  7. How to import Swagger APIs into Postman?
  8. How can we turn off swagger-ui in production?
  9. How can we mutually define exclusive query parameters in Swagger?
  10. How can we define empty array in swagger?

What is swagger?

Swagger acts as an Interface Description Language for describing RESTful API expressed using JSON, as it is used together with set of an open source software tool for designing, building, documenting and using RESTful web services.Swagger is a set of rules which can be used for sharing documentation among product manegers, testers and developers.
Swagger is used from developing APIs with the OpenAPI Specification as it consists of open source and professional tools, catering for almost every need and use case.It also includes automated documentation, code generation, and test-case generation.


What is API Testing?

API are protocols that are used for building the software application.This application software that consistes of multiple APIs which can help in performing Application Programming Interface(API) testing.
API form of testing includes interaction between various multiple APIs and the interaction between API and application program, it mainly includes making API calls with the help of software and observing system response after receiving the output.


What are the tools used for API testing?

Tools used for API testing are SOAPUI, Runscope, LOADUI, Automated API testing and Curl.

What is Swagger Editor?

Swagger Editor is used as a tool for getting started with the swagger specification, it create new APIs or updating existing in a powerful Ecitor which visually renders our swagger and provides real time error feedback.

How to Add API in swagger?

We can add API in swagger by using the code below:

/**
 * @SWG\Resource(
 *     apiVersion="1.0",
 *     swaggerVersion="1.2",
 *     description="Search users"
 * )
 */



How to add new Operation for an API?

      @SWG\Api(
          path="/search",
          @SWG\Operation(
              method="POST",
              summary="Get users near your location",
              type="Users list",
              @SWG\Parameters(
                  @SWG\Parameter(
                      name="interests",
                      paramType="form",
                      description="comma seperated looking for",
                      required=false,
                      type="string",
                      minimum="1.0"
                  ),
                  @SWG\Parameter(
                      name="page",
                      paramType="form",
                      description="page number",
                      required=true,
                      type="string",
                      minimum="1.0"
                  ),
              ),
              @SWG\ResponseMessage(code=400, message="Invalid Data")
          )
      )


How to import Swagger APIs into Postman?

We can import swagger APIs into Postman by using the following steps:
Click on the 'Import' button in the top left corner of Postman UI.
You will see multiple options to import the API doc. Click on the Paste Raw Text.
Paste the JSON format in the text area and click import.
You will see all your APIs as 'Postman Collection' and can use it from the Postman.

Swagger


How can we turn off swagger-ui in production

We use swagger for configuration into separate configuration class and annotate it with @Profile annotation.

@Configuration
@EnableSwagger2
@Profile("dev")
public class SwaggerConfig {
    // your swagger configuration
}



How can we mutually define exclusive query parameters in Swagger?


"parameters": [
                    {
                        "name": "username",
                        "description": "Fetch username by username/email",
                        "required": false,
                        "type": "string",
                        "paramType": "query"
                    },
                    {
                        "name": "site",
                        "description": "Fetch username by site",
                        "required": false,
                        "type": "string",
                        "paramType": "query"
                    },
                    {
                        "name": "survey",
                        "description": "Fetch username by survey",
                        "required": false,
                        "type": "string",
                        "paramType": "query"
                    }
                ],



How can we define empty array in swagger

We can string data in swagger like this:

"photoUrls" : {
    "type":"array",
    "items":{
         "type":"string"
    }
}
The output of the above code will be:

"photoUrls":[
    "string"
]