Skip to content
wing328 edited this page Apr 20, 2016 · 38 revisions

Table of contents

GENERAL

How to debug Swagger Codegen?

You can leverage the following debug flags when generating the libraries:

  • [debugSwagger] prints the swagger specification as interpreted by the codegen
  • [debugModels] prints models passed to the template engine
  • [debugOperations] prints operations passed to the template engine
  • [debugSupportingFiles] prints additional data passed to the template engine

Here is an example using debugModels:

mvn clean package
java -DdebugModels=true -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
  -i http://petstore.swagger.io/v2/swagger.json \
  -l ruby -o /var/tmp/ruby/

How do "tags" affect the generated code?

tags basically groups endpoints into the same API class file. For example, an endpoint with the "store" tags will be generated in the StoreApi class file.

Ref: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#tagObject

How to import Java objects that are not defined as model in the OpenAPI/Swagger spec?

Please refer to https://github.com/swagger-api/swagger-codegen#bringing-your-own-models

Generator Service

https://generator.swagger.io/

The API is split in to two sections: client and server. The endpoints under client are meant for generating code to consume an API. The endpoints under server are meant for generating code to run the API itself (server stubs etc).

Each section has 4 endpoints

  • Get a list of languages/frameworks that you can generate code for
  • Get options schema for a language/framework (where do these options go? options in GeneratorInput?)
  • Post GeneratorInput to generate code to a zip file and get back link to file
  • Get zip file (download)

Example node script to generate typescript angular client from swagger.yaml. Note that we use http. Request cannot verify the first certificate if using https (at the time of writing this)

var fs = require('fs');
var path = require('path');
var jsYaml = require('js-yaml');
var request = require('request');
var unzip = require('unzip2');

var codeGenEndpoint = 'http://generator.swagger.io/api/gen/clients';
var language = 'typescript-angular';

fs.readFile(path.resolve('swagger.yaml'), 'utf8', function (error, yaml) {
    if (error) {
        throw error;
    }

    var swaggerObj = jsYaml.load(yaml);

    var postBody = {
        spec: swaggerObj,
        options: {
            modelPropertyNaming: 'camelCase',
            apiPackage: 'api.clients.settings',
            modelPackage: 'api.clients.settings'
        }
    };

    request.post({
        url: codeGenEndpoint + '/' + language,
        body: JSON.stringify(postBody),
        headers: {
            'Content-Type': 'application/json'
        }
    }, function(error, response, body){
        if (error) {
            throw error;
        }

        if (response.statusCode !== 200) {
            throw new Error('Response code was not 200. ' + body)
        }

        var responseObj = JSON.parse(body);

        request({
            url: responseObj.link,
            encoding: null
        }).pipe(unzip.Extract({ path: 'src/client/js/codegen/settingsApi'}));
    });
});

Java

Using Java API client to make request results in SSL errors due to invalid certificate. Is there a way to bypass that?

Yes, please refer to http://stackoverflow.com/a/6055903/677735

Android

How can I generate an Android SDK?

The Java SDK is also compatible with Android.

[RECOMMENDED] To generate the Java SDK with okhttp and gson libraries, run the following:

mvn clean package
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
  -i http://petstore.swagger.io/v2/swagger.json \
  -l java --library=okhttp-gson \
  -o /var/tmp/java/okhttp-gson/ 

You can also generate the Java SDK with other HTTP libraries by replacing okhttp-gson with retrofit for example. For a list of support libraries, please run

java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar config-help -l java

To generate the Android SDK with volley, please run

mvn clean package
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
  -i http://petstore.swagger.io/v2/swagger.json \
  -l android --library=volley \
  -o /var/tmp/android/volley/ 

We do not recommend using the default HTTP library (Apache HttpClient) with android as it's not actively maintained.

For discussion related to method count, please refer to https://github.com/swagger-api/swagger-codegen/issues/687