refactor: jsonSchema转换

This commit is contained in:
chenjianxing 2021-05-26 17:11:33 +08:00 committed by jianxing
parent add73293a7
commit 0b8b329c19
5 changed files with 69 additions and 27 deletions

View File

@ -16,6 +16,7 @@ import io.metersphere.api.service.ApiModuleService;
import io.metersphere.base.domain.ApiDefinitionWithBLOBs;
import io.metersphere.base.domain.ApiModule;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.json.JSONSchemaGenerator;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.XMLUtils;
@ -716,17 +717,26 @@ public class Swagger3Parser extends SwaggerAbstractParser {
String bodyType = body.getString("type");
if(bodyType == null) {
}else if(bodyType.equals("JSON")) {
try{ // 若请求体是一个 object
bodyInfo = buildRequestBodyJsonInfo(body.getJSONArray("raw"));
} catch (Exception e) {
try { // 若请求体是一个 array
bodyInfo = buildRequestBodyJsonInfo(body.getJSONObject("raw"));
} catch (Exception e1) { // 若请求体 json 不合法则忽略错误原样字符串导出/导入
bodyInfo = new JSONObject();
((JSONObject) bodyInfo).put("type", "string");
((JSONObject) bodyInfo).put("example", body.get("raw").toString());
} else if(bodyType.equals("JSON")) {
try {
if (StringUtils.equals(body.getString("format"), "JSON-SCHEMA")) {
String jsonSchema = JSONSchemaGenerator.getJson(body.getString("jsonSchema"));
try {
bodyInfo = buildRequestBodyJsonInfo(JSONObject.parseObject(jsonSchema));
} catch (Exception e) {
bodyInfo = buildRequestBodyJsonInfo(JSONObject.parseArray(jsonSchema));
}
} else {
try{ // 若请求体是一个 object
bodyInfo = buildRequestBodyJsonInfo(body.getJSONArray("raw"));
} catch (Exception e) {
bodyInfo = buildRequestBodyJsonInfo(body.getJSONObject("raw"));
}
}
} catch (Exception e1) { // 若请求体 json 不合法则忽略错误原样字符串导出/导入
bodyInfo = new JSONObject();
((JSONObject) bodyInfo).put("type", "string");
((JSONObject) bodyInfo).put("example", body.get("raw").toString());
}
} else if(bodyType.equals("XML")) {
String xmlText = body.getString("raw");

View File

@ -225,22 +225,22 @@ public class JSONSchemaGenerator {
if (object.has("items")) {
if (itemsObject.has("enum")) {
array.add(analyzeEnumProperty(object));
array.add(analyzeEnumProperty(itemsObject));
} else if (itemsObject.has("type") && itemsObject.get("type").getAsString().equals("string")) {
if (object.has("default")) {
array.add(object.get("default"));
} else if (object.has("mock") && object.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString())) {
String value = ScriptEngineUtils.calculate(object.get("mock").getAsJsonObject().get("mock").toString());
array.add(object.get(value));
if (itemsObject.has("default")) {
array.add(itemsObject.get("default"));
} else if (itemsObject.has("mock") && itemsObject.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString())) {
String value = ScriptEngineUtils.calculate(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString());
array.add(value);
} else {
array.add(null);
}
} else if (itemsObject.has("type") && itemsObject.get("type").getAsString().equals("number")) {
if (object.has("default")) {
array.add(object.get("default"));
} else if (object.has("mock") && object.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString())) {
String value = ScriptEngineUtils.calculate(object.get("mock").getAsJsonObject().get("mock").toString());
array.add(object.get(value));
if (itemsObject.has("default")) {
array.add(itemsObject.get("default"));
} else if (itemsObject.has("mock") && itemsObject.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString())) {
String value = ScriptEngineUtils.calculate(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString());
array.add(value);
} else {
array.add(0);
}

View File

@ -38,7 +38,7 @@
<el-switch active-text="JSON-SCHEMA" v-model="body.format" active-value="JSON-SCHEMA"/>
</div>
<ms-json-code-edit v-if="body.format==='JSON-SCHEMA'" :body="body" ref="jsonCodeEdit"/>
<ms-code-edit v-else :read-only="isReadOnly" height="400px" :data.sync="body.raw" :modes="modes" :mode="'json'" ref="codeEdit"/>
<ms-code-edit v-else-if="codeEditActive" :read-only="isReadOnly" height="400px" :data.sync="body.raw" :modes="modes" :mode="'json'" ref="codeEdit"/>
</div>
<div class="ms-body" v-if="body.type == 'XML'">
@ -70,6 +70,8 @@
import MsApiBinaryVariable from "./ApiBinaryVariable";
import MsApiFromUrlVariable from "./ApiFromUrlVariable";
import BatchAddParameter from "../basis/BatchAddParameter";
import Convert from "@/business/components/common/json-schema/convert/convert";
export default {
name: "MsApiBody",
@ -100,9 +102,29 @@
type: BODY_TYPE,
modes: ['text', 'json', 'xml', 'html'],
jsonSchema: "JSON",
codeEditActive: true
};
},
watch: {
'body.format'() {
const MsConvert = new Convert();
if (this.body.format === 'JSON-SCHEMA') {
this.body.jsonSchema = MsConvert.format(JSON.parse(this.body.raw));
} else {
MsConvert.schemaToJsonStr(this.body.jsonSchema, (result) => {
this.$set(this.body, 'raw', result);
this.reloadCodeEdit();
});
}
}
},
methods: {
reloadCodeEdit() {
this.codeEditActive = false;
this.$nextTick(() => {
this.codeEditActive = true;
});
},
modeChange(mode) {
switch (this.body.type) {
case "JSON":

View File

@ -19,7 +19,6 @@
</template>
<script>
import {schemaToJson} from './common';
import MsImportJson from './import/ImportJson';
const Convert = require('./convert/convert.js');
@ -66,12 +65,10 @@
methods: {
handleClick() {
if (this.activeName === 'preview') {
//
//this.preview = schemaToJson(this.schema.root);
this.loading = true;
//
this.$post('/api/definition/preview', this.schema.root, response => {
this.preview = response.data;
MsConvert.schemaToJsonStr(this.schema.root, (result) => {
this.preview = result;
this.loading = false;
});
}

View File

@ -5,6 +5,7 @@ const isNull = require("lodash.isnull");
const isNumber = require("lodash.isnumber");
const isObject = require("lodash.isobject");
const isString = require("lodash.isstring");
const {post} = require("@/common/js/ajax");
const isArray = Array.isArray;
@ -175,6 +176,18 @@ class Convert {
return objectTemplate;
}
/**
* 后台转换
* @param callback
*/
schemaToJsonStr(schema, callback) {
post('/api/definition/preview', schema, (response) => {
if (callback) {
callback(response.data);
}
});
}
}
module.exports = Convert;