Generate a client side java SDK for multiple APIs

Overview

  • If you have read my last blog post then you already know what I’m going to tell you now. So if you haven’t read those yet please refer it before starting this tutorial.

https://randikan.wordpress.com/2016/01/12/generate-client-side-code-using-swagger-codegen/

Prerequisites

  • You need to have a clear idea about swagger codegen. If you need a single jar file as the output you can use this customized swagger codegen instead of the one swagger provided.

How are we doing this?

  • Swagger is used to represent APIs and swagger codegen is used to generate client side sdks for those APIs. But swagger codegen cannot be used to generate SDKs for multiple apis, because there will be so many conflicts. For an example there may be conflicts in end point urls.
  • Main approach that we are going to follow is,
    • We can get swagger definitions for each API.
    • After that we have to merge those swagger files into a single file.
    • Then we’ll use that final swagger file as the input for swagger codegen.

Merging swagger files.

  • Before start merging we have to consider following rules.
    • Only two things can be changed in each swagger file. Other fields must be same.
      • Paths and the Definitions are those two things.
    • Swagger codegen uses url patterns in the API as the model when creating classes. So two APIs cannot have the same parameter.
      • For example if API A has /pet/updatePet as the pattern then API B cannot have /pet/UpdateName as the pattern.
  • Now to the merging part.
    • This can be used to merge swagger files.
    • What you have to do is download the project and build it using following command.
                       mvn clean install
      
    • If your system doesn’t have maven installed , use this command to install maven in your linux system.
                       $ sudo apt-get install maven
    • Place all swagger files in /SwaggerJsonMerger/modules/wso2jsonmerger/inputSwagger folder. Swagger files should be named as 0.josn,1.json and 2.json likewise
    • Execute merger.sh file.
    • You can find the output swagger file in SwaggerJsonMerger/distribution/swagger.json

Now all you have to do is use the merged swagger.json file as the input for swagger codegen. Simple as that. 🙂

 

Leave a Reply