Skip to content

Commit de727d7

Browse files
jfialawing328
authored andcommitted
[JaxrsResteasy] Improvements for Resteasy for Jboss EAP 4512 (#4712)
* add language for resteasy eap #4512 * first implementation of jaxrs-resteasy-eap #4512 * add support for joda and java8 datetime types #4512 * add new file JacksonConfig and new sample eap-joda #4512 * add dynamic swagger support to jaxrs-resteasy-eap #4512 * adapt readme for eap #4512 * add tests for jaxrs-resteasy-eap #4512 * cleanup tabs #4512 * cleanup tabs #4512 * cleanup tabs #4512 * cleanup tabs / regenerate without joda #4512 * regenerate with updated templates #4512 * fix test (wrong setter invoked) #4512 * fix test #4512 * fix no invocation #4512 * replace tabs with spaces #4512
1 parent d11d0f8 commit de727d7

File tree

80 files changed

+4098
-6
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+4098
-6
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractJavaCodegen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ public void processOpts() {
357357
}
358358

359359
if("joda".equals(dateLibrary)) {
360+
additionalProperties.put("joda", "true");
360361
typeMapping.put("date", "LocalDate");
361362
typeMapping.put("DateTime", "DateTime");
362363

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
package io.swagger.codegen.languages;
2+
3+
import java.io.File;
4+
import java.util.ArrayList;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
import org.apache.commons.lang3.BooleanUtils;
10+
11+
import io.swagger.codegen.CliOption;
12+
import io.swagger.codegen.CodegenModel;
13+
import io.swagger.codegen.CodegenOperation;
14+
import io.swagger.codegen.CodegenProperty;
15+
import io.swagger.codegen.CodegenResponse;
16+
import io.swagger.codegen.SupportingFile;
17+
import io.swagger.codegen.languages.features.BeanValidationFeatures;
18+
import io.swagger.codegen.languages.features.JbossFeature;
19+
import io.swagger.codegen.languages.features.SwaggerFeatures;
20+
import io.swagger.models.Operation;
21+
22+
public class JavaResteasyEapServerCodegen extends AbstractJavaJAXRSServerCodegen
23+
implements JbossFeature, BeanValidationFeatures, SwaggerFeatures {
24+
25+
protected boolean useBeanValidation = true;
26+
protected boolean generateJbossDeploymentDescriptor = true;
27+
protected boolean useSwaggerFeature = false;
28+
29+
public JavaResteasyEapServerCodegen() {
30+
31+
super();
32+
33+
artifactId = "swagger-jaxrs-resteasy-eap-server";
34+
35+
outputFolder = "generated-code/JavaJaxRS-Resteasy-eap";
36+
apiTemplateFiles.put("apiServiceImpl.mustache", ".java");
37+
apiTestTemplateFiles.clear(); // TODO: add test template
38+
39+
// clear model and api doc template as AbstractJavaJAXRSServerCodegen
40+
// does not support auto-generated markdown doc at the moment
41+
//TODO: add doc templates
42+
modelDocTemplateFiles.remove("model_doc.mustache");
43+
apiDocTemplateFiles.remove("api_doc.mustache");
44+
45+
dateLibrary = "legacy";// TODO: change to joda
46+
47+
embeddedTemplateDir = templateDir = "JavaJaxRS" + File.separator + "resteasy" + File.separator + "eap";
48+
49+
cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION, "Use BeanValidation API annotations"));
50+
cliOptions.add(CliOption.newBoolean(GENERATE_JBOSS_DEPLOYMENT_DESCRIPTOR, "Generate Jboss Deployment Descriptor"));
51+
cliOptions.add(CliOption.newBoolean(USE_SWAGGER_FEATURE, "Use dynamic Swagger generator"));
52+
53+
}
54+
55+
@Override
56+
public String getName() {
57+
return "jaxrs-resteasy-eap";
58+
}
59+
60+
@Override
61+
public String getHelp() {
62+
return "Generates a Java JAXRS-Resteasy Server application.";
63+
}
64+
65+
@Override
66+
public void processOpts() {
67+
super.processOpts();
68+
69+
if (additionalProperties.containsKey(GENERATE_JBOSS_DEPLOYMENT_DESCRIPTOR)) {
70+
boolean generateJbossDeploymentDescriptorProp = convertPropertyToBooleanAndWriteBack(GENERATE_JBOSS_DEPLOYMENT_DESCRIPTOR);
71+
this.setGenerateJbossDeploymentDescriptor(generateJbossDeploymentDescriptorProp);
72+
}
73+
74+
if (additionalProperties.containsKey(USE_BEANVALIDATION)) {
75+
this.setUseBeanValidation(convertPropertyToBoolean(USE_BEANVALIDATION));
76+
}
77+
78+
if (useBeanValidation) {
79+
writePropertyBack(USE_BEANVALIDATION, useBeanValidation);
80+
}
81+
82+
if (additionalProperties.containsKey(USE_SWAGGER_FEATURE)) {
83+
this.setUseSwaggerFeature(convertPropertyToBoolean(USE_SWAGGER_FEATURE));
84+
}
85+
86+
if (useSwaggerFeature) {
87+
writePropertyBack(USE_SWAGGER_FEATURE, useSwaggerFeature);
88+
}
89+
90+
writeOptional(outputFolder, new SupportingFile("pom.mustache", "", "pom.xml"));
91+
writeOptional(outputFolder, new SupportingFile("gradle.mustache", "", "build.gradle"));
92+
writeOptional(outputFolder, new SupportingFile("settingsGradle.mustache", "", "settings.gradle"));
93+
writeOptional(outputFolder, new SupportingFile("README.mustache", "", "README.md"));
94+
writeOptional(outputFolder, new SupportingFile("web.mustache", ("src/main/webapp/WEB-INF"), "web.xml"));
95+
96+
supportingFiles.add(new SupportingFile("JacksonConfig.mustache", (projectFolder + File.separator + "java" + '/' + invokerPackage).replace(".", "/"), "JacksonConfig.java"));
97+
98+
if (generateJbossDeploymentDescriptor) {
99+
writeOptional(outputFolder, new SupportingFile("jboss-web.mustache", ("src/main/webapp/WEB-INF"), "jboss-web.xml"));
100+
}
101+
102+
writeOptional(outputFolder, new SupportingFile("RestApplication.mustache", (projectFolder + File.separator + "java" + '/' + invokerPackage).replace(".", "/"), "RestApplication.java"));
103+
104+
}
105+
106+
@Override
107+
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
108+
String basePath = resourcePath;
109+
if (basePath.startsWith("/")) {
110+
basePath = basePath.substring(1);
111+
}
112+
int pos = basePath.indexOf("/");
113+
if (pos > 0) {
114+
basePath = basePath.substring(0, pos);
115+
}
116+
117+
if (basePath == "") {
118+
basePath = "default";
119+
} else {
120+
if (co.path.startsWith("/" + basePath)) {
121+
co.path = co.path.substring(("/" + basePath).length());
122+
}
123+
co.subresourceOperation = !co.path.isEmpty();
124+
}
125+
List<CodegenOperation> opList = operations.get(basePath);
126+
if (opList == null) {
127+
opList = new ArrayList<CodegenOperation>();
128+
operations.put(basePath, opList);
129+
}
130+
opList.add(co);
131+
co.baseName = basePath;
132+
}
133+
134+
@Override
135+
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
136+
137+
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
138+
if (operations != null) {
139+
List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation");
140+
for (CodegenOperation operation : ops) {
141+
if (operation.hasConsumes == Boolean.TRUE) {
142+
Map<String, String> firstType = operation.consumes.get(0);
143+
if (firstType != null) {
144+
if ("multipart/form-data".equals(firstType.get("mediaType"))) {
145+
operation.isMultipart = Boolean.TRUE;
146+
}
147+
}
148+
}
149+
List<CodegenResponse> responses = operation.responses;
150+
if (responses != null) {
151+
for (CodegenResponse resp : responses) {
152+
if ("0".equals(resp.code)) {
153+
resp.code = "200";
154+
}
155+
}
156+
}
157+
if (operation.returnType == null) {
158+
operation.returnType = "Void";
159+
} else if (operation.returnType.startsWith("List")) {
160+
String rt = operation.returnType;
161+
int end = rt.lastIndexOf(">");
162+
if (end > 0) {
163+
operation.returnType = rt.substring("List<".length(), end).trim();
164+
operation.returnContainer = "List";
165+
}
166+
} else if (operation.returnType.startsWith("Map")) {
167+
String rt = operation.returnType;
168+
int end = rt.lastIndexOf(">");
169+
if (end > 0) {
170+
operation.returnType = rt.substring("Map<".length(), end).split(",")[1].trim();
171+
operation.returnContainer = "Map";
172+
}
173+
} else if (operation.returnType.startsWith("Set")) {
174+
String rt = operation.returnType;
175+
int end = rt.lastIndexOf(">");
176+
if (end > 0) {
177+
operation.returnType = rt.substring("Set<".length(), end).trim();
178+
operation.returnContainer = "Set";
179+
}
180+
}
181+
}
182+
}
183+
return objs;
184+
}
185+
186+
@Override
187+
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
188+
// Add imports for Jackson
189+
if (!BooleanUtils.toBoolean(model.isEnum)) {
190+
model.imports.add("JsonProperty");
191+
192+
if (BooleanUtils.toBoolean(model.hasEnums)) {
193+
model.imports.add("JsonValue");
194+
}
195+
}
196+
}
197+
198+
@Override
199+
public Map<String, Object> postProcessModelsEnum(Map<String, Object> objs) {
200+
objs = super.postProcessModelsEnum(objs);
201+
202+
// Add imports for Jackson
203+
List<Map<String, String>> imports = (List<Map<String, String>>) objs.get("imports");
204+
List<Object> models = (List<Object>) objs.get("models");
205+
for (Object _mo : models) {
206+
Map<String, Object> mo = (Map<String, Object>) _mo;
207+
CodegenModel cm = (CodegenModel) mo.get("model");
208+
// for enum model
209+
if (Boolean.TRUE.equals(cm.isEnum) && cm.allowableValues != null) {
210+
cm.imports.add(importMapping.get("JsonValue"));
211+
Map<String, String> item = new HashMap<String, String>();
212+
item.put("import", importMapping.get("JsonValue"));
213+
imports.add(item);
214+
}
215+
}
216+
217+
return objs;
218+
}
219+
220+
public void setUseBeanValidation(boolean useBeanValidation) {
221+
this.useBeanValidation = useBeanValidation;
222+
}
223+
224+
public void setGenerateJbossDeploymentDescriptor(boolean generateJbossDeploymentDescriptor) {
225+
this.generateJbossDeploymentDescriptor = generateJbossDeploymentDescriptor;
226+
}
227+
228+
public void setUseSwaggerFeature(boolean useSwaggerFeature) {
229+
this.useSwaggerFeature = useSwaggerFeature;
230+
}
231+
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/features/BeanValidationFeatures.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public interface BeanValidationFeatures {
44

55
// Language supports generating BeanValidation-Annotations
66
public static final String USE_BEANVALIDATION = "useBeanValidation";
7-
7+
88
public void setUseBeanValidation(boolean useBeanValidation);
99

1010
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/features/CXFServerFeatures.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
*
66
*/
77
public interface CXFServerFeatures
8-
extends CXFFeatures, SwaggerFeatures, SpringFeatures, JbossFeature, BeanValidationExtendedFeatures,
9-
SwaggerUIFeatures
8+
extends CXFFeatures, SwaggerFeatures, SpringFeatures, JbossFeature, BeanValidationExtendedFeatures, SwaggerUIFeatures
109
{
1110

1211
public static final String USE_WADL_FEATURE = "useWadlFeature";

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/features/PerformBeanValidationFeatures.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public interface PerformBeanValidationFeatures {
44

55
// Language supports performing BeanValidation
66
public static final String PERFORM_BEANVALIDATION = "performBeanValidation";
7-
7+
88
public void setPerformBeanValidation(boolean performBeanValidation);
99

1010
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/features/SpringFeatures.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ public interface SpringFeatures extends BeanValidationFeatures {
1414

1515
public void setUseSpringAnnotationConfig(boolean useSpringAnnotationConfig);
1616

17-
17+
1818
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/features/SwaggerFeatures.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.swagger.codegen.languages.features;
22

3-
public interface SwaggerFeatures extends CXFFeatures {
3+
public interface SwaggerFeatures {
44

55
public static final String USE_SWAGGER_FEATURE = "useSwaggerFeature";
66

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.swagger.api;
2+
3+
import javax.ws.rs.Produces;
4+
import javax.ws.rs.core.MediaType;
5+
import javax.ws.rs.ext.ContextResolver;
6+
import javax.ws.rs.ext.Provider;
7+
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
11+
import com.fasterxml.jackson.databind.ObjectMapper;
12+
import com.fasterxml.jackson.datatype.joda.JodaModule;
13+
14+
@Provider
15+
@Produces(MediaType.APPLICATION_JSON)
16+
public class JacksonConfig implements ContextResolver<ObjectMapper> {
17+
18+
private static final Logger LOG = LoggerFactory.getLogger(JacksonConfig.class);
19+
20+
private ObjectMapper objectMapper;
21+
22+
public JacksonConfig() throws Exception {
23+
this.objectMapper = new ObjectMapper();
24+
25+
{{#java8}}
26+
this.objectMapper.registerModule(new JavaTimeModule());
27+
{{/java8}}
28+
{{#joda}}
29+
this.objectMapper.registerModule(new JodaModule());
30+
{{/joda}}
31+
32+
// sample to convert any DateTime to readable timestamps
33+
//this.objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
34+
}
35+
36+
public ObjectMapper getContext(Class<?> objectType) {
37+
return objectMapper;
38+
}
39+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Swagger generated server
2+
3+
## Overview
4+
This server was generated by the [swagger-codegen](https://github.com/swagger-api/swagger-codegen) project. By using the
5+
[OpenAPI-Spec](https://github.com/swagger-api/swagger-core/wiki) from a remote server, you can easily generate a server stub. This
6+
is an example of building a swagger-enabled JAX-RS server.
7+
8+
This example uses the [JAX-RS](https://jax-rs-spec.java.net/) framework for Jboss Resteasy.
9+
10+
You can deploy the WAR file to Jboss EAP or any other JEE server supporting Jboss Resteasy.
11+
12+
You can then view the swagger listing here:
13+
14+
```
15+
http://localhost:{{serverPort}}{{contextPath}}/swagger.json
16+
```
17+
18+
Note that if you have configured the `host` to be something other than localhost, the calls through
19+
swagger-ui will be directed to that host and not localhost!

0 commit comments

Comments
 (0)