Swaggen factory (Swagger JSON to Kotlin data class and Retrofit interface)

Chetan Bhalerao
2 min readAug 16, 2020

We, android developers, are always excited to build something cool something really meaningful which will help the business grow and ultimately help ourselves to learn new technologies, to focus on the real work we always look for the automate the tasks which are essential and unavoidable and of course redundant.

Every android developer in their life spends some time in building data classes and retrofit interfaces from request and response of API calls, which backend team provide. Nowadays many backend teams from various companies maintain really good API documentation in Swagger, which uses OpenAPI specification. To take advantage of this and help the team I created 1 ruby script which provides parse data class and retrofit interfaces.

You can find this script here https://github.com/csbhalerao/swag_gen_factory

This script will take the Swagger openapi.json file and generate the Kotlin network API code.

This script will help in generating Kotlin code for network API calls like Data requests and responses, also this script will generate the Retrofit interface which includes abstract function with retrofit annotation.

Prerequisite

You need a ruby setup on your machine.

You can check inside a terminal emulator by typing:

ruby -v

This should give some basic version information about ruby if ruby is already installed.

If ruby is not installed you can refer here for installation process https://www.ruby-lang.org/en/documentation/installation/

How to use this script

  1. Download this script.
  2. Get the openapi.json from the swagger editor.
  3. Then just execute the following script.
ruby gen_network_code.rb openapi.json

4. After the successful execution of the script, you will get NetworkApi.kt with a request, response, and retrofit interface.

This script is created to help developers. Please provide me the feedback and let me know how I can improve the script or let me know what else we can automate. Thanks for reading this blog. Happy coding.

--

--

Chetan Bhalerao

Android developer, Backend developer. Interested in exploring new technologies. https://twitter.com/csbhalerao