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.03k stars 6.03k forks source link

Custom templates don't work on Windows #8552

Open demonfiddler opened 6 years ago

demonfiddler commented 6 years ago
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.

HugoMario commented 6 years ago

hello @demonfiddler , can you please try with 3.0.0-SNAPSHOT?

demonfiddler commented 6 years ago

Hello @HugoMario, thank you for the response. Whence may one obtain io.swagger:swagger-codegen-maven-plugin:3.0.0-SNAPSHOT? It's not on Maven Central. Or is there some other central snapshot repo of which I'm unaware? Or do I have to build it myself?