Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specifying a TemplateDirectory requires all the templates to be present in the new TemplateDirectory #8904

Closed
osvacaneljr opened this issue Nov 10, 2018 · 6 comments
Assignees

Comments

@osvacaneljr
Copy link

Description

Need to customize the api.mustache template,

  1. Modified api.mustache file in the resource directory
  2. Specified the templateDirectory configuration option in the pom.xml
    <templateDirectory>${project.basedir}/src/main/resources</templateDirectory>

when the code generation is invoked, this is the error.

Caused by: java.lang.RuntimeException: can't load template /abc/src/main/resources/model.mustache
        at io.swagger.codegen.v3.templates.MustacheTemplateEngine.getTemplateReader(MustacheTemplateEngine.java:85)
        at io.swagger.codegen.v3.templates.MustacheTemplateEngine.readTemplate(MustacheTemplateEngine.java:54)
        at io.swagger.codegen.v3.templates.MustacheTemplateEngine.getRendered(MustacheTemplateEngine.java:33)
        at io.swagger.codegen.v3.DefaultGenerator.processTemplateToFile(DefaultGenerator.java:735)
        at io.swagger.codegen.v3.DefaultGenerator.generateModels(DefaultGenerator.java:392)
        ... 24 more

if I copy the model.mustache file to the resource directory, then the generation fails, due too another mustache file missing.

I found this old Issue,

https://github.com/swagger-api/swagger-codegen/issues/200

Specifying that just the template to be modified is required to exist in the templateDirectory and how the code generator will fallback to the embedded templates.

Swagger-codegen version

3.0.2

Swagger declaration file content or url
Command line used for generation
Steps to reproduce
Related issues/PRs
Suggest a fix/enhancement
@osvacaneljr osvacaneljr changed the title Specifying a TemplateDirectory requires all the templates to be copied Specifying a TemplateDirectory requires all the templates to be present in the new TemplateDirectory Nov 11, 2018
@osvacaneljr
Copy link
Author

The root cause of the issue (at least in the scope of spring) is in the SpringCodegen class. It does not initialize the embeddedTemplateDir variable if the templateDir was specified. Interesting enough the but is fixed in the master branch so the unit tests there do not fail but, the 3.0.0 branch does not include the version of SpringCodegen class in the master branch. It instead uses the version of the SpringCodegen class in the swagger-codegen-generators package v1.0.2 which has the issue.

So, I guess the question is, when the SpringCodegen class in the master branch is going to be released?

@HugoMario
Copy link
Contributor

this has been fixed by swagger-codegen-generators#710 for codegen v3

@vithu30
Copy link

vithu30 commented Nov 3, 2020

I'm still facing the same issue with codegen maven plugin - 3.0.23. It is required to copy all the relevant mustache files into the template directory. Getting the following exception when generating code for jaxrs-cxf-cdi:

[ERROR] 
java.lang.RuntimeException: Could not generate api file for 'xxxx'
    at io.swagger.codegen.v3.DefaultGenerator.generateApis (DefaultGenerator.java:579)
    at io.swagger.codegen.v3.DefaultGenerator.generate (DefaultGenerator.java:782)
    at io.swagger.codegen.v3.maven.plugin.CodeGenMojo.execute_ (CodeGenMojo.java:554)
    at io.swagger.codegen.v3.maven.plugin.CodeGenMojo.execute (CodeGenMojo.java:319)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:208)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:954)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
Caused by: com.github.jknack.handlebars.HandlebarsException: /~/pathParams.mustache:1:41: The partial '/~/beanValidationPathParams.mustache' at /~/beanValidationPathParams.mustache' could not be found
    at com.github.jknack.handlebars.internal.Partial.merge (Partial.java:236)
    at com.github.jknack.handlebars.internal.BaseTemplate.apply (BaseTemplate.java:130)
    at com.github.jknack.handlebars.internal.TemplateList.merge (TemplateList.java:95)
    at com.github.jknack.handlebars.internal.BaseTemplate.apply (BaseTemplate.java:130)
    at com.github.jknack.handlebars.internal.BaseTemplate.apply (BaseTemplate.java:118)
    at com.github.jknack.handlebars.Options.apply (Options.java:538)
    at com.github.jknack.handlebars.Options.fn (Options.java:433)

@HugoMario, appreciate any help on this

@ashishbharg
Copy link

ashishbharg commented Dec 23, 2020

I am also facing this issue with codegen plugin, version 3.0.23. I am getting the following exception when using io.swagger.codegen.v3.generators.java.JavaJerseyServerCodegen as language in my gradle file.

Exception in thread "Thread-1" java.lang.RuntimeException: Could not generate model 'randomName'
        at io.swagger.codegen.v3.DefaultGenerator.generateModels(DefaultGenerator.java:451)
        at io.swagger.codegen.v3.DefaultGenerator.generate(DefaultGenerator.java:779)
        at io.swagger.codegen.v3.cli.cmd.Generate.run(Generate.java:378)
        at java.lang.Thread.run(Thread.java:748)
Caused by: com.github.jknack.handlebars.HandlebarsException: ~/licenseInfo.mustache' could not be found
        at com.github.jknack.handlebars.internal.Partial.merge(Partial.java:236)
        at com.github.jknack.handlebars.internal.BaseTemplate.apply(BaseTemplate.java:130)
        at com.github.jknack.handlebars.internal.TemplateList.merge(TemplateList.java:95)
        at com.github.jknack.handlebars.internal.BaseTemplate.apply(BaseTemplate.java:130)
        at com.github.jknack.handlebars.internal.BaseTemplate.apply(BaseTemplate.java:118)
        at com.github.jknack.handlebars.internal.ForwardingTemplate.apply(ForwardingTemplate.java:72)
        at io.swagger.codegen.v3.templates.HandlebarTemplateEngine.getRendered(HandlebarTemplateEngine.java:26)
        at io.swagger.codegen.v3.DefaultGenerator.processTemplateToFile(DefaultGenerator.java:795)
        at io.swagger.codegen.v3.DefaultGenerator.generateModels(DefaultGenerator.java:438

@pgr0ss
Copy link

pgr0ss commented Apr 12, 2021

Any word on this one? I am also experiencing the same issue.

@radarsh
Copy link

radarsh commented Nov 11, 2021

I discovered that specifying the same name for the template dir helps. In my case I am coming up with a custom library for the JavaClientCodegen (similar to resttemplate). Say my library is called example. If I store the templates under src/main/resources/Java/example it works without using having to explicitly set the templateDir or embeddedTemplateDir.

I think somewhere in the codegen it uses Java/<library> to determine the location of templates.

For swagger codegen v3, this changes to handlebars/Java/<library>. So you will need to store your custom templates under src/main/resources/handlebars/Java/example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants