fix (接口测试): 修复生成枚举数据错误问题

--bug=1007472 --user=赵勇 【接口测试】接口定义,自动生成的用例错误 https://www.tapd.cn/55049933/s/1058985
This commit is contained in:
fit2-zhao 2021-10-26 11:12:48 +08:00 committed by fit2-zhao
parent 2a3e89d97c
commit c296dd79de
2 changed files with 45 additions and 109 deletions

View File

@ -31,34 +31,17 @@ public class JSONSchemaGenerator {
if (rootElement.has("type") || rootElement.has("allOf")) {
analyzeObject(rootElement, rootObj);
}
if (rootElement.has("definitions")) {
// Section 9 in json-validation
analyzeDefinitions(rootElement);
}
}
private static void analyzeObject(JsonObject object, JSONObject rootObj) {
// Creating the concept
if (object.has("title")) {
// 暂不处理后续使用时再加
String title = object.get("title").getAsString();
}
if (object.has("description")) {
// 暂不处理后续使用时再加
String description = object.get("description").getAsString();
}
if (object.has("allOf")) {
JsonArray allOfArray = object.get("allOf").getAsJsonArray();
for (JsonElement allOfElement : allOfArray) {
JsonObject allOfElementObj = allOfElement.getAsJsonObject();
if (allOfElementObj.has("$ref")) {
// 暂不处理后续使用时再加
String ref = allOfElementObj.get("$ref").getAsString();
} else if (allOfElementObj.has("properties")) {
if (allOfElementObj.has("properties")) {
// Properties elements will become the attributes/references of the element
JsonObject propertiesObj = allOfElementObj.get("properties").getAsJsonObject();
for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) {
@ -68,8 +51,6 @@ public class JSONSchemaGenerator {
}
}
}
} else if (object.has("oneOf")) {
// 暂不处理后续使用时再加
} else if (object.has("properties")) {
JsonObject propertiesObj = object.get("properties").getAsJsonObject();
for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) {
@ -82,10 +63,6 @@ public class JSONSchemaGenerator {
} else if (object.has("type") && !object.get("type").getAsString().equals("object")) {
analyzeProperty(rootObj, object.getAsString(), object);
}
if (object.has("required")) {
// 必选项暂不处理后续使用时再加
}
}
private static void analyzeProperty(JSONObject concept, String propertyName, JsonObject object) {
@ -102,11 +79,23 @@ public class JSONSchemaGenerator {
}
}
}
if(object.has("default")){
if (object.has("default")) {
concept.put(propertyName, object.get("default"));
}else if (object.has("enum")) {
concept.put(propertyName, analyzeEnumProperty(object));
} else if (object.has("enum")) {
try {
if (object.has("mock") && object.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString())) {
Object value = object.get("mock").getAsJsonObject().get("mock");
concept.put(propertyName, value);
} else {
List<Object> list = analyzeEnumProperty(object);
if (list.size() > 0) {
int index = (int) (Math.random() * list.size());
concept.put(propertyName, list.get(index));
}
}
} catch (Exception e) {
concept.put(propertyName, "");
}
} else if (propertyObjType.equals("string")) {
// 先设置空值
concept.put(propertyName, "");
@ -123,21 +112,6 @@ public class JSONSchemaGenerator {
String value = ScriptEngineUtils.buildFunctionCallString(object.get("mock").getAsJsonObject().get("mock").getAsString());
concept.put(propertyName, value);
}
if (object.has("maxLength")) {
}
// Section 6.3.1 in json-schema-validation. Resolved as OCL
if (object.has("minLength")) {
}
// Section 6.3.2 in json-schema-validation. Resolved as OCL
if (object.has("pattern")) {
// Section 6.3.3 in json-schema-validation. Resolved as OCL, possible?
// TODO 6.3.3 in json-schema-validation
}
} else if (propertyObjType.equals("integer")) {
// 先设置空值
concept.put(propertyName, 0);
@ -153,30 +127,6 @@ public class JSONSchemaGenerator {
concept.put(propertyName, value);
}
}
if (object.has("multipleOf")) {
}
// Section 6.2.1 in json-schema-validation. Resolved as OCL
if (object.has("maximum")) {
}
// Section 6.2.2 in json-schema-validation. Resolved as OCL
if (object.has("exclusiveMaximum")) {
}
// Section 6.2.3 in json-schema-validation. Resolved as OCL
if (object.has("minimum")) {
}
// Section 6.2.4 in json-schema-validation. Resolved as OCL
if (object.has("exclusiveMinimum")) {
}
// Section 6.2.5 in json-schema-validation. Resolved as OCL
} else if (propertyObjType.equals("number")) {
// 先设置空值
@ -215,13 +165,12 @@ public class JSONSchemaGenerator {
if (object.has("items") && object.get("items").isJsonArray()) {
jsonArray = object.get("items").getAsJsonArray();
} else {
JsonObject itemsObject = itemsObject = object.get("items").getAsJsonObject();
JsonObject itemsObject = object.get("items").getAsJsonObject();
array.add(itemsObject);
}
for(int i = 0; i < jsonArray.size(); i ++){
for (int i = 0; i < jsonArray.size(); i++) {
JsonObject itemsObject = jsonArray.get(i).getAsJsonObject();
if (object.has("items")) {
if (itemsObject.has("mock") && itemsObject.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString())) {
try {
@ -231,8 +180,8 @@ public class JSONSchemaGenerator {
String value = ScriptEngineUtils.buildFunctionCallString(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString());
array.add(value);
}
}else if (itemsObject.has("enum")) {
// array.add(analyzeEnumProperty(itemsObject));
} else if (itemsObject.has("enum")) {
array.add(analyzeEnumProperty(itemsObject));
} else if (itemsObject.has("type") && itemsObject.get("type").getAsString().equals("string")) {
if (itemsObject.has("default")) {
array.add(itemsObject.get("default"));
@ -251,12 +200,6 @@ public class JSONSchemaGenerator {
} else {
array.add(0);
}
} else if (itemsObject.has("oneOf")) {
} else if (itemsObject.has("anyOf")) {
} else if (itemsObject.has("allOf")) {
// TODO
} else if (itemsObject.has("properties")) {
JSONObject propertyConcept = new JSONObject();
JsonObject propertiesObj = itemsObject.get("properties").getAsJsonObject();
@ -267,12 +210,10 @@ public class JSONSchemaGenerator {
}
array.add(propertyConcept);
} else if (itemsObject.has("$ref")) {
analyzeRef(concept, propertyName, itemsObject);
}else if(itemsObject.has("type") && itemsObject.get("type") instanceof JsonPrimitive){
} else if (itemsObject.has("type") && itemsObject.get("type") instanceof JsonPrimitive) {
JSONObject newJsonObj = new JSONObject();
analyzeProperty(newJsonObj,propertyName+"_item",itemsObject);
array.add(newJsonObj.get(propertyName+"_item"));
analyzeProperty(newJsonObj, propertyName + "_item", itemsObject);
array.add(newJsonObj.get(propertyName + "_item"));
}
} else if (object.has("items") && object.get("items").isJsonArray()) {
JsonArray itemsObjectArray = object.get("items").getAsJsonArray();
@ -287,45 +228,30 @@ public class JSONSchemaGenerator {
concept.put(propertyName, obj);
analyzeObject(object, obj);
}
} else if (object.has("$ref")) {
analyzeRef(concept, propertyName, object);
} else if (object.has("oneOf")) {
// Section 6.7.3 in json-schema-validation
} else if (object.has("anyOf")) {
// Section 6.7.2 in json-schema-validation
}
}
private static List<Object> analyzeEnumProperty(JsonObject object) {
List<Object> list = new LinkedList<>();
String jsonStr = null;
JsonArray enumValues = null;
try {
enumValues = object.get("enum").getAsJsonArray();
JsonArray enumValues = object.get("enum").getAsJsonArray();
for (JsonElement enumValueElem : enumValues) {
String enumValue = enumValueElem.getAsString();
list.add(enumValue);
}
}catch (Exception e){
} catch (Exception e) {
jsonStr = object.get("enum").getAsString();
}
if(jsonStr != null && list.isEmpty()){
String [] arrs = jsonStr.split("\n");
for (String str: arrs) {
if (jsonStr != null && list.isEmpty()) {
String[] arrays = jsonStr.split("\n");
for (String str : arrays) {
list.add(str);
}
}
return list;
}
private static void analyzeRef(JSONObject concept, String propertyName, JsonObject object) {
String ref = object.get("$ref").getAsString();
}
private static void analyzeDefinitions(JsonObject object) {
JsonObject definitionsObj = object.get("definitions").getAsJsonObject();
for (Entry<String, JsonElement> entry : definitionsObj.entrySet()) {

View File

@ -2,24 +2,24 @@
<div>
<div v-if="request.protocol === 'HTTP'">
<div v-if="request.url || isCustomizeReq">
<el-select v-model="request.method" style="width: 100px;margin-right: 10px" size="small">
<el-select v-model="request.method" class="ms-select" size="small" :disabled="request.disabled">
<el-option v-for="item in reqOptions" :key="item.id" :label="item.label" :value="item.id"/>
</el-select>
<el-input v-model="request.domain" v-if="request.isRefEnvironment && request.domain" size="small" readonly style="width: 150px;margin-right: 10px"/>
<el-input v-model="request.domain" v-if="request.isRefEnvironment && request.domain" size="small" readonly class="ms-input"/>
<el-input :placeholder="$t('api_test.definition.request.path_all_info')" v-model="request.url"
style="width: 50%" size="small" @blur="urlChange">
style="width: 50%" size="small" @blur="urlChange" :disabled="request.disabled">
</el-input>
<el-checkbox v-if="isCustomizeReq" class="is-ref-environment" v-model="request.isRefEnvironment">
{{ $t('api_test.request.refer_to_environment') }}
</el-checkbox>
</div>
<div v-else>
<el-select v-model="request.method" style="width: 100px;margin-right: 10px" size="small">
<el-select v-model="request.method" class="ms-select" size="small" :disabled="request.disabled">
<el-option v-for="item in reqOptions" :key="item.id" :label="item.label" :value="item.id"/>
</el-select>
<el-input v-model="request.domain" v-if="request.domain" size="small" readonly style="width: 150px;margin-right: 10px"/>
<el-input v-model="request.domain" v-if="request.domain" size="small" readonly class="ms-input" :disabled="request.disabled"/>
<el-input :placeholder="$t('api_test.definition.request.path_all_info')" style="width: 50%"
v-model="request.path" size="small" @blur="pathChange"/>
v-model="request.path" size="small" @blur="pathChange" :disabled="request.disabled"/>
</div>
</div>
@ -143,6 +143,16 @@ export default {
width: 120px;
}
.ms-select {
width: 100px;
margin-right: 10px;
}
.ms-input {
width: 150px;
margin-right: 10px;
}
.is-ref-environment {
margin-left: 15px;
}