Skip to content
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

Fix/#355 swagger example exception #356

Merged
merged 2 commits into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -369,17 +370,16 @@ public void generate(final String targetPath, Object model, Any config) throws I
// handler
for (Map<String, Object> op : operationList) {
String className = op.get("handlerName").toString();
String example = null;
@SuppressWarnings("unchecked")
List<Map> parameters = (List<Map>)op.get("parameters");
Object responseExample = op.get("responseExample");
if (responseExample != null) {
example = (String)responseExample;
}
Map<String, String> responseExample = (Map<String, String>)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
Expand Down Expand Up @@ -749,7 +749,8 @@ public List<Map<String, Object>> 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<String, String> responseExample = populateResponseExample(operation);
flattened.put("responseExample", responseExample);
if (enableParamDescription) {
//get parameters info and put into result
List<Parameter> parameterRawList = operation.getParameters();
Expand Down Expand Up @@ -864,19 +865,21 @@ private String populateRequestBodyExample(Operation operation) {
return result;
}

private String populateResponseExample(Operation operation) {
String result = null;
private Map<String, String> populateResponseExample(Operation operation) {
Map<String, String> result = new HashMap<>();
Object example;
for (String statusCode : operation.getResponses().keySet()) {
Optional<Response> 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> 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<String, Example> exampleMap = mediaType.get().getExamples();
Expand All @@ -885,7 +888,8 @@ private String populateResponseExample(Operation operation) {
Map.Entry<String, Example> 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()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@import java.util.Map
@import java.util.List
@option discardLogicWhitespace=true
@args (String handlerPackage, String className, String example, List<Map> parameters)
@args (String handlerPackage, String className, String statusCode, String example, List<Map> parameters)
package @handlerPackage;

import com.networknt.handler.LightHttpHandler;
Expand All @@ -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();
}
Expand Down