swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

maven-codegen-plugin Access custom templates from jar

Open tibors13 opened this issue 5 years ago • 1 comments

Description

I have following situation

  • customCodegen as jar artifact
  • customized templates as part of the same jar (similar packaging like with (e.g. adaptedTemplate)
  • used customCodegen from swagger-codegen-maven-plugin
  • using copy depen -to extract templates to working directory

If I extract and copy adaptedTemplate directory to build path -> it works OK

Swagger-codegen version

3.0.21

Swagger declaration file content or url
Command line used for generation

swagger-codegen-maven-plugin

Steps to reproduce
Related issues/PRs
Suggest a fix/enhancement

Question: is it possible to load modified templates directly from jar file, similar to original swagger-generators ? Customized codegen code works fine, just to unpack (in many poms is quite tedious :( )

I see problem in https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/java/io/swagger/codegen/config/CodegenConfigurator.java at setTemplate expects local directory, or is there any workaround? Would it be possible to specify jar file access to load modified templates? ( swagger.yml from jar works nice just as dependency in plugin, similar to custom codegen in customgenerator

  1. option would be to check local directory existence -> if not expect jar access
  2. other - define it alternate ways

(other problems would be to access them in Handlebar, but this I have probably solved, as I had modified HandlebarTemplateEngine search algorithm to access all 4 places, as it was in codegen v2.x )

Overwrite this configurator is neither not so easy, as created in swagger-coden-maven-plugin directly. Thanks for any hints

tibors13 avatar Oct 15 '20 12:10 tibors13

In case anyone else is still facing this issue, it seems there is a way to use a .jar file that holds custom templates only, but there are a couple of things to do on the side. In my case I needed to generate static html from an OpenAPI spec v.3, & here's how I got it working:

  • artifact containing custom templates: swagger-codegen-plugin-extension;
  • custom templates placed under resources/handlebars/htmlDocs/libraries/myCustomLib;
  • extend StaticHtmlCodegen & add myCustomLib to the list of supportedLibraries:
public class StaticHtmlCodegenExtension extends StaticHtmlCodegen {

    public StaticHtmlCodegenExtension() {
        super();
        // maps to template files from the handlebars/htmlDocs/libraries/ resource folder
        supportedLibraries.put("myCustomLib", "My fancy static HTML templates");
    }
}
  • plugin usage:
      <plugin>
          <groupId>io.swagger.codegen.v3</groupId>
          <artifactId>swagger-codegen-maven-plugin</artifactId>
          <version>3.0.56</version>
          <configuration>
              <language>my.org.StaticHtmlCodegenExtension</language>
              <library>myCustomLib</library>
              <!-- other configs... -->
          </configuration>
          <dependencies>
              <dependency>
                  <groupId>my.org</groupId>
                  <artifactId>swagger-codegen-plugin-extension</artifactId>
                  <version>1.0.0</version>
              </dependency>
          </dependencies>
      </plugin>

One caveat with this is that for mustache partials to work, they need to reference the library path from the jar resource, e.g.

{{> libraries/myCustomLib/partialFileNameHere}}

ionel-sirbu-crunch avatar Jun 05 '24 16:06 ionel-sirbu-crunch