refactor: jsonSchema转换
This commit is contained in:
parent
add73293a7
commit
0b8b329c19
|
@ -16,6 +16,7 @@ import io.metersphere.api.service.ApiModuleService;
|
||||||
import io.metersphere.base.domain.ApiDefinitionWithBLOBs;
|
import io.metersphere.base.domain.ApiDefinitionWithBLOBs;
|
||||||
import io.metersphere.base.domain.ApiModule;
|
import io.metersphere.base.domain.ApiModule;
|
||||||
import io.metersphere.commons.exception.MSException;
|
import io.metersphere.commons.exception.MSException;
|
||||||
|
import io.metersphere.commons.json.JSONSchemaGenerator;
|
||||||
import io.metersphere.commons.utils.CommonBeanFactory;
|
import io.metersphere.commons.utils.CommonBeanFactory;
|
||||||
import io.metersphere.commons.utils.LogUtil;
|
import io.metersphere.commons.utils.LogUtil;
|
||||||
import io.metersphere.commons.utils.XMLUtils;
|
import io.metersphere.commons.utils.XMLUtils;
|
||||||
|
@ -717,17 +718,26 @@ public class Swagger3Parser extends SwaggerAbstractParser {
|
||||||
if(bodyType == null) {
|
if(bodyType == null) {
|
||||||
|
|
||||||
} else if(bodyType.equals("JSON")) {
|
} 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
|
try{ // 若请求体是一个 object
|
||||||
bodyInfo = buildRequestBodyJsonInfo(body.getJSONArray("raw"));
|
bodyInfo = buildRequestBodyJsonInfo(body.getJSONArray("raw"));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
try { // 若请求体是一个 array
|
|
||||||
bodyInfo = buildRequestBodyJsonInfo(body.getJSONObject("raw"));
|
bodyInfo = buildRequestBodyJsonInfo(body.getJSONObject("raw"));
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (Exception e1) { // 若请求体 json 不合法,则忽略错误,原样字符串导出/导入
|
} catch (Exception e1) { // 若请求体 json 不合法,则忽略错误,原样字符串导出/导入
|
||||||
bodyInfo = new JSONObject();
|
bodyInfo = new JSONObject();
|
||||||
((JSONObject) bodyInfo).put("type", "string");
|
((JSONObject) bodyInfo).put("type", "string");
|
||||||
((JSONObject) bodyInfo).put("example", body.get("raw").toString());
|
((JSONObject) bodyInfo).put("example", body.get("raw").toString());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else if(bodyType.equals("XML")) {
|
} else if(bodyType.equals("XML")) {
|
||||||
String xmlText = body.getString("raw");
|
String xmlText = body.getString("raw");
|
||||||
JSONObject xmlToJson = XMLUtils.XmlToJson(xmlText);
|
JSONObject xmlToJson = XMLUtils.XmlToJson(xmlText);
|
||||||
|
|
|
@ -225,22 +225,22 @@ public class JSONSchemaGenerator {
|
||||||
|
|
||||||
if (object.has("items")) {
|
if (object.has("items")) {
|
||||||
if (itemsObject.has("enum")) {
|
if (itemsObject.has("enum")) {
|
||||||
array.add(analyzeEnumProperty(object));
|
array.add(analyzeEnumProperty(itemsObject));
|
||||||
} else if (itemsObject.has("type") && itemsObject.get("type").getAsString().equals("string")) {
|
} else if (itemsObject.has("type") && itemsObject.get("type").getAsString().equals("string")) {
|
||||||
if (object.has("default")) {
|
if (itemsObject.has("default")) {
|
||||||
array.add(object.get("default"));
|
array.add(itemsObject.get("default"));
|
||||||
} else if (object.has("mock") && object.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString())) {
|
} else if (itemsObject.has("mock") && itemsObject.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString())) {
|
||||||
String value = ScriptEngineUtils.calculate(object.get("mock").getAsJsonObject().get("mock").toString());
|
String value = ScriptEngineUtils.calculate(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString());
|
||||||
array.add(object.get(value));
|
array.add(value);
|
||||||
} else {
|
} else {
|
||||||
array.add(null);
|
array.add(null);
|
||||||
}
|
}
|
||||||
} else if (itemsObject.has("type") && itemsObject.get("type").getAsString().equals("number")) {
|
} else if (itemsObject.has("type") && itemsObject.get("type").getAsString().equals("number")) {
|
||||||
if (object.has("default")) {
|
if (itemsObject.has("default")) {
|
||||||
array.add(object.get("default"));
|
array.add(itemsObject.get("default"));
|
||||||
} else if (object.has("mock") && object.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString())) {
|
} else if (itemsObject.has("mock") && itemsObject.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString())) {
|
||||||
String value = ScriptEngineUtils.calculate(object.get("mock").getAsJsonObject().get("mock").toString());
|
String value = ScriptEngineUtils.calculate(itemsObject.get("mock").getAsJsonObject().get("mock").getAsString());
|
||||||
array.add(object.get(value));
|
array.add(value);
|
||||||
} else {
|
} else {
|
||||||
array.add(0);
|
array.add(0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,7 +38,7 @@
|
||||||
<el-switch active-text="JSON-SCHEMA" v-model="body.format" active-value="JSON-SCHEMA"/>
|
<el-switch active-text="JSON-SCHEMA" v-model="body.format" active-value="JSON-SCHEMA"/>
|
||||||
</div>
|
</div>
|
||||||
<ms-json-code-edit v-if="body.format==='JSON-SCHEMA'" :body="body" ref="jsonCodeEdit"/>
|
<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>
|
||||||
|
|
||||||
<div class="ms-body" v-if="body.type == 'XML'">
|
<div class="ms-body" v-if="body.type == 'XML'">
|
||||||
|
@ -70,6 +70,8 @@
|
||||||
import MsApiBinaryVariable from "./ApiBinaryVariable";
|
import MsApiBinaryVariable from "./ApiBinaryVariable";
|
||||||
import MsApiFromUrlVariable from "./ApiFromUrlVariable";
|
import MsApiFromUrlVariable from "./ApiFromUrlVariable";
|
||||||
import BatchAddParameter from "../basis/BatchAddParameter";
|
import BatchAddParameter from "../basis/BatchAddParameter";
|
||||||
|
import Convert from "@/business/components/common/json-schema/convert/convert";
|
||||||
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "MsApiBody",
|
name: "MsApiBody",
|
||||||
|
@ -100,9 +102,29 @@
|
||||||
type: BODY_TYPE,
|
type: BODY_TYPE,
|
||||||
modes: ['text', 'json', 'xml', 'html'],
|
modes: ['text', 'json', 'xml', 'html'],
|
||||||
jsonSchema: "JSON",
|
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: {
|
methods: {
|
||||||
|
reloadCodeEdit() {
|
||||||
|
this.codeEditActive = false;
|
||||||
|
this.$nextTick(() => {
|
||||||
|
this.codeEditActive = true;
|
||||||
|
});
|
||||||
|
},
|
||||||
modeChange(mode) {
|
modeChange(mode) {
|
||||||
switch (this.body.type) {
|
switch (this.body.type) {
|
||||||
case "JSON":
|
case "JSON":
|
||||||
|
|
|
@ -19,7 +19,6 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import {schemaToJson} from './common';
|
|
||||||
import MsImportJson from './import/ImportJson';
|
import MsImportJson from './import/ImportJson';
|
||||||
|
|
||||||
const Convert = require('./convert/convert.js');
|
const Convert = require('./convert/convert.js');
|
||||||
|
@ -66,12 +65,10 @@
|
||||||
methods: {
|
methods: {
|
||||||
handleClick() {
|
handleClick() {
|
||||||
if (this.activeName === 'preview') {
|
if (this.activeName === 'preview') {
|
||||||
// 前端转换
|
|
||||||
//this.preview = schemaToJson(this.schema.root);
|
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
// 后台转换
|
// 后台转换
|
||||||
this.$post('/api/definition/preview', this.schema.root, response => {
|
MsConvert.schemaToJsonStr(this.schema.root, (result) => {
|
||||||
this.preview = response.data;
|
this.preview = result;
|
||||||
this.loading = false;
|
this.loading = false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ const isNull = require("lodash.isnull");
|
||||||
const isNumber = require("lodash.isnumber");
|
const isNumber = require("lodash.isnumber");
|
||||||
const isObject = require("lodash.isobject");
|
const isObject = require("lodash.isobject");
|
||||||
const isString = require("lodash.isstring");
|
const isString = require("lodash.isstring");
|
||||||
|
const {post} = require("@/common/js/ajax");
|
||||||
const isArray = Array.isArray;
|
const isArray = Array.isArray;
|
||||||
|
|
||||||
|
|
||||||
|
@ -175,6 +176,18 @@ class Convert {
|
||||||
|
|
||||||
return objectTemplate;
|
return objectTemplate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 后台转换
|
||||||
|
* @param callback
|
||||||
|
*/
|
||||||
|
schemaToJsonStr(schema, callback) {
|
||||||
|
post('/api/definition/preview', schema, (response) => {
|
||||||
|
if (callback) {
|
||||||
|
callback(response.data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Convert;
|
module.exports = Convert;
|
||||||
|
|
Loading…
Reference in New Issue