Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to configure the source code file header generated by swagger-codegen?

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?

like image 658
Toto Avatar asked May 26 '26 00:05

Toto


1 Answers

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

Find the templates for your language here:

  • Codegen 2.x (OAS2): https://github.com/swagger-api/swagger-codegen/tree/master/modules/swagger-codegen/src/main/resources
  • Codegen 3.x (OAS3): https://github.com/swagger-api/swagger-codegen-generators/tree/master/src/main/resources

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.

like image 89
Helen Avatar answered Jun 02 '26 08:06

Helen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!