forked from loafle/openapi-generator-original
Merge pull request #770 from swagger-api/jaxrs-interface-generation
Jaxrs interface generation
This commit is contained in:
commit
0c3f7a54cd
@ -26,6 +26,6 @@ fi
|
|||||||
|
|
||||||
# if you've executed sbt assembly previously it will use that instead.
|
# if you've executed sbt assembly previously it will use that instead.
|
||||||
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
|
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
|
||||||
ags="$@ generate -t modules/swagger-codegen/src/main/resources/JavaJaxRS -i modules/swagger-codegen/src/test/resources/2_0/petstore.json -l jaxrs -o samples/server/petstore/jaxrs"
|
ags="$@ generate -t modules/swagger-codegen/src/main/resources/JavaJaxRS -i modules/swagger-codegen/src/test/resources/2_0/petstore.json -l jaxrs -o samples/server/petstore/jaxrs -Dswagger.codegen.jaxrs.impl.source=src/main/java"
|
||||||
|
|
||||||
java $JAVA_OPTS -jar $executable $ags
|
java $JAVA_OPTS -jar $executable $ags
|
||||||
|
@ -54,10 +54,16 @@ public class Generate implements Runnable {
|
|||||||
"Pass in a URL-encoded string of name:header with a comma separating multiple values")
|
"Pass in a URL-encoded string of name:header with a comma separating multiple values")
|
||||||
private String auth;
|
private String auth;
|
||||||
|
|
||||||
|
@Option( name= {"-D"}, title = "system properties", description = "sets specified system properties in " +
|
||||||
|
"the format of name=value,name=value")
|
||||||
|
private String systemProperties;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
verbosed(verbose);
|
verbosed(verbose);
|
||||||
|
|
||||||
|
setSystemProperties();
|
||||||
|
|
||||||
ClientOptInput input = new ClientOptInput();
|
ClientOptInput input = new ClientOptInput();
|
||||||
|
|
||||||
if (isNotEmpty(auth)) {
|
if (isNotEmpty(auth)) {
|
||||||
@ -77,6 +83,17 @@ public class Generate implements Runnable {
|
|||||||
new DefaultGenerator().opts(input.opts(new ClientOpts()).swagger(swagger)).generate();
|
new DefaultGenerator().opts(input.opts(new ClientOpts()).swagger(swagger)).generate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setSystemProperties() {
|
||||||
|
if( systemProperties != null && systemProperties.length() > 0 ){
|
||||||
|
for( String property : systemProperties.split(",")) {
|
||||||
|
int ix = property.indexOf('=');
|
||||||
|
if( ix > 0 && ix < property.length()-1 ){
|
||||||
|
System.setProperty( property.substring(0, ix), property.substring(ix+1) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If true parameter, adds system properties which enables debug mode in generator
|
* If true parameter, adds system properties which enables debug mode in generator
|
||||||
* @param verbose - if true, enables debug mode
|
* @param verbose - if true, enables debug mode
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
@ -1227,4 +1227,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;
|
||||||
|
}
|
||||||
}
|
}
|
@ -179,11 +179,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) {
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package com.wordnik.swagger.codegen.languages;
|
package com.wordnik.swagger.codegen.languages;
|
||||||
|
|
||||||
import com.wordnik.swagger.models.Operation;
|
import com.wordnik.swagger.models.Operation;
|
||||||
import com.wordnik.swagger.models.Path;
|
|
||||||
import com.wordnik.swagger.util.Json;
|
|
||||||
import com.wordnik.swagger.codegen.*;
|
import com.wordnik.swagger.codegen.*;
|
||||||
import com.wordnik.swagger.models.properties.*;
|
import com.wordnik.swagger.models.properties.*;
|
||||||
|
|
||||||
@ -14,7 +12,6 @@ public class JaxRSServerCodegen extends JavaClientCodegen implements CodegenConf
|
|||||||
protected String groupId = "io.swagger";
|
protected String groupId = "io.swagger";
|
||||||
protected String artifactId = "swagger-jaxrs-server";
|
protected String artifactId = "swagger-jaxrs-server";
|
||||||
protected String artifactVersion = "1.0.0";
|
protected String artifactVersion = "1.0.0";
|
||||||
protected String sourceFolder = "src/main/java";
|
|
||||||
protected String title = "Swagger Server";
|
protected String title = "Swagger Server";
|
||||||
|
|
||||||
public CodegenType getTag() {
|
public CodegenType getTag() {
|
||||||
@ -31,12 +28,19 @@ public class JaxRSServerCodegen extends JavaClientCodegen implements CodegenConf
|
|||||||
|
|
||||||
public JaxRSServerCodegen() {
|
public JaxRSServerCodegen() {
|
||||||
super();
|
super();
|
||||||
outputFolder = "generated-code/javaJaxRS";
|
|
||||||
|
sourceFolder = "src/gen/java";
|
||||||
|
|
||||||
|
outputFolder = System.getProperty( "swagger.codegen.jaxrs.genfolder", "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 = System.getProperty( "swagger.codegen.jaxrs.apipackage", "io.swagger.api") ;
|
||||||
modelPackage = "io.swagger.model";
|
modelPackage = System.getProperty( "swagger.codegen.jaxrs.modelpackage", "io.swagger.model" );
|
||||||
|
|
||||||
additionalProperties.put("invokerPackage", invokerPackage);
|
additionalProperties.put("invokerPackage", invokerPackage);
|
||||||
additionalProperties.put("groupId", groupId);
|
additionalProperties.put("groupId", groupId);
|
||||||
@ -146,4 +150,44 @@ 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";
|
||||||
|
|
||||||
|
String output = System.getProperty( "swagger.codegen.jaxrs.impl.source" );
|
||||||
|
if( output != null ){
|
||||||
|
result = result.replace( apiFileFolder(), implFileFolder(output));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if( templateName.endsWith( "Factory.mustache")){
|
||||||
|
int ix = result.lastIndexOf( '/' );
|
||||||
|
result = result.substring( 0, ix ) + "/factories" + result.substring( ix, result.length()-5 ) + "ServiceFactory.java";
|
||||||
|
|
||||||
|
String output = System.getProperty( "swagger.codegen.jaxrs.impl.source" );
|
||||||
|
if( output != null ){
|
||||||
|
result = result.replace( apiFileFolder(), implFileFolder(output));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if( templateName.endsWith( "Service.mustache")) {
|
||||||
|
int ix = result.lastIndexOf('.');
|
||||||
|
result = result.substring(0, ix) + "Service.java";
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String implFileFolder(String output) {
|
||||||
|
return outputFolder + "/" + output + "/" + apiPackage().replace('.', File.separatorChar);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
@ -25,24 +27,27 @@ import javax.ws.rs.*;
|
|||||||
{{#hasProduces}}@Produces({ {{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }){{/hasProduces}}
|
{{#hasProduces}}@Produces({ {{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }){{/hasProduces}}
|
||||||
@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 abstract class {{classname}}Service {
|
||||||
|
{{#operation}}
|
||||||
|
public abstract 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,32 @@
|
|||||||
|
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 extends {{classname}}Service {
|
||||||
|
{{#operation}}
|
||||||
|
@Override
|
||||||
|
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}}
|
@ -62,6 +62,25 @@
|
|||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>build-helper-maven-plugin</artifactId>
|
||||||
|
<version>1.9.1</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>add-source</id>
|
||||||
|
<phase>generate-sources</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>add-source</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<sources>
|
||||||
|
<source>src/gen/java</source>
|
||||||
|
</sources>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -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