-
Notifications
You must be signed in to change notification settings - Fork 6k
Add jersey2 server support #2488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/bin/sh | ||
|
||
SCRIPT="$0" | ||
|
||
while [ -h "$SCRIPT" ] ; do | ||
ls=`ls -ld "$SCRIPT"` | ||
link=`expr "$ls" : '.*-> \(.*\)$'` | ||
if expr "$link" : '/.*' > /dev/null; then | ||
SCRIPT="$link" | ||
else | ||
SCRIPT=`dirname "$SCRIPT"`/"$link" | ||
fi | ||
done | ||
|
||
if [ ! -d "${APP_DIR}" ]; then | ||
APP_DIR=`dirname "$SCRIPT"`/.. | ||
APP_DIR=`cd "${APP_DIR}"; pwd` | ||
fi | ||
|
||
executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar" | ||
|
||
if [ ! -f "$executable" ] | ||
then | ||
mvn clean package | ||
fi | ||
|
||
# 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" | ||
ags="$@ generate -t modules/swagger-codegen/src/main/resources/JavaJaxRS -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l jaxrs -o samples/server/petstore/jersey2 --library=jersey2" | ||
|
||
java $JAVA_OPTS -jar $executable $ags |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package io.swagger.codegen.languages; | ||
|
||
import io.swagger.codegen.CodegenOperation; | ||
import io.swagger.codegen.CodegenParameter; | ||
import io.swagger.codegen.CodegenResponse; | ||
import io.swagger.codegen.CodegenType; | ||
import io.swagger.models.Operation; | ||
|
@@ -95,6 +96,25 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) { | |
@SuppressWarnings("unchecked") | ||
List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation"); | ||
for ( CodegenOperation operation : ops ) { | ||
boolean isMultipartPost = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This addresses an issue where |
||
List<Map<String, String>> consumes = operation.consumes; | ||
if(consumes != null) { | ||
for(Map<String, String> consume : consumes) { | ||
String mt = consume.get("mediaType"); | ||
if(mt != null) { | ||
if(mt.startsWith("multipart/form-data")) { | ||
isMultipartPost = true; | ||
} | ||
} | ||
} | ||
} | ||
|
||
for(CodegenParameter parameter : operation.allParams) { | ||
if(isMultipartPost) { | ||
parameter.vendorExtensions.put("x-multipart", "true"); | ||
} | ||
} | ||
|
||
List<CodegenResponse> responses = operation.responses; | ||
if ( responses != null ) { | ||
for ( CodegenResponse resp : responses ) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,10 @@ | |
import java.io.File; | ||
import java.util.*; | ||
|
||
public class JavaJerseyServerCodegen extends AbstractJavaJAXRSServerCodegen | ||
{ | ||
public class JavaJerseyServerCodegen extends AbstractJavaJAXRSServerCodegen { | ||
boolean showGenerationTimestamp = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made generation timestamps optional in server generation |
||
|
||
public JavaJerseyServerCodegen() | ||
{ | ||
public JavaJerseyServerCodegen() { | ||
super(); | ||
|
||
sourceFolder = "src/gen/java"; | ||
|
@@ -43,11 +42,13 @@ public JavaJerseyServerCodegen() | |
Map<String, String> supportedLibraries = new LinkedHashMap<String, String>(); | ||
|
||
supportedLibraries.put(DEFAULT_LIBRARY, "Jersey core 1.18.1"); | ||
supportedLibraries.put("jersey2", "Jersey core 2.x"); | ||
library.setEnum(supportedLibraries); | ||
|
||
cliOptions.add(library); | ||
cliOptions.add(new CliOption(CodegenConstants.IMPL_FOLDER, CodegenConstants.IMPL_FOLDER_DESC)); | ||
cliOptions.add(new CliOption("title", "a title describing the application")); | ||
cliOptions.add(new CliOption("showGenerationTimestamp", "shows the timestamp when files were generated")); | ||
} | ||
|
||
@Override | ||
|
@@ -85,13 +86,34 @@ public void processOpts() { | |
supportingFiles.add(new SupportingFile("ApiOriginFilter.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "ApiOriginFilter.java")); | ||
supportingFiles.add(new SupportingFile("ApiResponseMessage.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "ApiResponseMessage.java")); | ||
supportingFiles.add(new SupportingFile("NotFoundException.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "NotFoundException.java")); | ||
supportingFiles.add(new SupportingFile("jacksonJsonProvider.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "JacksonJsonProvider.java")); | ||
writeOptional(outputFolder, new SupportingFile("bootstrap.mustache", (implFolder + '/' + apiPackage).replace(".", "/"), "Bootstrap.java")); | ||
|
||
writeOptional(outputFolder, new SupportingFile("web.mustache", ("src/main/webapp/WEB-INF"), "web.xml")); | ||
supportingFiles.add(new SupportingFile("StringUtil.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "StringUtil.java")); | ||
|
||
if ( additionalProperties.containsKey("dateLibrary") ) { | ||
setDateLibrary(additionalProperties.get("dateLibrary").toString()); | ||
additionalProperties.put(dateLibrary, "true"); | ||
} | ||
if(DEFAULT_LIBRARY.equals(library) || library == null) { | ||
if(templateDir.startsWith(JAXRS_TEMPLATE_DIRECTORY_NAME)) { | ||
// set to the default location | ||
templateDir = JAXRS_TEMPLATE_DIRECTORY_NAME + File.separator + "jersey1_18"; | ||
} | ||
else { | ||
templateDir += File.separator + "jersey1_18"; | ||
} | ||
} | ||
if("jersey2".equals(library)) { | ||
if(templateDir.startsWith(JAXRS_TEMPLATE_DIRECTORY_NAME)) { | ||
// set to the default location | ||
templateDir = JAXRS_TEMPLATE_DIRECTORY_NAME + File.separator + "jersey2"; | ||
} | ||
else { | ||
templateDir += File.separator + "jersey2"; | ||
} | ||
} | ||
|
||
if ( "joda".equals(dateLibrary) ) { | ||
supportingFiles.add(new SupportingFile("JodaDateTimeProvider.mustache", (sourceFolder + '/' + apiPackage).replace(".", "/"), "JodaDateTimeProvider.java")); | ||
|
@@ -128,5 +150,9 @@ public void addOperationToGroup(String tag, String resourcePath, Operation opera | |
} | ||
opList.add(co); | ||
co.baseName = basePath; | ||
} | ||
} | ||
|
||
public void showGenerationTimestamp(boolean showGenerationTimestamp) { | ||
this.showGenerationTimestamp = showGenerationTimestamp; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package {{apiPackage}}; | ||
|
||
import io.swagger.jaxrs.config.SwaggerContextService; | ||
import io.swagger.models.*; | ||
|
||
import io.swagger.models.auth.*; | ||
|
||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.ServletContext; | ||
import javax.servlet.ServletConfig; | ||
import javax.servlet.ServletException; | ||
|
||
public class Bootstrap extends HttpServlet { | ||
@Override | ||
public void init(ServletConfig config) throws ServletException { | ||
Info info = new Info() | ||
.title("{{title}}") | ||
.description("{{{appDescription}}}") | ||
.termsOfService("{{termsOfService}}") | ||
.contact(new Contact() | ||
.email("{{infoEmail}}")) | ||
.license(new License() | ||
.name("{{licenseInfo}}") | ||
.url("{{licenseUrl}}")); | ||
|
||
ServletContext context = config.getServletContext(); | ||
Swagger swagger = new Swagger().info(info); | ||
|
||
new SwaggerContextService().withServletConfig(config).updateSwagger(swagger); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
{{#isFormParam}}{{#notFile}}@ApiParam(value = "{{{description}}}"{{#required}}, required=true{{/required}}{{#allowableValues}}, {{> allowableValues }}{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}})@FormParam("{{paramName}}") {{{dataType}}} {{paramName}}{{/notFile}}{{#isFile}} @FormDataParam("file") InputStream inputStream, | ||
@FormDataParam("file") FormDataContentDisposition fileDetail{{/isFile}}{{/isFormParam}} | ||
{{#isFormParam}}{{#notFile}}{{^vendorExtensions.x-multipart}}@ApiParam(value = "{{{description}}}"{{#required}}, required=true{{/required}}{{#allowableValues}}, {{> allowableValues }}{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}){{/vendorExtensions.x-multipart}}{{#vendorExtensions.x-multipart}}@FormDataParam("{{paramName}}") {{{dataType}}} {{paramName}}{{/vendorExtensions.x-multipart}}{{^vendorExtensions.x-multipart}}@FormParam("{{paramName}}") {{{dataType}}} {{paramName}}{{/vendorExtensions.x-multipart}}{{/notFile}}{{#isFile}}@FormDataParam("file") InputStream inputStream, | ||
@FormDataParam("file") FormDataContentDisposition fileDetail{{/isFile}}{{/isFormParam}} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
@javax.annotation.Generated(value = "{{generatorClass}}", date = "{{generatedDate}}") | ||
{{#showGenerationTimestamps}}@javax.annotation.Generated(value = "{{generatorClass}}", date = "{{generatedDate}}"){{/showGenerationTimestamps}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed to use
petstore.yaml
as it doesn't have any of the test constructs in it, and re-adds file upload support