Browse Source

Merge branch 'dev' into v0.7.x

# Conflicts:
#	src/main/java/org/ssssssss/magicapi/config/MagicConfiguration.java
#	src/main/java/org/ssssssss/magicapi/controller/MagicWorkbenchController.java
mxd 4 years ago
parent
commit
7525feec2f

+ 2 - 2
README.md

@@ -47,8 +47,8 @@ magic-api 是一个基于Java的接口快速开发框架,编写接口将通过
 <!-- 以spring-boot-starter的方式引用 -->
 <dependency>
 	<groupId>org.ssssssss</groupId>
-	<artifactId>magic-api-spring-boot-starter</artifactId>
-    <version>0.6.0</version>
+    <artifactId>magic-api-spring-boot-starter</artifactId>
+    <version>0.6.1</version>
 </dependency>
 ```
 ## 修改application.properties

+ 2 - 2
pom.xml

@@ -11,7 +11,7 @@
     </parent>
     <groupId>org.ssssssss</groupId>
     <artifactId>magic-api</artifactId>
-    <version>0.6.0</version>
+    <version>0.6.1</version>
     <packaging>jar</packaging>
     <name>magic-api</name>
     <description>auto generate http api</description>
@@ -38,7 +38,7 @@
         <dependency>
             <groupId>org.ssssssss</groupId>
             <artifactId>magic-script</artifactId>
-            <version>1.3.7</version>
+            <version>1.3.8</version>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>

+ 13 - 2
src/main/java/org/ssssssss/magicapi/config/MagicConfiguration.java

@@ -33,6 +33,11 @@ public class MagicConfiguration {
 	 */
 	private String password;
 
+	/**
+	 * 编辑器配置文件
+	 */
+	private String editorConfig;
+
 	private final String tokenKey = "Magic-Token";
 
 	/**
@@ -79,8 +84,6 @@ public class MagicConfiguration {
 
 	private boolean enableWeb = false;
 
-	private SyncConfig syncConfig;
-
 	public void setEnableWeb(boolean enableWeb) {
 		this.enableWeb = enableWeb;
 	}
@@ -205,6 +208,14 @@ public class MagicConfiguration {
 		this.syncConfig = syncConfig;
 	}
 
+	public String getEditorConfig() {
+		return editorConfig;
+	}
+
+	public void setEditorConfig(String editorConfig) {
+		this.editorConfig = editorConfig;
+	}
+
 	/**
 	 * 打印banner
 	 */

+ 22 - 3
src/main/java/org/ssssssss/magicapi/controller/MagicWorkbenchController.java

@@ -1,9 +1,9 @@
 package org.ssssssss.magicapi.controller;
 
-import org.apache.commons.lang3.StringUtils;
-import org.springframework.core.ParameterizedTypeReference;
-import org.springframework.http.HttpEntity;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.http.HttpHeaders;
+import org.springframework.util.ResourceUtils;
 import org.springframework.http.HttpMethod;
 import org.springframework.http.MediaType;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -23,13 +23,18 @@ import org.ssssssss.magicapi.utils.MD5Utils;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import java.io.File;
 import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.util.*;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
 public class MagicWorkbenchController extends MagicController {
 
+	private static Logger logger = LoggerFactory.getLogger(MagicWorkbenchController.class);
+
 	private RestTemplate restTemplate = new RestTemplate();
 
 	private ApiServiceProvider agiApiService;
@@ -133,4 +138,18 @@ public class MagicWorkbenchController extends MagicController {
 		}).getBody();
 	}
 
+
+	@RequestMapping(value = "/config-js", produces = "application/javascript")
+	@ResponseBody
+	public Object configjs() {
+		if (configuration.getEditorConfig() != null) {
+			try {
+				File file = ResourceUtils.getFile(configuration.getEditorConfig());
+				return Files.readAllBytes(Paths.get(file.toURI()));
+			} catch (IOException e) {
+				logger.warn("读取编辑器配置文件{}失败", configuration.getEditorConfig());
+			}
+		}
+		return "var MAGIC_EDITOR_CONFIG = {}";
+	}
 }

+ 11 - 3
src/main/java/org/ssssssss/magicapi/controller/RequestHandler.java

@@ -88,14 +88,14 @@ public class RequestHandler extends MagicController {
 		}
 		if (requestedFromTest) {
 			if (isRequestedFromContinue(request)) {
-				return invokeContinueRequest(request, response);
+				return invokeContinueRequest(info, request, response);
 			}
 			return invokeTestRequest(info, (MagicScriptDebugContext) context, request, response);
 		}
 		return invokeRequest(info, context, request, response);
 	}
 
-	private Object invokeContinueRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
+	private Object invokeContinueRequest(ApiInfo info, HttpServletRequest request, HttpServletResponse response) throws Exception {
 		String sessionId = getRequestedSessionId(request);
 		MagicScriptDebugContext context = MagicScriptDebugContext.getDebugContext(sessionId);
 		if (context == null) {
@@ -115,6 +115,14 @@ public class RequestHandler extends MagicController {
 		} else if (context.isException()) {
 			return resolveThrowable((Throwable) context.getReturnValue());
 		}
+		Object value = context.getReturnValue();
+		// 执行后置拦截器
+		if ((value = doPostHandle(info, context, value, request, response)) != null) {
+			// 修正前端显示,当拦截器返回时,原样输出显示
+			response.setHeader(HEADER_RESPONSE_WITH_MAGIC_API, "false");
+			// 后置拦截器不包裹
+			return value;
+		}
 		return convertResult(context.getReturnValue(), response);
 	}
 
@@ -287,7 +295,7 @@ public class RequestHandler extends MagicController {
 	private Object readRequestBody(HttpServletRequest request) throws IOException {
 		if (configuration.getHttpMessageConverters() != null && request.getContentType() != null) {
 			MediaType mediaType = MediaType.valueOf(request.getContentType());
-			Class clazz = Map.class;
+			Class clazz = Object.class;
 			try {
 				for (HttpMessageConverter<?> converter : configuration.getHttpMessageConverters()) {
 					if (converter.canRead(clazz, mediaType)) {

+ 1 - 1
src/main/java/org/ssssssss/magicapi/modules/MongoCollectionExtension.java

@@ -63,7 +63,7 @@ public class MongoCollectionExtension {
 
 	@Comment("查询数量")
 	public static long count(MongoCollection<Document> collection, @Comment("查询") Map<String, Object> query) {
-		return collection.count(new Document(query));
+		return collection.countDocuments(new Document(query));
 	}
 
 	@Comment("批量删除,返回删除条数")

+ 46 - 8
src/main/java/org/ssssssss/magicapi/modules/MongoModule.java

@@ -1,9 +1,17 @@
 package org.ssssssss.magicapi.modules;
 
-import com.mongodb.MongoClient;
 import com.mongodb.client.MongoCollection;
+import com.mongodb.client.MongoDatabase;
+import org.apache.commons.lang3.StringUtils;
+import org.bson.Document;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.data.mongodb.core.MongoTemplate;
 import org.ssssssss.magicapi.config.MagicModule;
+import org.ssssssss.script.reflection.AbstractReflection;
+import org.ssssssss.script.reflection.JavaInvoker;
 
+import java.lang.reflect.Method;
 import java.util.HashMap;
 
 /**
@@ -11,18 +19,48 @@ import java.util.HashMap;
  */
 public class MongoModule extends HashMap<String, Object> implements MagicModule {
 
-	private MongoClient mongoClient;
+	private static Logger logger = LoggerFactory.getLogger(MongoModule.class);
 
-	public MongoModule(MongoClient mongoClient) {
-		this.mongoClient = mongoClient;
+	private final MongoTemplate mongoTemplate;
+
+	private JavaInvoker<Method> invoker;
+
+	private JavaInvoker<Method> mongoDbFactoryInvoker;
+
+	public MongoModule(MongoTemplate mongoTemplate) {
+		this.mongoTemplate = mongoTemplate;
+		AbstractReflection reflection = AbstractReflection.getInstance();
+		mongoDbFactoryInvoker = reflection.getMethod(this.mongoTemplate, "getMongoDbFactory");
+		if (mongoDbFactoryInvoker != null) {
+			try {
+				Object factory = mongoDbFactoryInvoker.invoke0(this.mongoTemplate, null);
+				invoker = reflection.getMethod(factory, "getDb", StringUtils.EMPTY);
+				if (invoker == null) {
+					invoker = reflection.getMethod(factory, "getMongoDatabase", StringUtils.EMPTY);
+				}
+			} catch (Throwable e) {
+				logger.error("mongo模块初始化失败", e);
+			}
+		} else {
+			logger.error("mongo模块初始化失败");
+		}
 	}
 
 	@Override
-	public Object get(Object database) {
-		return database == null ? null : new HashMap<String, MongoCollection>(){
+	public Object get(Object databaseName) {
+		return databaseName == null ? null : new HashMap<String, MongoCollection<Document>>() {
 			@Override
-			public MongoCollection get(Object collection) {
-				return collection == null ? null : mongoClient.getDatabase(database.toString()).getCollection(collection.toString());
+			public MongoCollection<Document> get(Object collection) {
+				if (collection == null) {
+					return null;
+				}
+				try {
+					Object factory = mongoDbFactoryInvoker.invoke0(mongoTemplate, null);
+					MongoDatabase database = (MongoDatabase) invoker.invoke0(factory, null, databaseName.toString());
+					return database.getCollection(collection.toString());
+				} catch (Throwable throwable) {
+					throw new RuntimeException(throwable);
+				}
 			}
 		};
 	}

+ 12 - 1
src/main/java/org/ssssssss/magicapi/provider/impl/DefaultMagicAPIService.java

@@ -49,11 +49,22 @@ public class DefaultMagicAPIService implements MagicAPIService {
 	}
 
 	private Object execute(ApiInfo info, Map<String, Object> context) {
+
+		// 获取原上下文
+		final MagicScriptContext magicScriptContext = MagicScriptContext.get();
+
 		MagicScriptContext scriptContext = new MagicScriptContext();
 		scriptContext.putMapIntoContext(context);
 		SimpleScriptContext simpleScriptContext = new SimpleScriptContext();
 		simpleScriptContext.setAttribute(MagicScript.CONTEXT_ROOT, scriptContext, ScriptContext.ENGINE_SCOPE);
-		return ScriptManager.compile("MagicScript", info.getScript()).eval(simpleScriptContext);
+		final Object evalVal;
+		try {
+			evalVal = ScriptManager.compile("MagicScript", info.getScript()).eval(simpleScriptContext);
+		} finally {
+			// 恢复原接口上下文,修复当前调完其它接口后原接口上下文丢失的问题
+			MagicScriptContext.set(magicScriptContext);
+		}
+		return evalVal;
 	}
 
 	@Override

+ 24 - 13
src/main/java/org/ssssssss/magicapi/swagger/SwaggerEntity.java

@@ -274,30 +274,26 @@ public class SwaggerEntity {
 
 		private String type;
 
-		private Map<String, Object> schema;
+		private Object schema;
 
 		private String description;
 
+		private Object example;
+
 		public Parameter(String name, String in, String type, String description, Object example) {
 			this.name = name;
 			this.in = in;
-			this.schema = new HashMap<>();
-			this.schema.put("type", type);
-			this.schema.put("example", example);
 			this.description = description;
 			if ("body".equalsIgnoreCase(in)) {
+				Map<String, Object> schema = new HashMap<>();
+				schema.put("type", type);
+				schema.put("example", example);
 				this.schema = doProcessSchema(example);
+			} else {
+				this.example = example;
 			}
 		}
 
-		public String getDescription() {
-			return description;
-		}
-
-		public void setDescription(String description) {
-			this.description = description;
-		}
-
 		public String getName() {
 			return name;
 		}
@@ -334,10 +330,25 @@ public class SwaggerEntity {
 			return schema;
 		}
 
-		public void setSchema(Map<String, Object> schema) {
+		public void setSchema(Object schema) {
 			this.schema = schema;
 		}
 
+		public String getDescription() {
+			return description;
+		}
+
+		public void setDescription(String description) {
+			this.description = description;
+		}
+
+		public Object getExample() {
+			return example;
+		}
+
+		public void setExample(Object example) {
+			this.example = example;
+		}
 	}
 
 	public static class Tag {