Table of contents
1.
Introduction
2.
What is Swagger?
2.1.
Basic Structure of Swagger File
2.1.1.
Metadata
2.1.2.
Base URL
2.1.3.
Consumes, Produces
2.1.4.
Paths
2.1.5.
Parameters
2.1.6.
Responses
2.1.7.
Input and Output Models
2.1.8.
Authentication
2.2.
Swagger Tools
2.2.1.
SwaggerHub
2.2.2.
Swagger Editor
2.2.3.
Swagger UI
2.2.4.
Swagger Codegen
3.
How to Import RESTful Requests from Swagger 2.0 in Katalon
4.
Frequently Asked Questions
4.1.
What are RESTful Requests?
4.2.
What are the four Swagger Tools?
4.3.
Write some of the Swagger Data types?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Import RESTful Requests from Swagger 2.0 in Katalon

Author Komal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
OG Image

Introduction

Hello Ninjas! How’s the learning going? Well, In this article, We will gain an understanding of Swagger (Open API 2.0) and How to import Restful requests from Swagger 2.0 in Katalon Studio. So, get ready to learn something new and practical. Let’s begin, shall we?

 

Swagger text

What is Swagger?

We will begin by getting our concepts straight about Swagger, the Basic Structure of a Swagger File, and some commonly used Swagger Tools.

So, Swagger is a specification that provides a set of rules to describe and document the RESTful APIs. Alternatively, Swagger is an open-source framework used to design and describe API. It is also referred to as Open API 2.0. The version Swagger 1.1 was released in 2012, and after some revisions, 2014 marked the release of Swagger 2.0.

Basic Structure of Swagger File

Swagger can be written in JSON or YAML, but it is recommended to write it in YAML as it is easier to understand.

 

Image depicting Structure of a Swagger File

#metadata
swagger: "2.0"
info:
  title: TEST API
  description: API description goes here
  version: 1.0.0
#base URL
host: api.example.com
basePath: /base
schemes:
  - https
#paths
paths:
  /articles:
    get:
      summary: Returns a list of articles.
      description: Description of articles
      produces:
        - application/json
      responses:
        200:
          description: OK

Metadata

Metadata contains the Swagger version which defines the overall structure and representation of an API specification, the API info which consists of title and description(optional and can be multiline), and the version. Info can also have fields that specify license and contact information.

swagger: "2.0"
info:
  title: TEST API
  description: API description goes here
  version: 1.0.0

Base URL

The base URL for the API calls is defined with the help of schemes, host, and basePath:

host: api.example.com
basePath: /base
schemes:
  - HTTPS

All the API paths are relative to the base URL. For example, if ‘ /base ’ is the base path,  ‘/articles’ refer to https://api.example.com/base/articles

Consumes, Produces

The 'consumes' section and 'produces' section define the MIME types supported by the API. 

consumes:
  - application/json
  - application/xml
produces:
  - application/json
  - application/xml

Paths

The ‘paths’ section specifies the paths in your API, and the HTTP methods supported by the endpoints. For e.g., GET /articles can be described as

paths:
  /articles:
    get:
      summary: Returns a list of articles.
      description: Description of articles
      produces:
        - application/json
      responses:
        200:
          description: OK
 

Parameters

