3

I am using swagger-codegen to generate a SDK based on my Web API, and the generated source files contains the following header:

/* 
 * [MY PROJECT NAME]
 *
 * [MY COPYRIGHT]
 *
 * OpenAPI spec version: 1.0.0 - Beta
 * Contact: [MY EMAIL]
 * Generated by: https://github.com/swagger-api/swagger-codegen.git
 */

Would it be possible to configure swagger-codegen to not generate the last 3 lines of the header?

Helen
  • 87,344
  • 17
  • 243
  • 314
Toto
  • 736
  • 9
  • 33

1 Answers1

4

Yes it's possible. The output format is defined using Mustache templates.

Find the templates for your language here:

Download the templates you want to change and modify them as required. Then run the generator using the -t argument to specify the path to your custom templates:

java -jar swagger-codegen-cli-2-4-18.jar generate
  -i http://petstore.swagger.io/v2/swagger.json
  -l csharp
  -o PetstoreCSharpClient
  -t path/to/MyTemplates    <------

Any custom templates found in the -t folder will be used instead of the corresponding standard templates. Templates not found in the -t folder will default to the standard templates.

Example

In case of the csharp generator in Codegen 2.x (OAS2), the header in question comes from:

modules/swagger-codegen/src/main/resources/csharp/partial_header.mustache

and there are similar headers in:

modules/swagger-codegen/src/main/resources/csharp/Project.mustache
modules/swagger-codegen/src/main/resources/csharp/TestProject.mustache

Download these 3 files to, say, C:\MyTemplates, and remove unwanted lines from them. Then run the generator with -t C:\MyTemplates to use your custom templates without that header.

Helen
  • 87,344
  • 17
  • 243
  • 314
  • Thank you for the insight, I have taken a look at the different templates and tried to understand the logic by using them with swagger-codegen but I was not able to modify the generated header, do you have further information about this? – Toto May 04 '18 at 19:13
  • @Toto Which generator (`-l` argument) are you using, which templates did you modify and how? – Helen May 04 '18 at 19:17
  • I am using the csharp generator, I have tried to manually create a template where I hardcoded a header "// test" like so to see if it had an effect on the generated SDK, which it didn't. I then downloaded all the templates and put them in my template folder in order to see what impact they had on the generated source code but they did not bring much modifications (some methods were generated as virtual, and some properties name were changed from the documentation and that's about it). – Toto May 04 '18 at 20:20
  • @Toto Updated the answer. I've confirmed that this approach works using Codegen 2.3.1. – Helen May 04 '18 at 21:14