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

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

Closed
yuvalishay opened this issue Oct 21, 2019 · 16 comments · Fixed by #7587
Closed

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

yuvalishay opened this issue Oct 21, 2019 · 16 comments · Fixed by #7587

Comments

@yuvalishay
Copy link

yuvalishay commented Oct 21, 2019

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

#3364
I see this issue was closed.
( but it wasn't open on the Maven plugin from the beginning )

Thanks,
Yuval Ishay

@andrzejwp
Copy link

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

@davidjayb
Copy link

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

@yuvalishay
Copy link
Author

Any news?

@adin234
Copy link

adin234 commented Mar 29, 2020

same thing with Homebrew and npm installations

@rj93
Copy link

rj93 commented Apr 16, 2020

Still present in 4.3.0

@alexibz
Copy link

alexibz commented May 1, 2020

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
Copy link

adin234 commented May 2, 2020

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
Copy link
Member

wing328 commented May 2, 2020

@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
Copy link
Member

wing328 commented May 2, 2020

                        <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
Copy link
Author

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
Copy link

MderM commented Jun 4, 2020

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
Copy link

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
Copy link

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
Copy link

ogerardin commented Jul 24, 2020

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
Copy link

@jimschubert this bug is still open and directly corresponds to our conversation here: #6357 (comment)

Have you had another look into it?

@jimschubert
Copy link
Member

@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.

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

Successfully merging a pull request may close this issue.