feat(接口定义): 完成json编辑器

This commit is contained in:
fit2-zhao 2021-01-04 19:05:31 +08:00
parent 9c4e5bfbdb
commit cd44e90808
22 changed files with 343 additions and 18 deletions

View File

@ -385,6 +385,12 @@
<artifactId>kubernetes-client</artifactId>
<version>4.13.0</version>
</dependency>
<dependency>
<groupId>com.github.fge</groupId>
<artifactId>json-schema-validator</artifactId>
<version>2.2.6</version>
</dependency>
</dependencies>
<build>

View File

@ -11,6 +11,7 @@ import io.metersphere.api.dto.definition.parse.ApiDefinitionImport;
import io.metersphere.api.service.ApiDefinitionService;
import io.metersphere.base.domain.ApiDefinition;
import io.metersphere.commons.constants.RoleConstants;
import io.metersphere.commons.json.JSONSchemaGenerator;
import io.metersphere.commons.utils.PageUtils;
import io.metersphere.commons.utils.Pager;
import io.metersphere.commons.utils.SessionUtils;
@ -159,4 +160,9 @@ public class ApiDefinitionController {
apiDefinitionService.testPlanRelevance(request);
}
@PostMapping("/preview")
public String preview(@RequestBody String jsonSchema) {
return JSONSchemaGenerator.getJson(jsonSchema);
}
}

View File

@ -1,6 +1,7 @@
package io.metersphere.api.dto.scenario;
import io.metersphere.api.dto.scenario.request.BodyFile;
import io.metersphere.commons.json.JSONSchemaGenerator;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
@ -55,6 +56,10 @@ public class Body {
} else {
if (!this.isJson()) {
sampler.setPostBodyRaw(true);
} else {
if (this.getJsonSchema() != null) {
this.raw = JSONSchemaGenerator.getJson(com.alibaba.fastjson.JSON.toJSONString(this.getJsonSchema()));
}
}
KeyValue keyValue = new KeyValue("", this.getRaw());
keyValue.setEnable(true);

View File

@ -0,0 +1,310 @@
package io.metersphere.commons.json;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonschema.cfg.ValidationConfiguration;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.processors.syntax.SyntaxValidator;
import com.google.gson.*;
import io.metersphere.commons.utils.ScriptEngineUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
public class JSONSchemaGenerator {
private static void generator(String json, JSONObject obj) {
analyzeSchema(json, obj);
}
private static void analyzeSchema(String json, JSONObject rootObj) {
// Let's start with the root element of the file
JsonObject rootElement = null;
try {
JsonParser jsonParser = new JsonParser();
JsonElement inputElement = jsonParser.parse(json);
rootElement = inputElement.getAsJsonObject();
} catch (Exception e) {
e.printStackTrace();
}
analyzeRootSchemaElement(rootElement, rootObj);
}
private static void analyzeRootSchemaElement(JsonObject rootElement, JSONObject rootObj) {
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")) {
// Properties elements will become the attributes/references of the element
JsonObject propertiesObj = allOfElementObj.get("properties").getAsJsonObject();
for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) {
String propertyKey = entry.getKey();
JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject();
analyzeProperty(rootObj, propertyKey, propertyObj);
}
}
}
} else if (object.has("oneOf")) {
// 暂不处理后续使用时再加
} else if (object.has("properties")) {
JsonObject propertiesObj = object.get("properties").getAsJsonObject();
for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) {
String propertyKey = entry.getKey();
JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject();
analyzeProperty(rootObj, propertyKey, propertyObj);
}
} 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) {
if (object.has("type")) {
String propertyObjType = null;
if (object.get("type") instanceof JsonPrimitive) {
propertyObjType = object.get("type").getAsString();
} else if (object.get("type") instanceof JsonArray) {
JsonArray typeArray = (JsonArray) object.get("type").getAsJsonArray();
propertyObjType = typeArray.get(0).getAsString();
if (typeArray.size() > 1) {
if (typeArray.get(1).getAsString().equals("null")) {
// 暂不处理后续使用时再加
}
}
}
if (object.has("enum")) {
concept.put(propertyName, analyzeEnumProperty(object));
} else if (propertyObjType.equals("string")) {
// 先设置空值
concept.put(propertyName, null);
if (object.has("format")) {
String propertyFormat = object.get("format").getAsString();
if (propertyFormat.equals("date-time")) {
}
}
if (object.has("default")) {
concept.put(propertyName, object.get("default"));
}
if (object.has("mock") && object.get("mock").getAsJsonObject() != null && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString()) && StringUtils.isNotEmpty(object.get("mock").getAsJsonObject().get("mock").getAsString())) {
String value = ScriptEngineUtils.calculate(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") || propertyObjType.equals("number")) {
// 先设置空值
concept.put(propertyName, 0);
if (object.has("default")) {
concept.put(propertyName, object.get("default"));
}
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());
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("boolean")) {
// 先设置空值
concept.put(propertyName, false);
if (object.has("default")) {
concept.put(propertyName, object.get("default"));
}
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());
concept.put(propertyName, value);
}
} else if (propertyObjType.equals("array")) {
// 先设置空值
List<Object> array = new LinkedList<>();
JsonObject itemsObject = null;
if (object.has("items") && object.get("items").isJsonArray()) {
itemsObject = object.get("items").getAsJsonArray().get(0).getAsJsonObject();
} else {
itemsObject = object.get("items").getAsJsonObject();
}
if (object.has("items")) {
if (itemsObject.has("enum")) {
array.add(analyzeEnumProperty(object));
} 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));
} 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));
} 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();
for (Entry<String, JsonElement> entry : propertiesObj.entrySet()) {
String propertyKey = entry.getKey();
JsonObject propertyObj = propertiesObj.get(propertyKey).getAsJsonObject();
analyzeProperty(propertyConcept, propertyKey, propertyObj);
}
array.add(propertyConcept);
} else if (itemsObject.has("$ref")) {
analyzeRef(concept, propertyName, itemsObject);
}
} else if (object.has("items") && object.get("items").isJsonArray()) {
JsonArray itemsObjectArray = object.get("items").getAsJsonArray();
array.add(itemsObjectArray);
}
concept.put(propertyName, array);
} else if (propertyObjType.equals("object")) {
JSONObject obj = new JSONObject();
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) {
JsonArray enumValues = object.get("enum").getAsJsonArray();
List<Object> list = new LinkedList<>();
for (JsonElement enumValueElem : enumValues) {
String enumValue = enumValueElem.getAsString();
list.add(enumValue);
}
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()) {
String definitionKey = entry.getKey();
JsonObject definitionObj = definitionsObj.get(definitionKey).getAsJsonObject();
JSONObject obj = new JSONObject();
analyzeRootSchemaElement(definitionObj, obj);
}
}
private static final SyntaxValidator VALIDATOR = new SyntaxValidator(ValidationConfiguration.byDefault());
public static String getJson(String jsonSchema) {
if (StringUtils.isEmpty(jsonSchema)) {
return null;
}
try {
JsonNode jsonNode = JsonLoader.fromString(jsonSchema);
ProcessingReport report = VALIDATOR.validateSchema(jsonNode);
if (report.isSuccess()) {
JSONObject root = new JSONObject();
generator(jsonSchema, root);
// 格式化返回
Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().create();
return gson.toJson(root);
} else {
return report.getExceptionThreshold().toString();
}
} catch (Exception ex) {
ex.printStackTrace();
return ex.getMessage();
}
}
}

