swagger-api / swagger-api/swagger-codegen

Custom templates don't work on Windows

Open
#8,552 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Custom templates for supporting files don't get resolved under Windows. The reason is that DefaultGenerator.generateSupportingFiles (around line 581 in version 3.0.0-rc1) contains the following erroneous construct:

	if(ignoreProcessor.allowsFile(new File(outputFilename))) {
		if (templateFile.endsWith("mustache")) {
			String templateName = templateFile;
			final com.github.jknack.handlebars.Template hTemplate = getHandlebars(templateName.replace(config.templateDir(), StringUtils.EMPTY)); // 581: WRONG!!!

On Windows, config.templateDir() returns the canonical Windows path of the custom template directory (e.g. C:\source\svn\WBPM\cloud\obj-api\trunk\objectapi\templates\jaxrs-cxf) whereas templateName has already been converted (at around line 568, which changes \ to / in templateFile) to use Unix-style path separator and has a value like C:/source/svn/WBPM/cloud/obj-api/trunk/objectapi/templates/jaxrs-cxf/server/pom.mustache. Thus the call to getHandlebars() fails with the following exception:

java.io.FileNotFoundException: C:\source\svn\WBPM\cloud\obj-api\trunk\objectapi\templates\jaxrs-cxf/C:/source/svn/WBPM/cloud/obj-api/trunk/objectapi/templates/jaxrs-cxf/server/pom.mustache

I discovered this while trying to use swagger-codegen-maven-plugin to generate jaxrs-cxf code with custom templates (BTW the existing jaxrs-cxf templates do not yield an executable Spring application).

Isn't it rather a major bug that custom templates don't work on Windows?!

Swagger-codegen version

3.0.0-rc1

Swagger declaration file content or url

Sorry, I am not permitted to post the YAML in question, for reasons of commercial confidentiality. Pretty much any old YAML should suffice.

Command line used for generation

swagger-codegen-maven-plugin configuration:

	<configuration>
		<language>jaxrs-cxf</language>
		<templateDirectory>${project.basedir}/templates/jaxrs-cxf</templateDirectory>
		<inputSpec>${project.build.yaml.directory}/ae.yaml</inputSpec>
		<output>${project.build.output.directory}/jaxrs-cxf</output>
		<groupId>com.blah.bpm.clients</groupId>
		<artifactId>objectapi-test-server</artifactId>
		<artifactVersion>${objectapi.version}</artifactVersion>
		<apiPackage>com.blah.clients.ae.api</apiPackage>
		<modelPackage>com.blah.clients.ae.model</modelPackage>
		<invokerPackage>ae</invokerPackage>
		<configOptions>
			<sourceFolder>src/main/java</sourceFolder>
			<generateSpringApplication>true</generateSpringApplication>
			<generateSpringBootApplication>true</generateSpringBootApplication>
			<artifactName>Blah Microservices</artifactName>
		</configOptions>
	</configuration>
Steps to reproduce

Run mvn install on a project configured to invoke swagger-codegen-maven-plugin configured as above.

Related issues/PRs

I had to patch in a fix for the following bug to get this far:
https://github.com/swagger-api/swagger-codegen/issues/8291

Suggest a fix/enhancement

Please see the description. I don't have a suggested fix at this point.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in DefaultGenerator.generateSupportingFiles around lines 568-581 and inspect how templateFile and config.templateDir() are combined before getHandlebars(). Reproduce the Maven-plugin generation with a custom template directory on Windows, then verify that custom supporting-file templates resolve without the duplicated path shown in the exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
handlebars, java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.