Skip to content

Commit

Permalink
Add javadoc descriptions for parameters in handlers - closes #188 (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
BalloonWen authored and NicholasAzar committed Dec 11, 2018
1 parent 2b8a3b6 commit a477963
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep
boolean supportClient = config.toBoolean("supportClient");
String dockerOrganization = config.toString("dockerOrganization");
String version = config.toString("version");

if(dockerOrganization == null || dockerOrganization.length() == 0) dockerOrganization = "networknt";

transfer(targetPath, "", "pom.xml", templates.graphql.pom.template(config));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import static java.io.File.separator;

Expand All @@ -42,6 +39,7 @@ public class OpenApiGenerator implements Generator {
boolean skipHealthCheck = false;
boolean skipServerInfo = false;
boolean specChangeCodeReGenOnly = false;
boolean enableParamDescription = true;

public OpenApiGenerator() {
typeMapping.put("array", "java.util.List");
Expand Down Expand Up @@ -95,6 +93,7 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep
skipHealthCheck = config.toBoolean("skipHealthCheck");
skipServerInfo = config.toBoolean("skipServerInfo");
specChangeCodeReGenOnly = config.toBoolean("specChangeCodeReGenOnly");
enableParamDescription = config.toBoolean("enableParamDescription");

String version = config.toString("version");
String serviceId = config.get("groupId") + "." + config.get("artifactId") + "-" + config.get("version");
Expand Down Expand Up @@ -328,14 +327,15 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep
for(Map<String, Object> op : operationList){
String className = op.get("handlerName").toString();
String example = null;
List<Map> parameters = (List) op.get("parameters");
if(op.get("example") != null) {
//example = mapper.writeValueAsString(op.get("example"));
example = JsonStream.serialize(op.get("example"));
}
if(checkExist(targetPath, ("src.main.java." + handlerPackage).replace(".", separator), className + ".java") && !overwriteHandler) {
continue;
}
transfer(targetPath, ("src.main.java." + handlerPackage).replace(".", separator), className + ".java", templates.rest.handler.template(handlerPackage, className, example));
transfer(targetPath, ("src.main.java." + handlerPackage).replace(".", separator), className + ".java", templates.rest.handler.template(handlerPackage, className, example, parameters));
}

// handler test cases
Expand Down Expand Up @@ -415,6 +415,31 @@ public List<Map<String, Object>> getOperationList(Object model) {
flattened.put("normalizedPath", basePath + normalizedPath);
flattened.put("handlerName", Utils.camelize(normalizedPath) + Utils.camelize(entryOps.getKey()) + "Handler");
Operation operation = entryOps.getValue();
if (enableParamDescription) {
//get parameters info and put into result
List<Parameter> parameterRawList = operation.getParameters();
List<Map> parametersResultList = new LinkedList<>();
parameterRawList.forEach(parameter -> {
Map<String, String> parameterMap = new HashMap<>();
parameterMap.put("name", parameter.getName());
parameterMap.put("description", parameter.getDescription());
if(parameter.getRequired() != null) {
parameterMap.put("required", String.valueOf(parameter.getRequired()));
}
Schema schema = parameter.getSchema();
if(schema != null) {
parameterMap.put("type", schema.getType());
if(schema.getMinLength() != null) {
parameterMap.put("minLength", String.valueOf(schema.getMinLength()));
}
if(schema.getMaxLength() != null) {
parameterMap.put("maxLength", String.valueOf(schema.getMaxLength()));
}
}
parametersResultList.add(parameterMap);
});
flattened.put("parameters", parametersResultList);
}
Response response = operation.getResponse("200");
if(response != null) {
MediaType mediaType = response.getContentMediaType("application/json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class SwaggerGenerator implements Generator {
boolean prometheusMetrics =false;
boolean skipHealthCheck = false;
boolean skipServerInfo = false;
boolean enableParamDescription = true;
public SwaggerGenerator() {
typeMapping.put("array", "java.util.List");
typeMapping.put("map", "java.util.Map");
Expand Down Expand Up @@ -90,6 +91,7 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep
skipServerInfo = config.toBoolean("skipServerInfo");
String version = config.toString("version");
String serviceId = config.get("groupId") + "." + config.get("artifactId") + "-" + config.get("version");
enableParamDescription = config.toBoolean("enableParamDescription");

if(dockerOrganization == null || dockerOrganization.length() == 0) dockerOrganization = "networknt";

Expand Down Expand Up @@ -262,7 +264,18 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep
if(checkExist(targetPath, ("src.main.java." + handlerPackage).replace(".", separator), className + ".java") && !overwriteHandler) {
continue;
}
transfer(targetPath, ("src.main.java." + handlerPackage).replace(".", separator), className + ".java", templates.rest.handler.template(handlerPackage, className, example));
Any parametersRaw = op.get("parameters");
List<Map> parameterList = new ArrayList();
if(parametersRaw != null) {
List parameters = parametersRaw.asList();
for(Object parameterRaw : parameters) {
Map parameterMap = ((Any)parameterRaw).asMap();
Map parameterResultMap = new HashMap();
parameterMap.forEach((k, v) -> parameterResultMap.put(k, String.valueOf(v)));
parameterList.add(parameterResultMap);
}
}
transfer(targetPath, ("src.main.java." + handlerPackage).replace(".", separator), className + ".java", templates.rest.handler.template(handlerPackage, className, example, parameterList));
}

// handler test cases
Expand Down Expand Up @@ -335,6 +348,12 @@ public List<Map<String, Any>> getOperationList(Object model) {
}
}
}
if (enableParamDescription) {
Any parametersRaw = values.get("parameters");
if(parametersRaw != null) {
flattened.put("parameters", parametersRaw);
}
}
result.add(flattened);
}
}
Expand Down
17 changes: 13 additions & 4 deletions light-rest-4j/src/main/resources/templates/rest/handler.rocker.raw
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
@import org.apache.commons.text.StringEscapeUtils
@args (String handlerPackage, String className, String example)
@import java.util.Map
@import java.util.List
@option discardLogicWhitespace=true
@args (String handlerPackage, String className, String example, List<Map> parameters)
package @handlerPackage;

import com.networknt.handler.LightHttpHandler;
Expand All @@ -9,13 +12,19 @@ import java.util.HashMap;
import java.util.Map;

public class @className implements LightHttpHandler {
@if(parameters != null && !parameters.isEmpty()) {/**@for (parameter : parameters) {
* @@param @?parameter.get("name") @if(parameter.get("type") != null)
{ @with (String typeStr = ((String)parameter.get("type")).substring(0, 1).toUpperCase()+((String)parameter.get("type")).substring(1))
{ @?typeStr }} @if ( parameter.get("required").equals("true") ) {@@Required } else{@@Optional }@if(parameter.get("minLength") != null){minLength:@parameter.get("minLength");}@if(parameter.get("maxLength") != null) {maxLength:@parameter.get("maxLength");}@if(parameter.get("description") != null ){
* @parameter.get("description")}}
*/}
@@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
@if(example != null) {
exchange.getResponseHeaders().add(new HttpString("Content-Type"), "application/json");
@with (e = StringEscapeUtils.escapeJson(example)) { exchange.getResponseSender().send("@e");}
exchange.getResponseHeaders().add(new HttpString("Content-Type"), "application/json");
@with (e = StringEscapeUtils.escapeJson(example)) {exchange.getResponseSender().send("@e");}
} else {
exchange.endExchange();
exchange.endExchange();
}
}
}

0 comments on commit a477963

Please sign in to comment.