h = new HashMap<>();
+ h.put("required", res.isRequired() ? "1" : "0");
+ h.put("value", res.getValue());
+ h.put("name", res.getName());
+ h.put("desc", res.getDesc());
+ headers.add(j, h);
});
- method.put("req_headers",headers);
+ method.put("req_headers", headers);
Template apiTemplate = BeetlTemplateUtil.getByName(YAPI_RESULT_TPL);
apiTemplate.binding(TemplateVariable.RESPONSELIST.getVariable(), generateJson(apiMethodDoc.getResponseParams()));
- String json = apiTemplate.render();
- method.put("res_body",json);
+ String json = apiTemplate.render();
+ method.put("res_body", json);
- if(StringUtil.isNotEmpty(apiMethodDoc.getResponseUsage())){
- method.put("desc","\n"+apiMethodDoc.getResponseUsage()+"\n
\n");
+ if (StringUtil.isNotEmpty(apiMethodDoc.getResponseUsage())) {
+ method.put("desc", "\n" + apiMethodDoc.getResponseUsage() + "\n
\n");
}
- methods.add(idx,method);
+ methods.add(idx, method);
});
- module.put("list",methods);
+ module.put("list", methods);
requestItem.add(module);
});
@@ -183,26 +185,27 @@ public class YapiJsonBuilder {
private static String generateJson(List responseParams) {
StringBuffer re = new StringBuffer("\"type\":\"object\",\n\"properties\":{\n");
- HashSet required=new HashSet<>();
+ HashSet required = new HashSet<>();
responseParams.stream().forEach(apiParam -> {
re.append(getTypeAndPropertiesJson(apiParam));
- if(apiParam.isRequired()){
+ if (apiParam.isRequired()) {
required.add(apiParam.getField());
}
- } );
+ });
Gson gs = new Gson();
- re.append("\"required\":\""+gs.toJson(required.toArray())+"\"");
+ re.append("\"required\":\"" + gs.toJson(required.toArray()) + "\"");
re.append("\t}");
return re.toString();
}
/**
* 将字段类型转换为yapi的字段类型
- * @param type
- * @return
+ *
+ * @param type java type
+ * @return String
*/
public static String changeType(String type) {
- switch (type){
+ switch (type) {
case "boolean":
return "boolean";
case "int32":
@@ -216,25 +219,26 @@ public class YapiJsonBuilder {
/**
* 单个参数拼接字符串
- * @param param
- * @return
+ *
+ * @param param ApiParam
+ * @return String
*/
- public static String getTypeAndPropertiesJson(ApiParam param){
- StringBuffer resultJson = new StringBuffer( );
+ public static String getTypeAndPropertiesJson(ApiParam param) {
+ StringBuffer resultJson = new StringBuffer();
- resultJson.append("\""+param.getField()+"\":{");
- resultJson.append("\"type\":\""+changeType(param.getType())+"\", ");
+ resultJson.append("\"" + param.getField() + "\":{");
+ resultJson.append("\"type\":\"" + changeType(param.getType()) + "\", ");
- if(param.getChildren()!=null&¶m.getChildren().size()>0){
- if(param.getType().equals("object")){
+ if (param.getChildren() != null && param.getChildren().size() > 0) {
+ if (param.getType().equals("object")) {
resultJson.append(" \"properties\":{");
- param.getChildren().forEach(child->{
+ param.getChildren().forEach(child -> {
resultJson.append(getTypeAndPropertiesJson(child));
});
- }else if(param.getType().equals("array")){
+ } else if (param.getType().equals("array")) {
resultJson.append(" \"items\":{");
- resultJson .append("\"type\":\"object\",\n\"properties\":{\n");
- param.getChildren().forEach(child->{
+ resultJson.append("\"type\":\"object\",\n\"properties\":{\n");
+ param.getChildren().forEach(child -> {
resultJson.append(getTypeAndPropertiesJson(child));
});
resultJson.append("\t},");