How to generate Swagger client code when using multiple Veracode APIs together

Problem

Veracode’s REST API documentation website supports generating client side code to access the APIs. The generated code generally works if you use one API at a time, but you may run into issues when trying to combine generated code from multiple APIs in the same client. To address this, you must generate the code into separate packages. This article describes how to use an open source tool to do this successfully.

Solution

Swagger, aka the Open API Specification, supports a wide variety of capabilities including generating client side code. There are several tools you can use to do this, including the Veracode REST API documentation website. However, for some applications, a tool that provides additional capabilities is required.

For this article, we recommend using the open source Swagger code generation tool swagger-codegen. The source of this tool is available on Github and includes instructions for installing the tool from Maven or Homebrew.

The swagger-codegen tool supports passing additional options via a configuration file using the -c or --config option. Among the options that can be set are package names, which allow you to define the code for each API in its own package. We recommend taking this approach to isolate each set of generated code in its own package.

Example

To generate API code for the Applications and Identity APIs into two different packages, using a locally installed copy of swagger-codegen, do the following.

  • Download the Swagger definition files from the Veracode API documentation portal:

    • Application API

    • Identity API

  • Create a configuration file for the Application API called config-app.json with the following contents:

    { 
      "modelPackage": "com.veracode.applications.model", 
      "apiPackage": "com.veracode.applications.api" 
    }
    
  • Create a configuration file for the Identity API called config-identity.json with the following contents:

{
   "modelPackage": "com.veracode.identity.model", 
   "apiPackage": "com.veracode.identity.api" 
}
  • Generate each package on the command line as follows. (Note: if you installed swagger-codegen via Homebrew, the command will simply start with swagger-codegen rather than java -jar swagger-codegen-cli.jar.)
java -jar swagger-codegen-cli.jar generate -i ~/Documents/swagger-codegen/dynamicanalysis_v1_configservice.yaml -l java -c config-dynamic.json 

java -jar swagger-codegen-cli.jar generate -i ~/Documents/swagger-codegen/app.yaml -l java -c config-app.json
  • The results will look like this:

    % ls ./src/main/java/com/veracode/*/api
     ./src/main/java/com/veracode/applications/api: 
    ListsApplications_Api.java 
    
    ./src/main/java/com/veracode/identity/api: 
    ApiCredentialsApi.java BusinessUnitApi.java TeamApi.java UserApi.java
    

You can repeat this process with any of the other Veracode APIs. Just make sure to specify a unique path for the model and APIs in the configuration file for each API.

 

Topics (1)

Related Topics

    Ask the Community

    Get answers, share a use case, discuss your favorite features, or get input from the Community.