refactored to create separate serviceinterface, implementation and corresponding factory. JaxRs class uses the factory to find the implementation. Also fixed possibility for a code generator to control which API files that should be overwritten

This commit is contained in:
Ole Lensmar
2015-05-12 13:18:35 -04:00
parent c7e22b7a3b
commit 75b39e812e
15 changed files with 149 additions and 24 deletions

View File

@@ -56,4 +56,8 @@ public interface CodegenConfig {
Map<String, Object> postProcessModels(Map<String, Object> objs);
Map<String, Object> postProcessOperations(Map<String, Object> objs);
Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs);
String apiFilename(String templateName, String tag);
boolean shouldOverwrite(String filename);
}

View File

@@ -1169,4 +1169,13 @@ public class DefaultCodegen {
}
public String apiFilename(String templateName, String tag)
{
String suffix = apiTemplateFiles().get(templateName);
return apiFileFolder() + File.separator + toApiFilename(tag) + suffix;
}
public boolean shouldOverwrite( String filename ){
return true;
}
}

View File

@@ -175,11 +175,11 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
}
for (String templateName : config.apiTemplateFiles().keySet()) {
String suffix = config.apiTemplateFiles().get(templateName);
String filename = config.apiFileFolder() +
File.separator +
config.toApiFilename(tag) +
suffix;
String filename = config.apiFilename( templateName, tag );
if( new File( filename ).exists() && !config.shouldOverwrite( filename )){
continue;
}
String template = readTemplate(config.templateDir() + File.separator + templateName);
Template tmpl = Mustache.compiler()

View File

@@ -130,7 +130,6 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
return toModelName(name);
}
@Override
public String getTypeDeclaration(Property p) {
if(p instanceof ArrayProperty) {

View File

@@ -31,9 +31,14 @@ public class JaxRSServerCodegen extends JavaClientCodegen implements CodegenConf
public JaxRSServerCodegen() {
super();
outputFolder = "generated-code/javaJaxRS";
modelTemplateFiles.put("model.mustache", ".java");
apiTemplateFiles.put("api.mustache", ".java");
apiTemplateFiles.put("apiService.mustache", ".java");
apiTemplateFiles.put("apiServiceImpl.mustache", ".java");
apiTemplateFiles.put("apiServiceFactory.mustache", ".java");
templateDir = "JavaJaxRS";
apiPackage = "io.swagger.api";
modelPackage = "io.swagger.model";
@@ -146,4 +151,29 @@ public class JaxRSServerCodegen extends JavaClientCodegen implements CodegenConf
}
return objs;
}
@Override
public String apiFilename(String templateName, String tag) {
String result = super.apiFilename(templateName, tag);
if( templateName.endsWith( "Impl.mustache")){
int ix = result.lastIndexOf( '/' );
result = result.substring( 0, ix ) + "/impl" + result.substring( ix, result.length()-5 ) + "ServiceImpl.java";
} else if( templateName.endsWith( "Service.mustache")){
int ix = result.lastIndexOf( '.' );
result = result.substring( 0, ix ) + "Service.java";
}
else if( templateName.endsWith( "Factory.mustache")){
int ix = result.lastIndexOf( '/' );
result = result.substring( 0, ix ) + "/factories" + result.substring( ix, result.length()-5 ) + "ServiceFactory.java";
}
return result;
}
public boolean shouldOverwrite( String filename ){
return !filename.endsWith( "ServiceImpl.java") && !filename.endsWith( "ServiceFactory.java");
}
}