From c79212f2253df62c1b9403cefa85b68f87e8a862 Mon Sep 17 00:00:00 2001 From: AgAngle <1323481023@qq.com> Date: Mon, 15 Apr 2024 16:13:01 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E8=84=9A=E6=9C=AC=E6=B7=BB=E5=8A=A0=E5=A4=9A=E6=9D=A1=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=8F=98=E9=87=8F=E6=97=B6=EF=BC=8C=E5=8F=AA=E7=94=9F?= =?UTF-8?q?=E6=95=88=E4=BA=86=E4=B8=80=E6=9D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/service/ApiEnvironmentService.java | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiEnvironmentService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiEnvironmentService.java index 74c9529d09..fc845f59ed 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiEnvironmentService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiEnvironmentService.java @@ -111,6 +111,7 @@ public class ApiEnvironmentService { JsonNode jsonObj = elements.next(); if (jsonObj.has(NAME) && jsonObj.has(VALUE) && jsonObj.get(NAME).asText().equals(entry.getKey())) { + contains = true; if (jsonObj.get(VALUE).asText().equals(entry.getValue())) { break; } @@ -118,17 +119,31 @@ public class ApiEnvironmentService { envNeedUpdate = true; break; } else { - contains = true; envNeedUpdate = true; } } - if (contains) { + if (!contains) { + // 不包含则添加 ObjectNode itemObj = objectMapper.createObjectNode(); itemObj.put(NAME, entry.getKey()); itemObj.put(VALUE, entry.getValue()); itemObj.put(ENABLE, true); - ((ArrayNode) variables).set(length - 1, itemObj); + ArrayNode arrayNode = ((ArrayNode) variables); + JsonNode lastNode = arrayNode.get(arrayNode.size() - 1); + boolean isBlankLine = isBlankLine(lastNode); + if (isBlankLine) { + // 如果最后一行是空行,则替换最后一行 + ((ArrayNode) variables).set(length - 1, itemObj); + } else { + ((ArrayNode) variables).add(itemObj); + } + // 新增一个空行 + ObjectNode blankItem = objectMapper.createObjectNode(); + blankItem.put(NAME, StringUtils.EMPTY); + blankItem.put(VALUE, StringUtils.EMPTY); + blankItem.put(ENABLE, true); + ((ArrayNode) variables).add(blankItem); ((ObjectNode) configObj).set(VARIABLES, variables); } } @@ -142,6 +157,15 @@ public class ApiEnvironmentService { } } + private boolean isBlankLine(JsonNode lastNode) { + boolean nameBlank = lastNode.get(NAME) == null ? true : StringUtils.isBlank(lastNode.get(NAME).asText()); + boolean valueBlank = lastNode.get(VALUE) == null ? true : StringUtils.isBlank(lastNode.get(VALUE).asText()); + if (nameBlank && valueBlank) { + return true; + } + return false; + } + private List createArray(Map varMap) { return varMap.entrySet().stream() .map(entry -> objectMapper.createObjectNode()