diff --git a/frontend/src/business/components/api/definition/components/body/ApiBody.vue b/frontend/src/business/components/api/definition/components/body/ApiBody.vue index 4f188a7a7309..fe98a1a4ea77 100644 --- a/frontend/src/business/components/api/definition/components/body/ApiBody.vue +++ b/frontend/src/business/components/api/definition/components/body/ApiBody.vue @@ -97,6 +97,7 @@ import MsApiBinaryVariable from "./ApiBinaryVariable"; import MsApiFromUrlVariable from "./ApiFromUrlVariable"; import BatchAddParameter from "../basis/BatchAddParameter"; import Convert from "@/business/components/common/json-schema/convert/convert"; +import {jsonParse, trimAll} from "@/business/components/common/json-schema/convert/jsonParse"; export default { @@ -226,12 +227,9 @@ export default { if (this.body.format === 'JSON-SCHEMA') { if (this.body.raw) { try { - if (!this.body.jsonSchema) { - this.body.jsonSchema = MsConvert.format(JSON.parse(this.body.raw)); - } else { - let data = MsConvert.format(JSON.parse(this.body.raw)); - this.body.jsonSchema = this.deepAssign(this.body.jsonSchema, data); - } + const tmpStr = trimAll(this.body.raw) + const tmpObj = jsonParse(tmpStr) + this.body.jsonSchema = MsConvert.format(tmpObj); } catch (e) { this.body.format = 'JSON'; this.$error(this.$t('api_test.definition.json_format_error')); diff --git a/frontend/src/business/components/common/json-schema/convert/convert.js b/frontend/src/business/components/common/json-schema/convert/convert.js index 4e68ed945147..3428a804bbd1 100644 --- a/frontend/src/business/components/common/json-schema/convert/convert.js +++ b/frontend/src/business/components/common/json-schema/convert/convert.js @@ -7,7 +7,7 @@ const isObject = require("lodash.isobject"); const isString = require("lodash.isstring"); const {post} = require("@/common/js/ajax"); const isArray = Array.isArray; - +const { default: CustomNum } = require('./customNum'); class Convert { constructor() { @@ -113,7 +113,7 @@ class Convert { if (!result["properties"]) { continue; } - if (isObject(element)) { + if (isObject(element) && !(element instanceof CustomNum)) { // 创建当前属性的基本信息 result["properties"][key] = this._value2object(element, $id, key) if (isArray(element)) { @@ -234,6 +234,10 @@ class Convert { } else if (isArray(value)) { objectTemplate.type = "array"; objectTemplate["mock"] = undefined; + } else if (value instanceof CustomNum) { + // 解决丢失精度问题 + objectTemplate.type = 'number'; + objectTemplate['mock'].mock = value.get(); } else if (isObject(value)) { objectTemplate.type = "object" objectTemplate["mock"] = undefined; diff --git a/frontend/src/business/components/common/json-schema/convert/customNum.js b/frontend/src/business/components/common/json-schema/convert/customNum.js new file mode 100644 index 000000000000..33a59f17f48d --- /dev/null +++ b/frontend/src/business/components/common/json-schema/convert/customNum.js @@ -0,0 +1,9 @@ +class CustomNum{ + constructor(value) { + this.value = value + } + get() { + return this.value + } +} +export default CustomNum; diff --git a/frontend/src/business/components/common/json-schema/convert/jsonParse.js b/frontend/src/business/components/common/json-schema/convert/jsonParse.js new file mode 100644 index 000000000000..2001d74ab891 --- /dev/null +++ b/frontend/src/business/components/common/json-schema/convert/jsonParse.js @@ -0,0 +1,91 @@ +import CustomNum from './customNum'; + +export const jsonParse = (jsonStr) => { + let index = 0; + function parseValue() { + let char = jsonStr[index]; + if (char === '{') { + return parseObject(); + } else if (char === '[') { + return parseArray(); + } else if (char === '"') { + return parseString(); + } else if (char === 't' || char === 'f') { + return parseBoolean(); + } else if (char === 'n') { + return parseNull(); + } else { + return parseNumber(); + } + } + function parseObject() { + let obj = {}; + index++; + while (jsonStr[index] !== '}') { + let key = parseString(); + index++; + let value = parseValue(); + obj[key] = value; + if (jsonStr[index] === ',') { + index++; + } + } + index++; + return obj; + } + function parseArray() { + let arr = []; + index++; + while (jsonStr[index] !== ']') { + arr.push(parseValue()); + if (jsonStr[index] === ',') { + index++; + } + } + index++; + return arr; + } + function parseString() { + let str = ''; + index++; + while (jsonStr[index] !== '"') { + str += jsonStr[index]; + index++; + } + index++; + return str; + } + function parseNumber() { + let numStr = ''; + while (/[0-9.+-]/.test(jsonStr[index])) { + numStr += jsonStr[index]; + index++; + } + if (!isInteger(numStr) || numStr.length > 15) { + return new CustomNum(numStr); + } + return parseFloat(numStr); + } + function parseBoolean() { + if (jsonStr[index] === 't') { + index += 4; + return true; + } else { + index += 5; + return false; + } + } + function parseNull() { + index += 4; + return null; + } + return parseValue(); +}; +export const isInteger = (num) => { + return /^\d+$/.test(num); +}; +export const trimAll = (ele) => { + if (typeof ele === 'string') { + return ele.split(/[\t\r\f\n\s]*/g).join(''); + } +};