Quellcode durchsuchen

初步支持swagger2

mxd vor 5 Jahren
Ursprung
Commit
c10ac5f5c1

+ 5 - 1
src/main/java/org/ssssssss/magicapi/config/MappingHandlerMapping.java

@@ -106,6 +106,10 @@ public class MappingHandlerMapping {
 		this.magicApiService = magicApiService;
 	}
 
+	public List<ApiInfo> getApiInfos() {
+		return apiInfos;
+	}
+
 	/**
 	 * 注册请求
 	 */
@@ -193,7 +197,7 @@ public class MappingHandlerMapping {
 	 *
 	 * @param path 请求路径
 	 */
-	private String getRequestPath(String groupPrefix, String path) {
+	public String getRequestPath(String groupPrefix, String path) {
 		groupPrefix = groupPrefix == null ? "" : groupPrefix;
 		while (groupPrefix.endsWith("/")) {
 			groupPrefix = groupPrefix.substring(0, groupPrefix.length() - 1);

+ 23 - 0
src/main/java/org/ssssssss/magicapi/functions/ResponseFunctions.java

@@ -0,0 +1,23 @@
+package org.ssssssss.magicapi.functions;
+
+import org.ssssssss.magicapi.provider.ResultProvider;
+
+import java.util.List;
+
+public class ResponseFunctions {
+
+	private ResultProvider resultProvider;
+
+	public ResponseFunctions(ResultProvider resultProvider) {
+		this.resultProvider = resultProvider;
+	}
+
+	/**
+	 * 自行构建分页结果
+	 * @param total	条数
+	 * @param values	数据内容
+	 */
+	public Object page(long total, List<Object> values){
+		return resultProvider.buildPageResult(total,values);
+	}
+}

+ 1 - 0
src/main/java/org/ssssssss/magicapi/provider/impl/DefaultApiServiceProvider.java

@@ -20,6 +20,7 @@ public class DefaultApiServiceProvider implements ApiServiceProvider {
 
 	private final String SCRIPT_COLUMNS = "api_script script,\n" +
 			"api_parameter parameter,\n" +
+			"api_output output,\n" +
 			"api_option option_value\n";
 
 	private RowMapper<ApiInfo> rowMapper = new BeanPropertyRowMapper<>(ApiInfo.class);

+ 356 - 0
src/main/java/org/ssssssss/magicapi/swagger/SwaggerEntity.java

@@ -0,0 +1,356 @@
+package org.ssssssss.magicapi.swagger;
+
+import java.util.*;
+
+public class SwaggerEntity {
+
+	private String swagger = "2.0";
+
+	private String host;
+
+	private String basePath;
+
+	private Info info;
+
+	private Set<Tag> tags = new HashSet<>();
+
+	private Map<String,String> definitions = Collections.emptyMap();
+
+	private Map<String, Map<String, Path>> paths = new HashMap<>();
+
+	public Info getInfo() {
+		return info;
+	}
+
+	public void setInfo(Info info) {
+		this.info = info;
+	}
+
+	public void addPath(String path, String method, Path pathInfo) {
+		Map<String, Path> map = paths.get(path);
+		if(map == null){
+			map = new HashMap<>();
+			paths.put(path, map);
+		}
+		map.put(method.toLowerCase(), pathInfo);
+	}
+
+	public void addTag(String name, String description) {
+		this.tags.add(new Tag(name, description));
+	}
+
+	public String getHost() {
+		return host;
+	}
+
+	public void setHost(String host) {
+		this.host = host;
+	}
+
+	public String getSwagger() {
+		return swagger;
+	}
+
+	public void setSwagger(String swagger) {
+		this.swagger = swagger;
+	}
+
+	public String getBasePath() {
+		return basePath;
+	}
+
+	public void setBasePath(String basePath) {
+		this.basePath = basePath;
+	}
+
+	public Map<String,String> getDefinitions() {
+		return definitions;
+	}
+
+	public Set<Tag> getTags() {
+		return tags;
+	}
+
+	public Map<String, Map<String, Path>> getPaths() {
+		return paths;
+	}
+
+
+	public static class Info{
+
+		private String description;
+
+		private String version;
+
+		private String title;
+
+		private License license;
+
+		public Info(String description, String version, String title, License license) {
+			this.description = description;
+			this.version = version;
+			this.title = title;
+			this.license = license;
+		}
+
+		public String getDescription() {
+			return description;
+		}
+
+		public void setDescription(String description) {
+			this.description = description;
+		}
+
+		public String getVersion() {
+			return version;
+		}
+
+		public void setVersion(String version) {
+			this.version = version;
+		}
+
+		public String getTitle() {
+			return title;
+		}
+
+		public void setTitle(String title) {
+			this.title = title;
+		}
+
+		public License getLicense() {
+			return license;
+		}
+
+		public void setLicense(License license) {
+			this.license = license;
+		}
+	}
+
+	public static class Path {
+
+		private List<String> tags = new ArrayList<>();
+
+		private String summary;
+
+		private List<String> produces = new ArrayList<>();
+
+		private List<String> consumes = new ArrayList<>();
+
+		private List<Parameter> parameters = new ArrayList<>();
+
+		private Map<String, Object> responses = new HashMap<>();
+
+		public void addProduce(String produce) {
+			this.produces.add(produce);
+		}
+
+		public void addConsume(String consume) {
+			this.consumes.add(consume);
+		}
+
+		public void addParameter(Parameter parameter) {
+			this.parameters.add(parameter);
+		}
+
+		public void addResponse(String status, Object object) {
+			Map<String, Object> response = new HashMap<>();
+			response.put("description","OK");
+			response.put("schema",new HashMap<>());
+			response.put("examples",object);
+			this.responses.put(status, response);
+		}
+
+		public List<String> getTags() {
+			return tags;
+		}
+
+		public void addTag(String tag){
+			this.tags.add(tag);
+		}
+
+		public void setTags(List<String> tags) {
+			this.tags = tags;
+		}
+
+		public String getSummary() {
+			return summary;
+		}
+
+		public void setSummary(String summary) {
+			this.summary = summary;
+		}
+
+		public List<String> getProduces() {
+			return produces;
+		}
+
+		public void setProduces(List<String> produces) {
+			this.produces = produces;
+		}
+
+		public List<String> getConsumes() {
+			return consumes;
+		}
+
+		public void setConsumes(List<String> consumes) {
+			this.consumes = consumes;
+		}
+
+		public List<Parameter> getParameters() {
+			return parameters;
+		}
+
+		public void setParameters(List<Parameter> parameters) {
+			this.parameters = parameters;
+		}
+
+		public Map<String, Object> getResponses() {
+			return responses;
+		}
+
+		public void setResponses(Map<String, Object> responses) {
+			this.responses = responses;
+		}
+	}
+
+	public static class Parameter {
+
+		private String name;
+
+		private String in;
+
+		private boolean required = false;
+
+		private String type;
+
+		private Map<String,Object> schema;
+
+		private Object examples;
+
+		public Parameter(String name, String in, String type, Object examples) {
+			this.name = name;
+			this.in = in;
+			this.schema = new HashMap<>();
+			this.schema.put("type",type);
+			this.examples = examples;
+		}
+
+		public String getName() {
+			return name;
+		}
+
+		public void setName(String name) {
+			this.name = name;
+		}
+
+		public String getIn() {
+			return in;
+		}
+
+		public void setIn(String in) {
+			this.in = in;
+		}
+
+		public boolean isRequired() {
+			return required;
+		}
+
+		public void setRequired(boolean required) {
+			this.required = required;
+		}
+
+		public String getType() {
+			return type;
+		}
+
+		public void setType(String type) {
+			this.type = type;
+		}
+
+		public Object getSchema() {
+			return schema;
+		}
+
+		public void setSchema(Map<String, Object> schema) {
+			this.schema = schema;
+		}
+
+		public Object getExamples() {
+			return examples;
+		}
+
+		public void setExamples(Object examples) {
+			this.examples = examples;
+		}
+	}
+
+	public static class Tag {
+
+		private String name;
+
+		private String description;
+
+		public Tag(String name, String description) {
+			this.name = name;
+			this.description = description;
+		}
+
+		public String getName() {
+			return name;
+		}
+
+		public void setName(String name) {
+			this.name = name;
+		}
+
+		public String getDescription() {
+			return description;
+		}
+
+		public void setDescription(String description) {
+			this.description = description;
+		}
+
+		@Override
+		public boolean equals(Object o) {
+			if (this == o) return true;
+			if (o == null || getClass() != o.getClass()) return false;
+			Tag tag = (Tag) o;
+			return Objects.equals(name, tag.name);
+		}
+
+		@Override
+		public int hashCode() {
+			return Objects.hash(name);
+		}
+	}
+
+
+	public static class License {
+
+		private String name;
+
+		private String url;
+
+		public License(String name, String url) {
+			this.name = name;
+			this.url = url;
+		}
+
+		public String getName() {
+			return name;
+		}
+
+		public void setName(String name) {
+			this.name = name;
+		}
+
+		public String getUrl() {
+			return url;
+		}
+
+		public void setUrl(String url) {
+			this.url = url;
+		}
+	}
+}

+ 69 - 0
src/main/java/org/ssssssss/magicapi/swagger/SwaggerProvider.java

@@ -0,0 +1,69 @@
+package org.ssssssss.magicapi.swagger;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.ssssssss.magicapi.config.ApiInfo;
+import org.ssssssss.magicapi.config.MappingHandlerMapping;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+
+public class SwaggerProvider {
+
+	private MappingHandlerMapping mappingHandlerMapping;
+
+	public void setMappingHandlerMapping(MappingHandlerMapping mappingHandlerMapping) {
+		this.mappingHandlerMapping = mappingHandlerMapping;
+	}
+
+	@ResponseBody
+	public SwaggerEntity swaggerJson(){
+		List<ApiInfo> infos = mappingHandlerMapping.getApiInfos();
+		SwaggerEntity swaggerEntity = new SwaggerEntity();
+		SwaggerEntity.License license = new SwaggerEntity.License("MIT", "https://gitee.com/ssssssss-team/magic-api/blob/master/LICENSE");
+		swaggerEntity.setInfo(new SwaggerEntity.Info("MagicAPI 接口信息","0.2.2","MagicAPI Swagger Docs",license));
+		ObjectMapper mapper = new ObjectMapper();
+		for (ApiInfo info : infos) {
+			swaggerEntity.addTag(info.getGroupName(),info.getGroupPrefix());
+			SwaggerEntity.Path path = new SwaggerEntity.Path();
+			path.addTag(info.getGroupName());
+			try {
+				path.addResponse("200",mapper.readValue(Objects.toString(info.getOutput(),"{}"),Object.class));
+			} catch (IOException ignored) {
+			}
+			path.addConsume("*/*");
+			path.addProduce("application/json");
+			path.setSummary(info.getName());
+			try {
+				Map map = mapper.readValue(Objects.toString(info.getParameter(),"{}"),Map.class);
+				Object request = map.get("request");
+				if(request instanceof Map){
+					Map requestMap = (Map) request;
+					Set keys = requestMap.keySet();
+					for (Object key : keys) {
+						path.addParameter(new SwaggerEntity.Parameter(key.toString(),"query","string", requestMap.getOrDefault(key,"")));
+					}
+				}
+				Object header = map.get("header");
+				if(header instanceof Map){
+					Map headers = (Map) header;
+					Set keys = headers.keySet();
+					for (Object key : keys) {
+						path.addParameter(new SwaggerEntity.Parameter(key.toString(),"header","string", headers.getOrDefault(key,"")));
+					}
+				}
+				if(map.containsKey("body")){
+					path.addParameter(new SwaggerEntity.Parameter("body","body",null,map.get("body")));
+				}
+			} catch (IOException ignored) {
+			}
+			swaggerEntity.addPath(mappingHandlerMapping.getRequestPath(info.getGroupPrefix(),info.getPath()),info.getMethod(),path);
+		}
+		return swaggerEntity;
+	}
+
+
+}

+ 6 - 2
src/main/resources/magicapi-support/js/index.js

@@ -46,6 +46,7 @@ var MagicEditor = {
         $('input[name=name]').val('');
         $('input[name=path]').val('');
         $('input[name=prefix]').val('');
+        this.outputJson = null;
         this.apiId = null;
         this.scriptEditor&&this.scriptEditor.setValue('return message;');
         this.requestEditor && this.requestEditor.setValue(this.defaultRequestValue);
@@ -626,7 +627,8 @@ var MagicEditor = {
                 groupPrefix : groupPrefix,
                 parameter: this.requestEditor.getValue(),
                 option: this.optionsEditor.getValue(),
-                name : name
+                name : name,
+                output : this.outputJson
             },
             async : false,
             exception : function(){
@@ -704,7 +706,9 @@ var MagicEditor = {
         $(".button-run").removeClass('disabled');
         $('.button-continue').addClass('disabled');
         this.navigateTo(2)
-        this.resultEditor.setValue(this.formatJson(json.data))
+        var outputJson = this.formatJson(json.data);
+        this.outputJson = outputJson;
+        this.resultEditor.setValue(outputJson);
         return ret;
     },
     debugIn : function(id,data){