Skip to content

Commit dd53f84

Browse files
committed
Merge branch 'arturdzm-master'
2 parents 3c9b172 + e2e0af1 commit dd53f84

28 files changed

+2398
-0
lines changed

bin/jaxrs-spec-petstore-server.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/sh
2+
3+
SCRIPT="$0"
4+
5+
while [ -h "$SCRIPT" ] ; do
6+
ls=`ls -ld "$SCRIPT"`
7+
link=`expr "$ls" : '.*-> \(.*\)$'`
8+
if expr "$link" : '/.*' > /dev/null; then
9+
SCRIPT="$link"
10+
else
11+
SCRIPT=`dirname "$SCRIPT"`/"$link"
12+
fi
13+
done
14+
15+
if [ ! -d "${APP_DIR}" ]; then
16+
APP_DIR=`dirname "$SCRIPT"`/..
17+
APP_DIR=`cd "${APP_DIR}"; pwd`
18+
fi
19+
20+
executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar"
21+
22+
if [ ! -f "$executable" ]
23+
then
24+
mvn clean package
25+
fi
26+
27+
# if you've executed sbt assembly previously it will use that instead.
28+
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
29+
ags="$@ generate -t modules/swagger-codegen/src/main/resources/JavaJaxRS -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l jaxrs-spec -o samples/server/petstore/jaxrs-spec -DhideGenerationTimestamp=true"
30+
31+
java $JAVA_OPTS -jar $executable $ags
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package io.swagger.codegen.languages;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
6+
import java.util.ArrayList;
7+
import java.util.LinkedHashMap;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
import io.swagger.codegen.CliOption;
12+
import io.swagger.codegen.CodegenConstants;
13+
import io.swagger.codegen.CodegenModel;
14+
import io.swagger.codegen.CodegenOperation;
15+
import io.swagger.codegen.CodegenProperty;
16+
import io.swagger.codegen.SupportingFile;
17+
import io.swagger.models.Operation;
18+
import io.swagger.models.Swagger;
19+
import io.swagger.util.Json;
20+
import org.apache.commons.io.FileUtils;
21+
import com.fasterxml.jackson.core.JsonProcessingException;
22+
23+
public class JavaJAXRSSpecServerCodegen extends AbstractJavaJAXRSServerCodegen
24+
{
25+
public JavaJAXRSSpecServerCodegen()
26+
{
27+
super();
28+
supportsInheritance = true;
29+
sourceFolder = "src/main/java";
30+
invokerPackage = "io.swagger.api";
31+
artifactId = "swagger-jaxrs-server";
32+
outputFolder = "generated-code/JavaJaxRS-Spec";
33+
34+
modelTemplateFiles.put("model.mustache", ".java");
35+
apiTemplateFiles.put("api.mustache", ".java");
36+
apiPackage = "io.swagger.api";
37+
modelPackage = "io.swagger.model";
38+
39+
additionalProperties.put("title", title);
40+
41+
typeMapping.put("date", "LocalDate");
42+
typeMapping.put("DateTime", "javax.xml.datatype.XMLGregorianCalendar"); // Map DateTime fields to Java standart class 'XMLGregorianCalendar'
43+
44+
importMapping.put("LocalDate", "org.joda.time.LocalDate");
45+
46+
super.embeddedTemplateDir = templateDir = JAXRS_TEMPLATE_DIRECTORY_NAME + File.separator + "spec";
47+
48+
for ( int i = 0; i < cliOptions.size(); i++ ) {
49+
if ( CodegenConstants.LIBRARY.equals(cliOptions.get(i).getOpt()) ) {
50+
cliOptions.remove(i);
51+
break;
52+
}
53+
}
54+
55+
CliOption library = new CliOption(CodegenConstants.LIBRARY, "library template (sub-template) to use");
56+
library.setDefault(DEFAULT_LIBRARY);
57+
58+
Map<String, String> supportedLibraries = new LinkedHashMap<String,String>();
59+
60+
supportedLibraries.put(DEFAULT_LIBRARY, "JAXRS");
61+
library.setEnum(supportedLibraries);
62+
63+
cliOptions.add(library);
64+
cliOptions.add(new CliOption(CodegenConstants.IMPL_FOLDER, CodegenConstants.IMPL_FOLDER_DESC));
65+
cliOptions.add(new CliOption("title", "a title describing the application"));
66+
}
67+
68+
@Override
69+
public void processOpts()
70+
{
71+
super.processOpts();
72+
73+
supportingFiles.clear(); // Don't need extra files provided by AbstractJAX-RS & Java Codegen
74+
writeOptional(outputFolder, new SupportingFile("pom.mustache", "", "pom.xml"));
75+
76+
writeOptional(outputFolder, new SupportingFile("RestApplication.mustache",
77+
(sourceFolder + '/' + invokerPackage).replace(".", "/"), "RestApplication.java"));
78+
79+
}
80+
81+
@Override
82+
public String getName()
83+
{
84+
return "jaxrs-spec";
85+
}
86+
87+
@Override
88+
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
89+
String basePath = resourcePath;
90+
if (basePath.startsWith("/")) {
91+
basePath = basePath.substring(1);
92+
}
93+
int pos = basePath.indexOf("/");
94+
if (pos > 0) {
95+
basePath = basePath.substring(0, pos);
96+
}
97+
98+
if (basePath == "") {
99+
basePath = "default";
100+
} else {
101+
if (co.path.startsWith("/" + basePath)) {
102+
co.path = co.path.substring(("/" + basePath).length());
103+
}
104+
co.subresourceOperation = !co.path.isEmpty();
105+
}
106+
List<CodegenOperation> opList = operations.get(basePath);
107+
if (opList == null) {
108+
opList = new ArrayList<CodegenOperation>();
109+
operations.put(basePath, opList);
110+
}
111+
opList.add(co);
112+
co.baseName = basePath;
113+
}
114+
115+
@Override
116+
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
117+
super.postProcessModelProperty(model, property);
118+
model.imports.remove("ApiModelProperty");
119+
model.imports.remove("ApiModel");
120+
model.imports.remove("JsonSerialize");
121+
model.imports.remove("ToStringSerializer");
122+
model.imports.remove("JsonValue");
123+
model.imports.remove("JsonProperty");
124+
}
125+
126+
@Override
127+
public void preprocessSwagger(Swagger swagger) {
128+
//copy input swagger to output folder
129+
try {
130+
String swaggerJson = Json.pretty(swagger);
131+
FileUtils.writeStringToFile(new File(outputFolder + File.separator + "swagger.json"), swaggerJson);
132+
} catch (JsonProcessingException e) {
133+
throw new RuntimeException(e.getMessage(), e.getCause());
134+
} catch (IOException e) {
135+
throw new RuntimeException(e.getMessage(), e.getCause());
136+
}
137+
super.preprocessSwagger(swagger);
138+
139+
}
140+
@Override
141+
public String getHelp()
142+
{
143+
return "Generates a Java JAXRS Server according to JAXRS 2.0 specification.";
144+
}
145+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package {{invokerPackage}};
2+
3+
import javax.ws.rs.ApplicationPath;
4+
import javax.ws.rs.core.Application;
5+
6+
@ApplicationPath("/")
7+
public class RestApplication extends Application {
8+
9+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{#allowableValues}}allowableValues="{{#values}}{{{.}}}{{^-last}}, {{/-last}}{{/values}}{{^values}}range=[{{#min}}{{.}}{{/min}}{{^min}}-infinity{{/min}}, {{#max}}{{.}}{{/max}}{{^max}}infinity{{/max}}]{{/values}}"{{/allowableValues}}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package {{package}};
2+
3+
{{#imports}}import {{import}};
4+
{{/imports}}
5+
6+
import javax.ws.rs.*;
7+
import javax.ws.rs.core.Response;
8+
9+
import io.swagger.annotations.*;
10+
11+
import java.util.List;
12+
13+
@Path("/{{baseName}}")
14+
15+
@Api(description = "the {{baseName}} API")
16+
{{#hasConsumes}}@Consumes({ {{#consumes}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} }){{/hasConsumes}}
17+
{{#hasProduces}}@Produces({ {{#produces}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }){{/hasProduces}}
18+
{{>generatedAnnotation}}
19+
20+
public class {{classname}} {
21+
{{#operations}}
22+
{{#operation}}
23+
24+
@{{httpMethod}}
25+
{{#subresourceOperation}}@Path("{{path}}"){{/subresourceOperation}}
26+
{{#hasConsumes}}@Consumes({ {{#consumes}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} }){{/hasConsumes}}
27+
{{#hasProduces}}@Produces({ {{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }){{/hasProduces}}
28+
@ApiOperation(value = "{{{summary}}}", notes = "{{{notes}}}", response = {{{returnType}}}.class{{#returnContainer}}, responseContainer = "{{{returnContainer}}}"{{/returnContainer}}{{#hasAuthMethods}}, authorizations = {
29+
{{#authMethods}}@Authorization(value = "{{name}}"{{#isOAuth}}, scopes = {
30+
{{#scopes}}@AuthorizationScope(scope = "{{scope}}", description = "{{description}}"){{#hasMore}},
31+
{{/hasMore}}{{/scopes}}
32+
}{{/isOAuth}}){{#hasMore}},
33+
{{/hasMore}}{{/authMethods}}
34+
}{{/hasAuthMethods}}, tags={ {{#vendorExtensions.x-tags}}"{{tag}}"{{#hasMore}}, {{/hasMore}}{{/vendorExtensions.x-tags}} })
35+
@ApiResponses(value = { {{#responses}}
36+
@ApiResponse(code = {{{code}}}, message = "{{{message}}}", response = {{{returnType}}}.class{{#returnContainer}}, responseContainer = "{{{returnContainer}}}"{{/returnContainer}}){{#hasMore}},{{/hasMore}}{{/responses}} })
37+
public Response {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},{{/hasMore}}{{/allParams}}) {
38+
return Response.ok().entity("magic!").build();
39+
}
40+
{{/operation}}
41+
}
42+
{{/operations}}
43+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{#isBodyParam}}{{{dataType}}} {{paramName}}{{/isBodyParam}}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import javax.xml.bind.annotation.XmlEnum;
2+
import javax.xml.bind.annotation.XmlType;
3+
4+
@XmlType(name="{{classname}}")
5+
@XmlEnum
6+
public enum {{classname}} {
7+
{{#allowableValues}}{{.}}{{^-last}}, {{/-last}}{{#-last}};{{/-last}}{{/allowableValues}}
8+
9+
public String value() {
10+
return name();
11+
}
12+
13+
public static {{classname}} fromValue(String v) {
14+
return valueOf(v);
15+
}
16+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{{#isFormParam}}{{#notFile}}@FormParam(value = "{{paramName}}"{{^required}}, required = false{{/required}}) {{{dataType}}} {{paramName}}{{/notFile}}{{#isFile}} @FormParam(value = "{{paramName}}"{{^required}}, required = false{{/required}}) InputStream {{paramName}}InputStream,
2+
@FormParam(value = "{{paramName}}" {{^required}}, required = false{{/required}}) Attachment {{paramName}}Detail{{/isFile}}{{/isFormParam}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@javax.annotation.Generated(value = "{{generatorClass}}", date = "{{generatedDate}}")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{#isHeaderParam}}@HeaderParam("{{baseName}}") {{{dataType}}} {{paramName}}{{/isHeaderParam}}

0 commit comments

Comments
 (0)