The Operations have parameters which can be passed through the URL path (e.g. ‘/articles/{articleId}’, query string- ‘/articles?role=admin’, headers (X-CustomHeader: Value), and the request body.

paths:
  /articles/{articleId}:
    get:
      summary: Returns an article by ID.
      parameters:
        - in: path
          name: articleId
          required: true
          type: integer
          minimum: 1
          description: Parameter description
      responses:
        200:
          description: OK
 

Responses

For each operation, various possible status codes are defined, such as ‘200 OK’ or ‘404 Not Found’, and the schema of the response body. Schemas can be either defined inline or can be referenced from an external definition via ‘$ref’ .

paths:
  /articles/{articleId}:
    get:
      summary: Returns an article by ID.
      parameters:
        - in: path
          name: articleId
          required: true
          type: integer
          minimum: 1
          description: The ID of the article to return.
      responses:
        200:
          description: An Article object.
          schema:
            type: object
            properties:
              id:
                type: integer
                example: 101
              name:
                type: string
                example: Introduction-to-Programming
        400:
          description: The specified article ID is invalid
        404:
          description: An article with the specified ID was not found.
        default:
          description: Unexpected error

Input and Output Models

The global definitions section lets you define common data structures used in your API. They can be referenced via $ref whenever a schema is required – both for the request body and response body. For example, this JSON object:

{
  "id": 101,
  "name": "Introduction-to-Programming"
}

The above can be represented as:

definitions:
  Article:
    properties:
      id:
        type: integer
      name:
        type: string
    #Both properties required
    required:  
      - id
      - name

Then, it can be referenced in the request body schema and response body schema as follows:

paths:
  /articles/{articleId}:
    get:
      summary: Returns an article by ID.
      parameters:
        - in: path
          name: articleId
          required: true
          type: integer
      responses:
        200:
          description: OK
          schema:
            $ref: '#/definitions/Article'
  /articles:
    post:
      summary: Creates a new article.
      parameters:
        - in: body
          name: article
          schema:
            $ref: '#/definitions/Article'
      responses:
        200:
          description: OK
 

Authentication

The 'securityDefinitions' and ‘security’ keywords are used to describe the authentication methods referred to in the API.

Various authentication methods that are supported are mentioned below:

  • API Key
  • Basic Authentication
  • OAuth 2 common flows (implicit, password, application, and access code)

Swagger Tools

Let us now discuss the Swagger tools. With open source and professional toolset, Swagger tools help simplify development for individual users and teams. Let's take a look at them:

SwaggerHub

This platform is useful to teams and individuals to design and documentation of API with openAPI specification

Swagger Editor

It’s an API editor for the users to design APIs with openAPI specification.

Swagger UI

Swagger UI helps users to visualize the open API specification in an interactive UI

Swagger Codegen

It generates server stubs and client software development kits from the OpenAPI Specification.

How to Import RESTful Requests from Swagger 2.0 in Katalon

Let us now move on to address the primary concern of this blog - How to import RESTful Requests from a Swagger 2.0.

Let's have a look at the following few steps!

  1. Open an existing API/Web Service Project in Katalon studio. If you don't have an existing project, you can create a new one and then proceed by opening it in the Katalon Studio.

 

Image depicting how to open a project

 

Image depicting how to open a project

 

2. After opening the project, We need to import the RESTful request from OpenAPI 2.0 (Swagger). For that, click on Action > API/Web Service > OpenAPI >  Import OpenAPI 2 (Swagger)

 

Image Depicting how to import Restful Request

Alternatively, We can also right-click on the ‘Object Repository’ and then Import > from Open API 2 (Swagger)

Image Depicting how to import Restful Request

 

3. After selecting the option, the following dialog box will pop up. We have two methods for importing RESTful requests; we can import either a Swagger local File or a Swagger (Open API) URL. For importing a local file, we can click on the browse option and choose any local file on the PC. Let us test with the URL:

 

Image Depicting how to import Restful Request using Swagger 2.0

NOTE: The URL used for testing: https://petstore.swagger.io/v2/swagger.json

4. As soon as we press ‘Ok,’ Katalon Studio loads the file or URL in ‘Object Repository,’ and RESTful APIs are generated accordingly.

Image depicting the loaded files

Frequently Asked Questions

What are RESTful Requests?

REST stands for Representational State Transfer. The client makes requests to the server (REST requests) and the server returns the response in a format easily understandable by the user.

What are the four Swagger Tools?

The four Swagger tools are as follows:

1. SwaggerHub

2. Swagger Editor

3. Swagger UI

4. Swagger Codegen

All of them have specific purposes

Write some of the Swagger Data types?

Swagger Data types are mentioned below:

1. Boolean

2. Number

3. Array

4. Integer

5. Object

Conclusion

We hope this blog helped you enhance your Knowledge about Swagger and RESTful APIs. The blog introduced you to the concept of Swagger (OpenAPI 2.0), marking the Swagger Tools and the basic structure of a Swagger file, and finally concluded with explaining how to import RESTful API from Swagger in Katalon.

If you found this blog interesting and insightful, you can check out some similar blogs:

https://www.codingninjas.com/studio/library/rest-api-749

https://www.codingninjas.com/studio/library/introduction-to-swagger-documentation-formatt

Refer to the Basics of C++ with Data StructureDBMSOperating System by Coding Ninjas, and keep practicing on our platform Coding Ninjas Studio. You can check out the mock test series on code studio.

You can also refer to our Guided Path on Coding Ninjas Studio to upskill yourself in domains like Data Structures and AlgorithmsCompetitive ProgrammingAptitude, and many more! Refer to the interview bundle if you want to prepare for placement interviews. Check out interview experiences to get an idea about various companies' interview questions.

Give your career an edge over others by considering our premium courses!

Happy Learning!

 

Thankyou Image
Live masterclass