diff --git a/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java b/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java index 319490584..7d8e0ca14 100644 --- a/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java +++ b/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java @@ -10,6 +10,7 @@ import com.networknt.oas.OpenApiParser; import com.networknt.oas.model.*; import com.networknt.oas.model.impl.OpenApi3Impl; +import com.networknt.utility.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -369,17 +370,16 @@ public void generate(final String targetPath, Object model, Any config) throws I // handler for (Map op : operationList) { String className = op.get("handlerName").toString(); - String example = null; @SuppressWarnings("unchecked") List parameters = (List)op.get("parameters"); - Object responseExample = op.get("responseExample"); - if (responseExample != null) { - example = (String)responseExample; - } + Map responseExample = (Map)op.get("responseExample"); + String example = responseExample.get("example"); + String statusCode = responseExample.get("statusCode"); + statusCode = StringUtils.isBlank(statusCode) || statusCode.equals("default") ? "-1" : statusCode; 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, parameters)); + transfer(targetPath, ("src.main.java." + handlerPackage).replace(".", separator), className + ".java", templates.rest.handler.template(handlerPackage, className, statusCode, example, parameters)); } // handler test cases @@ -749,7 +749,8 @@ public List> getOperationList(Object model) { .collect(Collectors.toMap(k -> k.getName(), v -> UrlGenerator.generateValidParam(v))); flattened.put("headerNameValueMap", headerNameValueMap); flattened.put("requestBodyExample", populateRequestBodyExample(operation)); - flattened.put("responseExample", populateResponseExample(operation)); + Map responseExample = populateResponseExample(operation); + flattened.put("responseExample", responseExample); if (enableParamDescription) { //get parameters info and put into result List parameterRawList = operation.getParameters(); @@ -864,19 +865,21 @@ private String populateRequestBodyExample(Operation operation) { return result; } - private String populateResponseExample(Operation operation) { - String result = null; + private Map populateResponseExample(Operation operation) { + Map result = new HashMap<>(); Object example; for (String statusCode : operation.getResponses().keySet()) { Optional response = Optional.ofNullable(operation.getResponse(String.valueOf(statusCode))); if (response.get().getContentMediaTypes().size() == 0) { - result = statusCode + ",{}"; + result.put("statusCode", statusCode); + result.put("example", "{}"); } for (String mediaTypeStr : response.get().getContentMediaTypes().keySet()) { Optional mediaType = Optional.ofNullable(response.get().getContentMediaType(mediaTypeStr)); example = mediaType.get().getExample(); if (example != null) { - result = statusCode + "," + JsonStream.serialize(example); + result.put("statusCode", statusCode); + result.put("example", JsonStream.serialize(example)); } else { // check if there are multiple examples Map exampleMap = mediaType.get().getExamples(); @@ -885,7 +888,8 @@ private String populateResponseExample(Operation operation) { Map.Entry entry = exampleMap.entrySet().iterator().next(); Example e = entry.getValue(); if (e != null) { - result = statusCode + "," + JsonStream.serialize(e.getValue()); + result.put("statusCode", statusCode); + result.put("example", JsonStream.serialize(e.getValue())); } } } diff --git a/light-rest-4j/src/main/java/com/networknt/codegen/rest/SwaggerGenerator.java b/light-rest-4j/src/main/java/com/networknt/codegen/rest/SwaggerGenerator.java index ec34795c1..0b7a47cf7 100644 --- a/light-rest-4j/src/main/java/com/networknt/codegen/rest/SwaggerGenerator.java +++ b/light-rest-4j/src/main/java/com/networknt/codegen/rest/SwaggerGenerator.java @@ -79,9 +79,9 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep boolean overwriteHandlerTest = config.toBoolean("overwriteHandlerTest"); boolean overwriteModel = config.toBoolean("overwriteModel"); boolean enableHttp = config.toBoolean("enableHttp"); - String httpPort = config.toString("httpPort"); + String httpPort = config.toString("httpPort").trim(); boolean enableHttps = config.toBoolean("enableHttps"); - String httpsPort = config.toString("httpsPort"); + String httpsPort = config.toString("httpsPort").trim(); boolean enableRegistry = config.toBoolean("enableRegistry"); boolean eclipseIDE = config.toBoolean("eclipseIDE"); boolean supportClient = config.toBoolean("supportClient"); @@ -289,7 +289,7 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep parameterList.add(parameterResultMap); } } - transfer(targetPath, ("src.main.java." + handlerPackage).replace(".", separator), className + ".java", templates.rest.handler.template(handlerPackage, className, example, parameterList)); + transfer(targetPath, ("src.main.java." + handlerPackage).replace(".", separator), className + ".java", templates.rest.handler.template(handlerPackage, className, "200", example, parameterList)); } // handler test cases diff --git a/light-rest-4j/src/main/resources/templates/rest/handler.rocker.raw b/light-rest-4j/src/main/resources/templates/rest/handler.rocker.raw index 92ae3373f..24b09025c 100644 --- a/light-rest-4j/src/main/resources/templates/rest/handler.rocker.raw +++ b/light-rest-4j/src/main/resources/templates/rest/handler.rocker.raw @@ -2,7 +2,7 @@ @import java.util.Map @import java.util.List @option discardLogicWhitespace=true -@args (String handlerPackage, String className, String example, List parameters) +@args (String handlerPackage, String className, String statusCode, String example, List parameters) package @handlerPackage; import com.networknt.handler.LightHttpHandler; @@ -22,8 +22,8 @@ public class @className implements LightHttpHandler { public void handleRequest(HttpServerExchange exchange) throws Exception { @if(example != null) { exchange.getResponseHeaders().add(new HttpString("Content-Type"), "application/json"); - @with (String statusCode = ((String)example).split(",", 2)[0]) {@if (!statusCode.equals("default")) {exchange.setStatusCode(@statusCode);} else {exchange.setStatusCode(-1);}} - @with (e = StringEscapeUtils.escapeJson(((String)example).split(",", 2)[1])) {exchange.getResponseSender().send("@e");} + exchange.setStatusCode(@statusCode); + @with (e = StringEscapeUtils.escapeJson(example)) {exchange.getResponseSender().send("@e");} } else { exchange.endExchange(); }