fix(接口测试): 解决接口测试swagger3导入丢失非引用属性的值的问题增加json类修改

This commit is contained in:
guoyuqi 2022-10-12 17:22:48 +08:00 committed by xiaomeinvG
parent e1137fe4a7
commit 4f1b446038
5 changed files with 24 additions and 11 deletions

View File

@ -4,6 +4,7 @@ import io.metersphere.commons.constants.PropertyConstant;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.math.BigDecimal;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -24,6 +25,8 @@ public class JsonSchemaItem {
private String pattern; private String pattern;
private Integer maxLength; private Integer maxLength;
private Integer minLength; private Integer minLength;
private BigDecimal minimum;
private BigDecimal maximum;
private String schema; private String schema;
public JsonSchemaItem() { public JsonSchemaItem() {

View File

@ -475,9 +475,6 @@ public class Swagger3Parser extends SwaggerAbstractParser {
private JsonSchemaItem parseSchema(Schema schema, Set<String> refSet) { private JsonSchemaItem parseSchema(Schema schema, Set<String> refSet) {
if (schema == null) return null; if (schema == null) return null;
if (StringUtils.isBlank(schema.get$ref()) && schema.getProperties() == null && refSet.isEmpty() && schema.getExample() == null) {
return null;
}
JsonSchemaItem item = new JsonSchemaItem(); JsonSchemaItem item = new JsonSchemaItem();
if (schema.getRequired() != null) { if (schema.getRequired() != null) {
item.setRequired(schema.getRequired()); item.setRequired(schema.getRequired());
@ -521,7 +518,8 @@ public class Swagger3Parser extends SwaggerAbstractParser {
item.setPattern(schema.getPattern()); item.setPattern(schema.getPattern());
item.setMaxLength(schema.getMaxLength()); item.setMaxLength(schema.getMaxLength());
item.setMinLength(schema.getMinLength()); item.setMinLength(schema.getMinLength());
item.setMaximum(schema.getMaximum());
item.setMinimum(schema.getMinimum());
return item; return item;
} }

View File

@ -1,10 +1,13 @@
package io.metersphere.commons.utils; package io.metersphere.commons.utils;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.type.CollectionType; import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.TypeFactory; import com.fasterxml.jackson.databind.type.TypeFactory;
@ -19,6 +22,10 @@ public class JSON {
static { static {
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 自动检测所有类的全部属性
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
// 如果一个对象中没有任何的属性那么在序列化的时候就会报错
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
} }
public static String toJSONString(Object value) { public static String toJSONString(Object value) {

View File

@ -84,7 +84,8 @@ public class TestEnvironmentController {
private void checkParams(TestEnvironmentDTO apiTestEnvironment) { private void checkParams(TestEnvironmentDTO apiTestEnvironment) {
try { try {
JSONObject json = new JSONObject(JSON.parseObject(apiTestEnvironment.getConfig())); Map<Object,Object> map = JSON.parseObject(apiTestEnvironment.getConfig(),Map.class);
JSONObject json = new JSONObject(map);
JSONObject commonConfig = json.getJSONObject("commonConfig"); JSONObject commonConfig = json.getJSONObject("commonConfig");
JSONArray databaseConfigs = json.getJSONArray("databaseConfigs"); JSONArray databaseConfigs = json.getJSONArray("databaseConfigs");

View File

@ -349,13 +349,17 @@ public class BaseEnvironmentService extends NodeTreeService<ApiModuleDTO> {
List<BodyFile> files = new ArrayList<>(); List<BodyFile> files = new ArrayList<>();
if (StringUtils.isNotEmpty(config)) { if (StringUtils.isNotEmpty(config)) {
Map<String, Object> map = JSON.parseObject(config, Map.class); Map<String, Object> map = JSON.parseObject(config, Map.class);
JSONObject commonConfig = new JSONObject(map).getJSONObject("commonConfig"); JSONObject commonConfig = new JSONObject(map).optJSONObject("commonConfig");
JSONArray variables = commonConfig.getJSONArray("variables"); if (commonConfig!=null){
JSONArray variables = commonConfig.optJSONArray("variables");
if (variables!=null){
List<ScenarioVariable> list = JSON.parseArray(variables.toString(), ScenarioVariable.class); List<ScenarioVariable> list = JSON.parseArray(variables.toString(), ScenarioVariable.class);
list.stream().filter(ScenarioVariable::isCSVValid).forEach(keyValue -> { list.stream().filter(ScenarioVariable::isCSVValid).forEach(keyValue -> {
files.addAll(keyValue.getFiles().stream().filter(BodyFile::isRef).collect(Collectors.toList())); files.addAll(keyValue.getFiles().stream().filter(BodyFile::isRef).collect(Collectors.toList()));
}); });
} }
}
}
if (!org.springframework.util.CollectionUtils.isEmpty(files)) { if (!org.springframework.util.CollectionUtils.isEmpty(files)) {
List<BodyFile> list = files.stream().distinct().collect(Collectors.toList()); List<BodyFile> list = files.stream().distinct().collect(Collectors.toList());
fileAssociationService.save(list, type, id); fileAssociationService.save(list, type, id);