Swagger-Codegen custom settings

Serj Sagan picture Serj Sagan · May 12, 2016 · Viewed 10k times · Source

I am using swagger-codegen to generate my client-side C# classes. It does the job, but there are a few things I'd like to customize:

1) Most importantly, how I tell it which namespace, or perhaps base namespace, to use? Right now all of the genned classes have IO.Swagger.Model namespace, how do I change that?

2) All of my generated models start with I... as in ICustomer... is there any way to autostrip that first I if the class name starts with 2 caps, and the first letter is an I?

3) If 2 can be done, is there a way to have it append a : IWhatever? So if it's trying to gen ICustomer, I'd like to see:

public class Customer : ICustomer

Can this be done?

4) For my last question, I am using this command-line:

java -jar swagger-codegen-cli.jar generate 
     -i http://localhost:57772/MyService/swagger/v1/swagger.json 
     -l csharp -o C:/Code/AutoGenned/MyService

The problem is that it adds this to the path: src\main\csharp\IO\Swagger

Is there a way for me to not have this added to the path?

5) Finally, and least importantly, it gens 4 folders: Api, Client, Model, and Properties. Is there a way to only gen the Client and the Model folders?

Answer

Serj Sagan picture Serj Sagan · May 12, 2016

Based on getting a response from swagger-codegen team and trying stuff out on my own:

1) To fix the namespace, I had to create a config.json file that has basically this:

{
  "packageName": "My.Namespace.Whatever"
}

now my run command is

    java -jar swagger-codegen-cli.jar generate 
     -i http://localhost:57772/MyService/swagger/v1/swagger.json 
     -l csharp -o C:/Code/AutoGenned/MyService 
     -c config.json

2) Can't replace, but I can prepend... so instead of having a class named ICustomer I decided that ApiICustomer made more sense... so to the above command, I added

--model-name-prefix Api

3) Still not sure if/how this is possible.

4) Looks like a new change is coming down the pipe that will change how this works. https://github.com/swagger-api/swagger-codegen/issues/2837

5) Based on the info here: https://github.com/swagger-api/swagger-codegen#selective-generation You can do it by adding java system properties, like this java -Dwhateverproperty. So the end result, not including any fix for #3 is:

java -Dmodels -DsupportingFiles -jar swagger-codegen-cli.jar generate 
     -i http://localhost:57772/MyService/swagger/v1/swagger.json 
     -l csharp -o C:/Code/AutoGenned/MyService 
     -c C:/Code/SwaggerCodeGen/config.json 
     --model-name-prefix Api