forked from loafle/openapi-generator-original
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:
parent
c7e22b7a3b
commit
75b39e812e
@ -56,4 +56,8 @@ public interface CodegenConfig {
|
|||||||
Map<String, Object> postProcessModels(Map<String, Object> objs);
|
Map<String, Object> postProcessModels(Map<String, Object> objs);
|
||||||
Map<String, Object> postProcessOperations(Map<String, Object> objs);
|
Map<String, Object> postProcessOperations(Map<String, Object> objs);
|
||||||
Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs);
|
Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs);
|
||||||
|
|
||||||
|
String apiFilename(String templateName, String tag);
|
||||||
|
|
||||||
|
boolean shouldOverwrite(String filename);
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -175,11 +175,11 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (String templateName : config.apiTemplateFiles().keySet()) {
|
for (String templateName : config.apiTemplateFiles().keySet()) {
|
||||||
String suffix = config.apiTemplateFiles().get(templateName);
|
|
||||||
String filename = config.apiFileFolder() +
|
String filename = config.apiFilename( templateName, tag );
|
||||||
File.separator +
|
if( new File( filename ).exists() && !config.shouldOverwrite( filename )){
|
||||||
config.toApiFilename(tag) +
|
continue;
|
||||||
suffix;
|
}
|
||||||
|
|
||||||
String template = readTemplate(config.templateDir() + File.separator + templateName);
|
String template = readTemplate(config.templateDir() + File.separator + templateName);
|
||||||
Template tmpl = Mustache.compiler()
|
Template tmpl = Mustache.compiler()
|
||||||
|
@ -130,7 +130,6 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
|||||||
return toModelName(name);
|
return toModelName(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getTypeDeclaration(Property p) {
|
public String getTypeDeclaration(Property p) {
|
||||||
if(p instanceof ArrayProperty) {
|
if(p instanceof ArrayProperty) {
|
||||||
|
@ -31,9 +31,14 @@ public class JaxRSServerCodegen extends JavaClientCodegen implements CodegenConf
|
|||||||
|
|
||||||
public JaxRSServerCodegen() {
|
public JaxRSServerCodegen() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
outputFolder = "generated-code/javaJaxRS";
|
outputFolder = "generated-code/javaJaxRS";
|
||||||
modelTemplateFiles.put("model.mustache", ".java");
|
modelTemplateFiles.put("model.mustache", ".java");
|
||||||
apiTemplateFiles.put("api.mustache", ".java");
|
apiTemplateFiles.put("api.mustache", ".java");
|
||||||
|
apiTemplateFiles.put("apiService.mustache", ".java");
|
||||||
|
apiTemplateFiles.put("apiServiceImpl.mustache", ".java");
|
||||||
|
apiTemplateFiles.put("apiServiceFactory.mustache", ".java");
|
||||||
|
|
||||||
templateDir = "JavaJaxRS";
|
templateDir = "JavaJaxRS";
|
||||||
apiPackage = "io.swagger.api";
|
apiPackage = "io.swagger.api";
|
||||||
modelPackage = "io.swagger.model";
|
modelPackage = "io.swagger.model";
|
||||||
@ -146,4 +151,29 @@ public class JaxRSServerCodegen extends JavaClientCodegen implements CodegenConf
|
|||||||
}
|
}
|
||||||
return objs;
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,6 +1,8 @@
|
|||||||
package {{package}};
|
package {{package}};
|
||||||
|
|
||||||
import {{modelPackage}}.*;
|
import {{modelPackage}}.*;
|
||||||
|
import {{package}}.{{classname}}Service;
|
||||||
|
import {{package}}.factories.{{classname}}ServiceFactory;
|
||||||
|
|
||||||
import com.wordnik.swagger.annotations.ApiParam;
|
import com.wordnik.swagger.annotations.ApiParam;
|
||||||
|
|
||||||
@ -23,24 +25,27 @@ import javax.ws.rs.*;
|
|||||||
@Path("/{{baseName}}")
|
@Path("/{{baseName}}")
|
||||||
@com.wordnik.swagger.annotations.Api(value = "/{{baseName}}", description = "the {{baseName}} API")
|
@com.wordnik.swagger.annotations.Api(value = "/{{baseName}}", description = "the {{baseName}} API")
|
||||||
{{#operations}}
|
{{#operations}}
|
||||||
public class {{classname}} {
|
public class {{classname}} {
|
||||||
{{#operation}}
|
|
||||||
@{{httpMethod}}
|
|
||||||
{{#subresourceOperation}}@Path("{{path}}"){{/subresourceOperation}}
|
|
||||||
{{#hasConsumes}}@Consumes({ {{#consumes}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} }){{/hasConsumes}}
|
|
||||||
{{#hasProduces}}@Produces({ {{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }){{/hasProduces}}
|
|
||||||
@com.wordnik.swagger.annotations.ApiOperation(value = "{{{summary}}}", notes = "{{{notes}}}", response = {{{returnType}}}.class{{#returnContainer}}, responseContainer = "{{{returnContainer}}}"{{/returnContainer}})
|
|
||||||
@com.wordnik.swagger.annotations.ApiResponses(value = { {{#responses}}
|
|
||||||
@com.wordnik.swagger.annotations.ApiResponse(code = {{{code}}}, message = "{{{message}}}"){{#hasMore}},
|
|
||||||
{{/hasMore}}{{/responses}} })
|
|
||||||
|
|
||||||
public Response {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},
|
private final {{classname}}Service delegate = {{classname}}ServiceFactory.get{{classname}}();
|
||||||
|
|
||||||
|
{{#operation}}
|
||||||
|
@{{httpMethod}}
|
||||||
|
{{#subresourceOperation}}@Path("{{path}}"){{/subresourceOperation}}
|
||||||
|
{{#hasConsumes}}@Consumes({ {{#consumes}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} }){{/hasConsumes}}
|
||||||
|
{{#hasProduces}}@Produces({ {{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }){{/hasProduces}}
|
||||||
|
@com.wordnik.swagger.annotations.ApiOperation(value = "{{{summary}}}", notes = "{{{notes}}}", response = {{{returnType}}}.class{{#returnContainer}}, responseContainer = "{{{returnContainer}}}"{{/returnContainer}})
|
||||||
|
@com.wordnik.swagger.annotations.ApiResponses(value = { {{#responses}}
|
||||||
|
@com.wordnik.swagger.annotations.ApiResponse(code = {{{code}}}, message = "{{{message}}}"){{#hasMore}},
|
||||||
|
{{/hasMore}}{{/responses}} })
|
||||||
|
|
||||||
|
public Response {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},
|
||||||
{{/hasMore}}{{/allParams}})
|
{{/hasMore}}{{/allParams}})
|
||||||
throws NotFoundException {
|
throws NotFoundException {
|
||||||
// do some magic!
|
// do some magic!
|
||||||
return Response.ok().entity(new ApiResponseMessage(ApiResponseMessage.OK, "magic!")).build();
|
return delegate.{{nickname}}({{#allParams}}{{#isFile}}fileDetail{{/isFile}}{{^isFile}}{{paramName}}{{/isFile}}{{#hasMore}},{{/hasMore}}{{/allParams}});
|
||||||
}
|
}
|
||||||
|
{{/operation}}
|
||||||
{{/operation}}
|
|
||||||
}
|
}
|
||||||
{{/operations}}
|
{{/operations}}
|
||||||
|
|
||||||
|
@ -0,0 +1,28 @@
|
|||||||
|
package {{package}};
|
||||||
|
|
||||||
|
import {{package}}.*;
|
||||||
|
import {{modelPackage}}.*;
|
||||||
|
|
||||||
|
import com.sun.jersey.multipart.FormDataParam;
|
||||||
|
|
||||||
|
{{#imports}}import {{import}};
|
||||||
|
{{/imports}}
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import {{package}}.NotFoundException;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
import com.sun.jersey.core.header.FormDataContentDisposition;
|
||||||
|
import com.sun.jersey.multipart.FormDataParam;
|
||||||
|
|
||||||
|
import javax.ws.rs.core.Response;
|
||||||
|
|
||||||
|
{{#operations}}
|
||||||
|
public interface {{classname}}Service {
|
||||||
|
{{#operation}}
|
||||||
|
public Response {{nickname}}({{#allParams}}{{>serviceQueryParams}}{{>servicePathParams}}{{>serviceHeaderParams}}{{>serviceBodyParams}}{{>serviceFormParams}}{{#hasMore}},{{/hasMore}}{{/allParams}})
|
||||||
|
throws NotFoundException;
|
||||||
|
{{/operation}}
|
||||||
|
}
|
||||||
|
{{/operations}}
|
@ -0,0 +1,14 @@
|
|||||||
|
package {{package}}.factories;
|
||||||
|
|
||||||
|
import {{package}}.{{classname}}Service;
|
||||||
|
import {{package}}.impl.{{classname}}ServiceImpl;
|
||||||
|
|
||||||
|
public class {{classname}}ServiceFactory {
|
||||||
|
|
||||||
|
private final static {{classname}}Service service = new {{classname}}ServiceImpl();
|
||||||
|
|
||||||
|
public static {{classname}}Service get{{classname}}()
|
||||||
|
{
|
||||||
|
return service;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package {{package}}.impl;
|
||||||
|
|
||||||
|
import {{package}}.*;
|
||||||
|
import {{modelPackage}}.*;
|
||||||
|
|
||||||
|
import com.sun.jersey.multipart.FormDataParam;
|
||||||
|
|
||||||
|
{{#imports}}import {{import}};
|
||||||
|
{{/imports}}
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import {{package}}.NotFoundException;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
import com.sun.jersey.core.header.FormDataContentDisposition;
|
||||||
|
import com.sun.jersey.multipart.FormDataParam;
|
||||||
|
|
||||||
|
import javax.ws.rs.core.Response;
|
||||||
|
|
||||||
|
{{#operations}}
|
||||||
|
public class {{classname}}ServiceImpl implements {{classname}}Service {
|
||||||
|
{{#operation}}
|
||||||
|
public Response {{nickname}}({{#allParams}}{{>serviceQueryParams}}{{>servicePathParams}}{{>serviceHeaderParams}}{{>serviceBodyParams}}{{>serviceFormParams}}{{#hasMore}},{{/hasMore}}{{/allParams}})
|
||||||
|
throws NotFoundException {
|
||||||
|
// do some magic!
|
||||||
|
return Response.ok().entity(new ApiResponseMessage(ApiResponseMessage.OK, "magic!")).build();
|
||||||
|
}
|
||||||
|
{{/operation}}
|
||||||
|
}
|
||||||
|
{{/operations}}
|
@ -129,7 +129,7 @@
|
|||||||
</repository>
|
</repository>
|
||||||
</repositories>
|
</repositories>
|
||||||
<properties>
|
<properties>
|
||||||
<swagger-core-version>2.1.1-M2-SNAPSHOT</swagger-core-version>
|
<swagger-core-version>1.5.2-M2-SNAPSHOT</swagger-core-version>
|
||||||
<jetty-version>9.2.9.v20150224</jetty-version>
|
<jetty-version>9.2.9.v20150224</jetty-version>
|
||||||
<jersey-version>1.13</jersey-version>
|
<jersey-version>1.13</jersey-version>
|
||||||
<slf4j-version>1.6.3</slf4j-version>
|
<slf4j-version>1.6.3</slf4j-version>
|
||||||
|
@ -0,0 +1 @@
|
|||||||
|
{{#isBodyParam}}{{{dataType}}} {{paramName}}{{/isBodyParam}}
|
@ -0,0 +1 @@
|
|||||||
|
{{#isFormParam}}{{#notFile}}{{{dataType}}} {{paramName}}{{/notFile}}{{#isFile}}FormDataContentDisposition fileDetail{{/isFile}}{{/isFormParam}}
|
@ -0,0 +1 @@
|
|||||||
|
{{#isHeaderParam}}{{{dataType}}} {{paramName}}{{/isHeaderParam}}
|
@ -0,0 +1 @@
|
|||||||
|
{{#isPathParam}}{{{dataType}}} {{paramName}}{{/isPathParam}}
|
@ -0,0 +1 @@
|
|||||||
|
{{#isQueryParam}}{{{dataType}}} {{paramName}}{{/isQueryParam}}
|
Loading…
x
Reference in New Issue
Block a user