swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.01k stars 6.03k forks source link

[Java] Codegen-Maven-Plugin breaks the build if custom templates are used #11295

Open jriegraf opened 2 years ago

jriegraf commented 2 years ago

Description

The swagger-codegen-maven-plugin throws a runtime exception when using custom templates. This issue only occurs on Windows!

[ERROR]
java.lang.RuntimeException: Could not generate supporting file 'SupportingFile:
        templateFile: ApiClient.mustache
        folder: src\main\java\io\swagger\client
        destinationFilename: ApiClient.java
'
    at io.swagger.codegen.v3.DefaultGenerator.generateSupportingFiles (DefaultGenerator.java:663)
    at io.swagger.codegen.v3.DefaultGenerator.generate (DefaultGenerator.java:786)
    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:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    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:957)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:566)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
Caused by: java.io.FileNotFoundException: /handlebars/Java/C:/dev/fun/swagger-codegen-template-test/src/main/resources/templateDirectory/ApiClient.mustache
    at com.github.jknack.handlebars.io.URLTemplateLoader.sourceAt (URLTemplateLoader.java:70)
    at com.github.jknack.handlebars.Handlebars.compile (Handlebars.java:438)
    at com.github.jknack.handlebars.Handlebars.compile (Handlebars.java:419)
    at io.swagger.codegen.v3.templates.HandlebarTemplateEngine.getHandlebars (HandlebarTemplateEngine.java:44)
    at io.swagger.codegen.v3.templates.HandlebarTemplateEngine.getRendered (HandlebarTemplateEngine.java:24)
    at io.swagger.codegen.v3.DefaultGenerator.generateSupportingFiles (DefaultGenerator.java:634)
    at io.swagger.codegen.v3.DefaultGenerator.generate (DefaultGenerator.java:786)
    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:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    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:957)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:566)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

see full buil log: https://github.com/jriegraf/swagger-codegen-template-test/runs/4486888138?check_suite_focus=true#step:4:839

Swagger-codegen version

<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.30</version>

Swagger declaration file content or url

Petstore Swagger 2.0 Spec (json) from https://petstore.swagger.io/

The templates I used: pojo.mustache, ApiClient.mustache Both copied from https://github.com/swagger-api/swagger-codegen/tree/master/modules/swagger-codegen/src/main/resources/Java

Command line used for generation

            <plugin>
                <groupId>io.swagger.codegen.v3</groupId>
                <artifactId>swagger-codegen-maven-plugin</artifactId>
                <version>3.0.30</version>
                <executions>
                    <execution>
                        <id>generate-petstore</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/swagger.json</inputSpec>
                            <language>java</language>
                            <output>${project.build.directory}/generated-sources/petstore</output>

                            <generateApiTests>false</generateApiTests>
                            <generateModelTests>false</generateModelTests>

                            <templateDirectory>${project.basedir}/src/main/resources/templateDirectory
                            </templateDirectory>

                            <configOptions>
                                <dateLibrary>java8</dateLibrary>
                                <java8>true</java8>
                            </configOptions>

                            <library>jersey2</library>

                        </configuration>
                    </execution>
                </executions>
            </plugin>

Steps to reproduce

  1. Start your windows machine
  2. Clone sample project from https://github.com/jriegraf/swagger-codegen-template-test
  3. run mvn clean package
  4. See exception in build log

Additional: remove the templateDirectory tag from the plugin definition in pom.xml -> build will succeed

skarthiksrinivas commented 2 years ago

I am also facing the same issue. Looks like there is an issue in dealing with paths on windows environment here - https://github.com/swagger-api/swagger-codegen/blob/v3.0.29/modules/swagger-codegen/src/main/java/io/swagger/codegen/v3/templates/CodegenTemplateLoader.java#L32

    private String resolveTemplateFile(String templateDir, String templateFile) {
        if (templateFile.startsWith(templateDir)) {
            templateFile = StringUtils.replaceOnce(templateFile, templateDir, StringUtils.EMPTY);
        }
        return templateFile;
    }

For the example provided by @jriegraf , the arguments should be this: templateDir = "/handlebars/Java" and templateFile = "/handlebars/Java/C:/dev/fun/swagger-codegen-template-test/src/main/resources/templateDirectory/ApiClient.mustache"

After String replacement, templateFile = "/C:/dev/fun/swagger-codegen-template-test/src/main/resources/templateDirectory/ApiClient.mustache" which leaves it with a leading / eventually causing file path invalid. cc: @HugoMario

wrightsonm commented 2 years ago

this also occurs when trying to use the language csharp.