fix: 修复mock返回JSON-Schema参数不全的问题、修复JSON-Schema响应参数在文档里不显示的问题

修复mock返回JSON-Schema参数不全的问题、修复JSON-Schema响应参数在文档里不显示的问题
This commit is contained in:
song-tianyang 2021-06-09 17:17:26 +08:00 committed by 刘瑞斌
parent f0b31114a2
commit b0e81fcc26
5 changed files with 124 additions and 31 deletions

View File

@ -28,6 +28,7 @@ public class ApiDocumentInfoDTO {
private String responseHead;
private String responseBody;
private Object jsonSchemaResponseBody;
private String responseBodyParamType;
private String responseBodyFormData;
private String responseBodyStrutureData;

View File

@ -1,5 +1,6 @@
package io.metersphere.api.dto.mockconfig.response;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.Setter;
@ -19,6 +20,8 @@ public class JsonSchemaReturnObj {
private String title;
private String type;
private int maxLength = -1;
private JSONObject properties;
private JSONObject items;
public String getMockValue() {
if (mock == null) {

View File

@ -257,10 +257,32 @@ public class ApiDocumentService {
apiInfoDTO.setResponseBodyParamType(type);
}
if (StringUtils.equalsAny(type, "JSON", "XML", "Raw")) {
if (bodyObj.containsKey("raw")) {
String raw = bodyObj.getString("raw");
apiInfoDTO.setResponseBodyStrutureData(raw);
//判断是否是JsonSchema
boolean isJsonSchema = false;
if (bodyObj.containsKey("format")) {
String foramtValue = String.valueOf(bodyObj.get("format"));
if (StringUtils.equals("JSON-SCHEMA", foramtValue)) {
isJsonSchema = true;
}
}
if (isJsonSchema) {
// apiInfoDTO.setRequestBodyParamType("JSON-SCHEMA");
apiInfoDTO.setResponseBodyParamType("JSON-SCHEMA");
apiInfoDTO.setJsonSchemaResponseBody(bodyObj);
// apiInfoDTO.setJsonSchemaBody(bodyObj);
} else {
if (bodyObj.containsKey("raw")) {
String raw = bodyObj.getString("raw");
apiInfoDTO.setResponseBodyStrutureData(raw);
//转化jsonObje 或者 jsonArray
this.setPreviewData(previewJsonArray, raw);
}
}
// if (bodyObj.containsKey("raw")) {
// String raw = bodyObj.getString("raw");
// apiInfoDTO.setResponseBodyStrutureData(raw);
// }
} else if (StringUtils.equalsAny(type, "Form Data", "WWW_FORM")) {
if (bodyObj.containsKey("kvs")) {
JSONArray bodyParamArr = new JSONArray();

View File

@ -328,24 +328,7 @@ public class MockConfigService {
if (bodyObj.containsKey("jsonSchema") && bodyObj.getJSONObject("jsonSchema").containsKey("properties")) {
String bodyRetunStr = bodyObj.getJSONObject("jsonSchema").getJSONObject("properties").toJSONString();
JSONObject bodyReturnObj = JSONObject.parseObject(bodyRetunStr);
Set<String> keySet = bodyReturnObj.keySet();
JSONObject returnObj = new JSONObject();
for (String key : keySet) {
try {
JsonSchemaReturnObj obj = bodyReturnObj.getObject(key, JsonSchemaReturnObj.class);
String values = obj.getMockValue();
if (StringUtils.isEmpty(values)) {
values = "";
} else {
try {
values = values.startsWith("@") ? ScriptEngineUtils.calculate(values) : values;
} catch (Exception e) {
}
}
returnObj.put(key, values);
} catch (Exception e) {
}
}
JSONObject returnObj = this.parseJsonSchema(bodyReturnObj);
returnStr = returnObj.toJSONString();
}
} else {
@ -422,6 +405,79 @@ public class MockConfigService {
return returnStr;
}
private JSONObject parseJsonSchema(JSONObject bodyReturnObj) {
JSONObject returnObj = new JSONObject();
if(bodyReturnObj == null){
return returnObj;
}
Set<String> keySet = bodyReturnObj.keySet();
for (String key : keySet) {
try {
JsonSchemaReturnObj obj = bodyReturnObj.getObject(key, JsonSchemaReturnObj.class);
if(StringUtils.equals("object",obj.getType())) {
JSONObject itemObj = this.parseJsonSchema(obj.getProperties());
if (!itemObj.isEmpty()) {
returnObj.put(key, itemObj);
}
}else if(StringUtils.equals("array",obj.getType())){
if(obj.getItems() != null){
JSONObject itemObj = obj.getItems();
if(itemObj.containsKey("type")){
if(StringUtils.equals("object",itemObj.getString("type"))&& itemObj.containsKey("properties")){
JSONObject arrayObj = itemObj.getJSONObject("properties");
// Set<String> arrayKeys = arrayObj.keySet();
//
// JSONObject parseObj = new JSONObject();
// for (String arrayKey : arrayKeys) {
// JsonSchemaReturnObj arrayItemObj = arrayObj.getObject(arrayKey, JsonSchemaReturnObj.class);
// String value = this.getMockValues(arrayItemObj.getMockValue());
// parseObj.put(arrayKey,value);
// }
JSONObject parseObj = this.parseJsonSchema(arrayObj);
JSONArray array = new JSONArray();
array.add(parseObj);
returnObj.put(key, array);
}else if(StringUtils.equals("string",itemObj.getString("type"))&& itemObj.containsKey("mock")){
JsonSchemaReturnObj arrayObj = JSONObject.toJavaObject(itemObj,JsonSchemaReturnObj.class);
String value = this.getMockValues(arrayObj.getMockValue());
JSONArray array = new JSONArray();
array.add(value);
returnObj.put(key, array);
}
}
}
}else {
String values = obj.getMockValue();
if (StringUtils.isEmpty(values)) {
values = "";
} else {
try {
values = values.startsWith("@") ? ScriptEngineUtils.calculate(values) : values;
} catch (Exception e) {
}
}
returnObj.put(key, values);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return returnObj;
}
private String getMockValues(String values) {
if (StringUtils.isEmpty(values)) {
values = "";
} else {
try {
values = values.startsWith("@") ? ScriptEngineUtils.calculate(values) : values;
} catch (Exception e) {
}
}
return values;
}
public MockExpectConfigWithBLOBs findMockExpectConfigById(String id) {
return mockExpectConfigMapper.selectByPrimaryKey(id);
}

View File

@ -104,7 +104,7 @@
</div>
<div v-else>
<el-table border :show-header="false"
:data="getJsonArr(apiInfo.requestHead)" row-key="name" class="test-content document-table">
:data="getJsonArr(apiInfo.requestHead)" class="test-content document-table">
<el-table-column prop="name"
:label="$t('api_test.definition.document.table_coloum.name')"
show-overflow-tooltip/>
@ -126,7 +126,7 @@
</div>
<div v-else>
<el-table border
:data="getJsonArr(apiInfo.urlParams)" row-key="name" class="test-content document-table">
:data="getJsonArr(apiInfo.urlParams)" class="test-content document-table">
<el-table-column prop="name"
:label="$t('api_test.definition.document.table_coloum.name')"
min-width="120px"
@ -157,7 +157,7 @@
</div>
<div>
<el-table border v-if="formParamTypes.includes(apiInfo.requestBodyParamType)"
:data="getJsonArr(apiInfo.requestBodyFormData)" row-key="name"
:data="getJsonArr(apiInfo.requestBodyFormData)"
class="test-content document-table">
<el-table-column prop="name"
:label="$t('api_test.definition.document.table_coloum.name')"
@ -224,7 +224,7 @@
<div class="blackFontClass">
{{ $t('api_test.definition.document.response_head') }}:
<el-table border :show-header="false"
:data="getJsonArr(apiInfo.responseHead)" row-key="name" class="test-content document-table">
:data="getJsonArr(apiInfo.responseHead)" class="test-content document-table">
<el-table-column prop="name"
:label="$t('api_test.definition.document.table_coloum.name')"
show-overflow-tooltip/>
@ -244,7 +244,7 @@
</div>
<div>
<el-table border v-if="formParamTypes.includes(apiInfo.responseBodyParamType)"
:data="getJsonArr(apiInfo.responseBodyFormData)" row-key="id"
:data="getJsonArr(apiInfo.responseBodyFormData)"
class="test-content document-table">
<el-table-column prop="name"
:label="$t('api_test.definition.document.table_coloum.name')"
@ -268,6 +268,9 @@
min-width="120px"
show-overflow-tooltip/>
</el-table>
<div v-else-if="apiInfo.responseBodyParamType == 'JSON-SCHEMA'" style="margin-left: 10px">
<ms-json-code-edit :body="apiInfo.jsonSchemaResponseBody" ref="jsonCodeEdit"/>
</div>
<div v-else class="showDataDiv">
<br/>
<p style="margin: 0px 20px;"
@ -282,7 +285,7 @@
<div class="blackFontClass">
{{ $t('api_test.definition.document.response_code') }}:
<el-table border :show-header="false"
:data="getJsonArr(apiInfo.responseCode)" row-key="name" class="test-content document-table">
:data="getJsonArr(apiInfo.responseCode)" class="test-content document-table">
<el-table-column prop="name"
:label="$t('api_test.definition.document.table_coloum.name')"
show-overflow-tooltip/>
@ -358,6 +361,7 @@ export default {
requestBodyStrutureData: "",
sharePopoverVisible:false,
jsonSchemaBody: {},
JsonSchemaResponseBody:{},
responseHead: "无",
responseBody: "",
responseBodyParamType: "无",
@ -427,7 +431,7 @@ export default {
},
trashEnable() {
this.initApiDocSimpleList();
}
},
},
methods: {
formatRowData(dataType, data) {
@ -660,7 +664,10 @@ export default {
handleScroll(){
if(!this.clickStepFlag){
//apiDocInfoDiv(item+20)
let apiDocDivScrollTop = this.$refs.apiDocInfoDiv.scrollTop;
let apiDocDivScrollTop = 0;
if(this.$refs.apiDocInfoDiv&&this.$refs.apiDocInfoDiv.scrollTop){
apiDocDivScrollTop = this.$refs.apiDocInfoDiv.scrollTop;
}
let apiDocDivClientTop = this.$refs.apiDocInfoDiv.clientHeight;
let scrolledHeigh = apiDocDivScrollTop+apiDocDivClientTop;
let lastIndex = 0;
@ -737,7 +744,9 @@ export default {
}else{
this.apiShowArray.shift();
let itemHeight = this.$refs.apiDocInfoDivItem[0].offsetHeight+10;
this.$refs.apiDocInfoDiv.scrollTop = (apiDocDivScrollTop-itemHeight);
if(this.$refs.apiDocInfoDiv&&this.$refs.apiDocInfoDiv.scrollTop){
this.$refs.apiDocInfoDiv.scrollTop = (apiDocDivScrollTop-itemHeight);
}
}
this.apiStepIndex ++;
}
@ -756,7 +765,9 @@ export default {
}
}
this.clickStepFlag = true;
this.$refs.apiDocInfoDiv.scrollTop = (apiDocDivClientTop+itemHeightCount);
if(this.$refs.apiDocInfoDiv&&this.$refs.apiDocInfoDiv.scrollTop){
this.$refs.apiDocInfoDiv.scrollTop = (apiDocDivClientTop+itemHeightCount);
}
},
//api2showArray