OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.68k stars 6.54k forks source link

[BUG] [MVN Plugin] - <templateDirectory> is ignored #4208

Closed yuvalishay closed 4 years ago

yuvalishay commented 5 years ago

I'm running mvn install with the following configuration - but the looks like the is ignored and Template directory always defaults to embedded dir.

Here is the maven plugin section in which the templateDirectory is ignored:

  <build>
        <plugins>
            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>4.1.3</version>
                <executions>
                    <execution>
                        <id>swagger-generate-server-code</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.parent.parent.basedir}/src/main/resources/swagger-spec-pet-store.json</inputSpec>
                            <templateDirectory>${project.basedir}/src/main/resources</templateDirectory>
                            <modelPackage>com.computrade.restful.services.model</modelPackage>
                            <apiPackage>com.computrade.restful.services.server.api</apiPackage>
                            <invokerPackage>com.computrade.restful.services.server.invoker</invokerPackage>
                            <configOptions>
                                <interfaceOnly>true</interfaceOnly>
                                <java8>true</java8>
                                <dateLibrary>java8</dateLibrary>
                                <skipDefaultInterface>true</skipDefaultInterface>
                            </configOptions>
                            <configHelp>false</configHelp>
                            <generatorName>spring</generatorName>
                            <library>spring-boot</library>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
openapi-generator version
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <version>4.1.3</version>

I tried also earlier from 4.0.0 and above - but still the same.

Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/3364 I see this issue was closed. ( but it wasn't open on the Maven plugin from the beginning )

Thanks, Yuval Ishay

andrzejwp commented 4 years ago

+1 looks like it's still present in 4.2.2 as well

davidjayb commented 4 years ago

This is still an issue. Adding the --debug option to Maven seems to pick up the template directory

yuvalishay commented 4 years ago

Any news?

adin234 commented 4 years ago

same thing with Homebrew and npm installations

rj93 commented 4 years ago

Still present in 4.3.0

alexibz commented 4 years ago

Also have this issue, but from the cli side. Using 4.3.0 and trying to generate java. Tried using the -t and --template options.

adin234 commented 4 years ago

i'm using 4.0.1 and this works for me

generator.sh

openapi-generator generate \
  -i <input file> \
  -g csharp-netcore \
  -t <template directory>/csharp-netcore \
  -o <output directory> \
  -c <config file>
wing328 commented 4 years ago

@adin234 please try the latest version (4.3.0) to see if it works for you.

I did some tests with the latest version of the CLI and the -t option works fine for me

wing328 commented 4 years ago
                        <templateDirectory>${project.basedir}/src/main/resources</templateDirectory>

You need to provide the folder with the customized templates instead. e.g.

<templateDirectory>${project.basedir}/src/main/resources/JavaSpring</templateDirectory>
yuvalishay commented 4 years ago

The issue is not with the CLI. This issue is with the MAVEN plugin.

I did provide also the specific folder but it still doesn't work.

MderM commented 4 years ago

Same here, but with templateResourcePath. The config parameter is picked up by the plugin and printed to the console, but generator seems to ignore it.

CLI works fine.

DenisKnoepfle commented 4 years ago

There is a bug under windows concerning the '/' and '\' in JAR resource paths since a classpath resource path with '\' does not work. It happened to me the same using the maven plugin. I configured a dependency to my own project containing my mustache templates (a simple maven project with the files inside src/main/resources/templates/JavaSpring) and wanted to reference those.

<plugin>
  <groupId>org.openapitools</groupId>
  <artifactId>openapi-generator-maven-plugin</artifactId>
  <version>4.3.1</version>
  <dependencies>
    <dependency>
      <groupId>my.company</groupId>
      <artifactId>my-generator-templates</artifactId>
      <version>1.0</version>
    </dependency>
  </dependencies>
  ...
  <configuration>
  ...
    <templateResourcePath>templates/JavaSpring</templateResourcePath>
  ...
  </configuration>
  ...
</plugin>

When I configured templateResourcePath as "JavaSpring", the plugin would not complain because coincidentally the embedded mustache template folder is also named "JavaSpring", thus during the check it finds that one. Then I changed the path to be sure it's different, and again no complains as long as it's a single-part path, e.g. "foobar", and not "foo\bar".

These classpath URLs have to be compiled always with the slash ('/') instead of File.separator which bugs out under windows, e.g. tries to find "templates\JavaSpring\pojo.mustache" which is not a valid resource path, but is found using '/', e.g. "templates/JavaSpring/pojo.mustache". I tried it out locally and it works when using '/' consistently regardless of OS.

Concerned methods: openapi-generator/org.openapitools.codegen.AbstractGenerator.getFullTemplateFile() and openapi-generator-core/org.openapitools.codegen.config.WorkflowSettings.withTemplateDir()

DenisKnoepfle commented 4 years ago

It seems in the original fix it worked because there was a replacement of the file-separator in case it was NOT '/' using the method getCPResourcePath(). After a refactoring in this class AbstractGenerator it broke.

ogerardin commented 4 years ago

What's the status on this ? It seems I have the same issue (Maven plugin, v4.3.0, using templateDirectory)

Edit: turns out I didn't, I just had an incorrect configuration. This part of the doc is important and was relevant to my problem:

OpenAPI Generator will lookup templates in this order:

  • User customized library path (e.g. custom_template/libraries/feign/model.mustache)
  • User customized generator top-level path (e.g. custom_template/model.mustache)
  • Embedded library path (e.g. resources/Java/libraries/feign/model.mustache)
  • Embedded top-level path (e.g. resources/Java/model.mustache)
  • Common embedded path (e.g. resources/_common/model.mustache)
DenisKnoepfle commented 4 years ago

@jimschubert this bug is still open and directly corresponds to our conversation here: https://github.com/OpenAPITools/openapi-generator/pull/6357#discussion_r449851055

Have you had another look into it?

jimschubert commented 4 years ago

@DenisKnoepfle thanks for bumping this. I never got your comment last month from the linked issue for some reason.

I was having trouble reproducing the issue you'd described. I'd found it was because I was testing using git bash in Windows so I guess it uses linux file separators and was never hitting the file separator issue you'd described. When I had tested the maven plugin, I was testing templateDirectory rather than templateResourcePath as is discussed in this issue.

I should have a fix shortly. I just need to get my Windows 10 VM to update successfully so I can test this in Windows Terminal, since I assume that's what most Windows developers would be using. I also want to be sure to test both templateDirectory and templateResourcePath options a few times in private CI and local builds to be sure the issue is resolved.

A workaround for anyone affected may be to switch to templateDirectory like:

<templateDirectory>${project.basedir}/templates</templateDirectory>

However, since this bug was reported 7 months before my refactor, I can't say for sure why the original implementation was failing.