From 55b8f0b943ce7a1c7e6c06479f12a6be837f0c72 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Mon, 4 Mar 2024 17:34:01 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20M?= =?UTF-8?q?ockServer=E5=A2=9E=E5=8A=A0=E5=93=8D=E5=BA=94=E4=BD=93=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E4=B8=BAJsonSchema=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../definition/ApiDefinitionController.java | 3 ++- .../api/dto/request/http/body/JsonBody.java | 10 +++++++ .../jmeter/body/MsJsonBodyConverter.java | 4 +-- .../definition/ApiDefinitionService.java | 26 ------------------- .../service/mockserver/MockServerService.java | 5 ++-- .../api/utils/JsonSchemaBuilder.java | 26 +++++++++++++++++++ 6 files changed, 42 insertions(+), 32 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiDefinitionController.java b/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiDefinitionController.java index d5c3049521..777e48a616 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiDefinitionController.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiDefinitionController.java @@ -11,6 +11,7 @@ import io.metersphere.api.dto.request.ImportRequest; import io.metersphere.api.service.definition.ApiDefinitionLogService; import io.metersphere.api.service.definition.ApiDefinitionNoticeService; import io.metersphere.api.service.definition.ApiDefinitionService; +import io.metersphere.api.utils.JsonSchemaBuilder; import io.metersphere.project.service.FileModuleService; import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.system.dto.OperationHistoryDTO; @@ -271,6 +272,6 @@ public class ApiDefinitionController { @Operation(summary = "接口测试-接口管理-接口-json-schema-预览") @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_READ) public String preview(@RequestBody TextNode jsonSchema) { - return apiDefinitionService.preview(jsonSchema.asText()); + return JsonSchemaBuilder.preview(jsonSchema.asText()); } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/http/body/JsonBody.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/http/body/JsonBody.java index 405b300ae4..8709229dfe 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/http/body/JsonBody.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/http/body/JsonBody.java @@ -1,6 +1,8 @@ package io.metersphere.api.dto.request.http.body; import io.metersphere.api.dto.schema.JsonSchemaItem; +import io.metersphere.api.utils.JsonSchemaBuilder; +import io.metersphere.sdk.util.JSON; import jakarta.validation.Valid; import lombok.Data; @@ -32,4 +34,12 @@ public class JsonBody { * 默认为 false */ private Boolean enableTransition = false; + + //判断是返回jsonValue还是计算JsonSchema + public String getJsonWithSchema() { + if (enableJsonSchema) { + return JsonSchemaBuilder.preview(JSON.toJSONString(jsonSchema)); + } + return jsonValue; + } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/body/MsJsonBodyConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/body/MsJsonBodyConverter.java index 88f1811585..c3ffd7a231 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/body/MsJsonBodyConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/body/MsJsonBodyConverter.java @@ -23,8 +23,8 @@ public class MsJsonBodyConverter extends MsBodyConverter { public void parse(HTTPSamplerProxy sampler, JsonBody body, ParameterConfig config) { sampler.setPostBodyRaw(true); try { - String raw = null; - if (body.getEnableJsonSchema() && body.getEnableTransition()) { + String raw; + if (body.getEnableJsonSchema()) { String jsonString = JsonSchemaBuilder.jsonSchemaToJson(JSON.toJSONString(body.getJsonSchema())); raw = StringEscapeUtils.unescapeJava(jsonString); } else { diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java index f84cb64d62..237914eeab 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java @@ -18,8 +18,6 @@ import io.metersphere.api.parser.ImportParserFactory; import io.metersphere.api.service.ApiCommonService; import io.metersphere.api.service.ApiFileResourceService; import io.metersphere.api.utils.ApiDataUtils; -import io.metersphere.api.utils.JsonSchemaBuilder; -import io.metersphere.jmeter.mock.Mock; import io.metersphere.plugin.api.spi.AbstractMsTestElement; import io.metersphere.project.domain.FileAssociation; import io.metersphere.project.domain.FileMetadata; @@ -59,8 +57,6 @@ import org.springframework.web.multipart.MultipartFile; import java.nio.charset.StandardCharsets; import java.util.*; import java.util.function.Function; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -1126,26 +1122,4 @@ public class ApiDefinitionService { return apiFileResourceService.transfer(request, userId, ApiResourceType.API.name()); } - public String preview(String jsonSchema) { - String jsonString = JsonSchemaBuilder.jsonSchemaToJson(jsonSchema); - //需要匹配到mock函数 然后换成mock数据 - if (StringUtils.isNotBlank(jsonString)) { - String pattern = "@[a-zA-Z\\\\(|,'-\\\\d ]*[a-zA-Z)-9),\\\\\"]"; - Pattern regex = Pattern.compile(pattern); - Matcher matcher = regex.matcher(jsonString); - while (matcher.find()) { - //取出group的最后一个字符 主要是防止 @string|number 和 @string 这种情况 - String group = matcher.group(); - String lastChar = null; - if (group.endsWith(",") || group.endsWith("\"")) { - lastChar = group.substring(group.length() - 1); - group = group.substring(0, group.length() - 1); - } - jsonString = jsonString.replace(matcher.group(), - StringUtils.join(Mock.calculate(group), lastChar)); - } - } - return jsonString; - - } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/mockserver/MockServerService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/mockserver/MockServerService.java index 570c1c8fb7..80430e4bad 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/mockserver/MockServerService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/mockserver/MockServerService.java @@ -75,8 +75,7 @@ public class MockServerService { LogUtils.info("Mock [" + url + "] request:{}", JSON.toJSONString(requestMockParams)); ApiDefinitionMockConfig compareMockConfig = this.match(apiDefinition.getId(), requestHeaderMap, requestMockParams); try { - Object returnObj = this.getReturn(compareMockConfig, apiDefinition.getId(), apiDefinition.getProjectId(), response); - return returnObj; + return this.getReturn(compareMockConfig, apiDefinition.getId(), apiDefinition.getProjectId(), response); } catch (Exception e) { return this.requestNotFound(response); } @@ -158,7 +157,7 @@ public class MockServerService { return StringUtils.EMPTY; } else { if (StringUtils.equalsIgnoreCase(responseBody.getBodyType(), Body.BodyType.JSON.name())) { - return responseBody.getJsonBody().getJsonValue(); + return responseBody.getJsonBody().getJsonWithSchema(); } else if (StringUtils.equalsIgnoreCase(responseBody.getBodyType(), Body.BodyType.XML.name())) { return responseBody.getXmlBody().getValue(); } else if (StringUtils.equalsIgnoreCase(responseBody.getBodyType(), Body.BodyType.RAW.name())) { diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/utils/JsonSchemaBuilder.java b/backend/services/api-test/src/main/java/io/metersphere/api/utils/JsonSchemaBuilder.java index a8c1576a35..1e85d58d51 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/utils/JsonSchemaBuilder.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/utils/JsonSchemaBuilder.java @@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.json.JsonMapper; import com.fasterxml.jackson.databind.node.*; +import io.metersphere.jmeter.mock.Mock; import io.metersphere.project.constants.PropertyConstant; import io.metersphere.sdk.util.LogUtils; import org.apache.commons.collections4.MapUtils; @@ -16,6 +17,8 @@ import org.jetbrains.annotations.NotNull; import java.util.HashMap; import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class JsonSchemaBuilder { @@ -136,4 +139,27 @@ public class JsonSchemaBuilder { processMap.put(key, targetValue); return new TextNode(value); } + + public static String preview(String jsonSchema) { + String jsonString = JsonSchemaBuilder.jsonSchemaToJson(jsonSchema); + //需要匹配到mock函数 然后换成mock数据 + if (StringUtils.isNotBlank(jsonString)) { + String pattern = "@[a-zA-Z\\\\(|,'-\\\\d ]*[a-zA-Z)-9),\\\\\"]"; + Pattern regex = Pattern.compile(pattern); + Matcher matcher = regex.matcher(jsonString); + while (matcher.find()) { + //取出group的最后一个字符 主要是防止 @string|number 和 @string 这种情况 + String group = matcher.group(); + String lastChar = null; + if (group.endsWith(",") || group.endsWith("\"")) { + lastChar = group.substring(group.length() - 1); + group = group.substring(0, group.length() - 1); + } + jsonString = jsonString.replace(matcher.group(), + StringUtils.join(Mock.calculate(group), lastChar)); + } + } + return jsonString; + + } }