diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/AbstractGenerator.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/AbstractGenerator.java index 0d530e7c959..2c90f4255ec 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/AbstractGenerator.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/AbstractGenerator.java @@ -60,6 +60,23 @@ public abstract class AbstractGenerator { throw new RuntimeException("can't load template " + name); } + /** + * Get the template file path with template dir prepended, and use the + * library template if exists. + */ + public String getFullTemplateFile(CodegenConfig config, String templateFile) { + String library = config.getLibrary(); + if (library != null && !"".equals(library)) { + String libTemplateFile = config.templateDir() + File.separator + + "libraries" + File.separator + library + File.separator + + templateFile; + if (templateExists(libTemplateFile)) { + return libTemplateFile; + } + } + return config.templateDir() + File.separator + templateFile; + } + public boolean templateExists(String name) { return this.getClass().getClassLoader().getResource(getCPResourcePath(name)) != null; } diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java index 535c9baec72..37d65473487 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java @@ -139,7 +139,8 @@ public class DefaultGenerator extends AbstractGenerator implements Generator { if (!config.shouldOverwrite(filename)) { continue; } - String template = readTemplate(config.templateDir() + File.separator + templateName); + String templateFile = getFullTemplateFile(config, templateName); + String template = readTemplate(templateFile); Template tmpl = Mustache.compiler() .withLoader(new Mustache.TemplateLoader() { public Reader getTemplate(String name) { @@ -191,7 +192,8 @@ public class DefaultGenerator extends AbstractGenerator implements Generator { continue; } - String template = readTemplate(config.templateDir() + File.separator + templateName); + String templateFile = getFullTemplateFile(config, templateName); + String template = readTemplate(templateFile); Template tmpl = Mustache.compiler() .withLoader(new Mustache.TemplateLoader() { public Reader getTemplate(String name) { @@ -262,19 +264,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator { continue; } - String templateFile = null; - String library = config.getLibrary(); - if (library != null && !"".equals(library)) { - String libTemplateFile = config.templateDir() + File.separator + - "libraries" + File.separator + library + File.separator + - support.templateFile; - if (templateExists(libTemplateFile)) { - templateFile = libTemplateFile; - } - } - if (templateFile == null) { - templateFile = config.templateDir() + File.separator + support.templateFile; - } + String templateFile = getFullTemplateFile(config, support.templateFile); if (templateFile.endsWith("mustache")) { String template = readTemplate(templateFile);