View File

@ -67,7 +67,7 @@
import MsApiKeyValue from "../ApiKeyValue";
import {BODY_TYPE, KeyValue} from "../../model/ApiTestModel";
import MsCodeEdit from "../../../../common/components/MsCodeEdit";
import MsJsonCodeEdit from "../../../../common/json-schema/JsonEditor";
import MsJsonCodeEdit from "../../../../common/json-schema/JsonSchemaEditor";
import MsDropdown from "../../../../common/components/MsDropdown";
import MsApiVariable from "../ApiVariable";
import MsApiBinaryVariable from "./ApiBinaryVariable";

View File

@ -1,5 +1,5 @@
<template>
<div id="app">
<div id="app" v-loading="loading">
<el-tabs v-model="activeName" @tab-click="handleClick">
<el-tab-pane label="模版" name="apiTemplate">
<el-button type="primary" size="mini" style="margin-left: 10px" @click="openOneClickOperation">导入</el-button>
@ -20,7 +20,6 @@
<script>
import {schemaToJson} from './common';
import json5 from 'json5';
import MsImportJson from './import/ImportJson';
const GenerateSchema = require('generate-schema/src/schemas/json.js');
@ -50,6 +49,7 @@
"properties": {},
}
},
loading: false,
preview: null,
activeName: "apiTemplate",
}
@ -57,7 +57,14 @@
methods: {
handleClick() {
if (this.activeName === 'preview') {
this.preview = schemaToJson(this.schema.root);
//
//this.preview = schemaToJson(this.schema.root);
this.loading = true;
//
this.$post('/api/definition/preview', this.schema.root, response => {
this.preview = response.data;
this.loading = false;
});
}
},
openOneClickOperation() {

View File

@ -1,10 +0,0 @@
<meta charset="utf-8">
<title>json-schema-editor-vue demo</title>
<script src="json-schema-editor-vue.umd.js"></script>
<link rel="stylesheet" href="json-schema-editor-vue.css">
<script>
console.log(json-schema-editor-vue)
</script>

View File

@ -6,7 +6,7 @@
<span v-if="pickValue.type==='object'" :class="hidden? 'el-tree-node__expand-icon el-icon-caret-right':
'expanded el-tree-node__expand-icon el-icon-caret-right'" @click="hidden = !hidden"/>
<span v-else style="width:10px;display:inline-block"></span>
<el-input :disabled="disabled || root" :value="pickKey" @blur="onInputName" size="small"/>
<input class="el-input el-input__inner" style="height: 32px" :disabled="disabled || root" :value="pickKey" @blur="onInputName" size="small"/>
</div>
<el-tooltip v-if="root" content="全选">
<input type="checkbox" :disabled="!isObject && !isArray" class="ant-col-name-required" @change="onRootCheck"/>
@ -110,6 +110,7 @@
import MsMock from './mock/index'
import MsDialogFooter from '../../../components/MsDialogFooter'
import LocalProvider from './provider/index'
import {getUUID} from "@/common/js/utils";
export default {
name: 'JsonSchemaEditor',
@ -282,7 +283,7 @@
}
},
_joinName() {
return `feild_${this.deep}_${this.countAdd++}`
return `feild_${this.deep}_${this.countAdd++}_${getUUID().substring(0, 5)}`
},
onSetting() {
this.modalVisible = true;

View File

@ -1,4 +1,4 @@
import JsonSchemaEditor from './json-schema-editor/index'
import JsonSchemaEditor from './editor/index'
const components = [
JsonSchemaEditor
]
@ -22,4 +22,4 @@ export default {
install,
// 组件列表
...components
}
}