Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the API name in the class generated by swagger-codegen

Tags:

swagger

I am using swagger-springmvc and swagger-codegen to generate a Java client library for a RESTful webservice. I have written my own extension of the BasicJavaGenerator (see below) to override the package names, and can successfully generate the client library files. The "main" files generated are:

swagger-codegen/generated-code/java/pom.xml
swagger-codegen/generated-code/java/src/main/java/com/example/ApiApi.java
swagger-codegen/generated-code/java/src/main/java/com/example/model/*.java

What I can't find is how to set the name of my API to get the code generator to rename ApiApi.java to MyProjectApi.java (for example) as seems to be done in the samples included in with swagger-codegen. I have tried looking at the code generator code to override the api name, and have also tried looking both at the swagger json spec and swagger springmvc functionality to an option to set the name.

The code generator:

package com.wordnik.swagger.codegen

import com.wordnik.swagger.codegen.BasicJavaGenerator

object MyJavaGenerator extends BasicJavaGenerator {
  def main(args: Array[String]) = generateClient(args)

  // api invoker package
  override def invokerPackage = Some("com.example.api")

  // package for models
  override def modelPackage = Some("com.example.api.model")

  // package for api classes
  override def apiPackage = Some("com.example.api")
}
like image 968
trf Avatar asked Feb 09 '15 16:02

trf


People also ask

What is codegen in swagger?

The Swagger Codegen is an open source code-generator to build server stubs and client SDKs directly from a Swagger defined RESTful API. The source code for the Swagger Codegen can be found in GitHub.

What is swagger codegen ignore?

# Swagger Codegen Ignore. # Generated by swagger-codegen https://github.com/swagger-api/swagger-codegen. # Use this file to prevent files from being overwritten by the generator.

What is generate server in swagger?

Swagger has a tool called the swagger-code-generator which allows you to generate Server or Client code for the API specification that you defined. By using this tool developers can save time by not having to manually set up the structure of the API involving the different web frameworks.


1 Answers

you can override this behavior as such:

  override def toApiName(name: String) = "MyProject" + name

as you see fit. Please note that you should consider upgrading to 2.1.0-SNAPSHOT, which lives in https://github.com/swagger-api/swagger-codegen/tree/develop_2.0

like image 85
fehguy Avatar answered Oct 05 '22 04:10

fehguy