fix(接口测试): 自定义Json Parse 方法增加转义字符判断
This commit is contained in:
parent
c57c49da6c
commit
cc5d536c6f
|
@ -28,8 +28,12 @@ export const jsonParse = (jsonStr) => {
|
||||||
function parseObject() {
|
function parseObject() {
|
||||||
let obj = {};
|
let obj = {};
|
||||||
index++;
|
index++;
|
||||||
while (jsonStr[index] !== '}') {
|
while (jsonStr[index] !== '}' && index < jsonStr.length) {
|
||||||
let key = parseString();
|
let key = parseString();
|
||||||
|
if (key === '}') {
|
||||||
|
index++;
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
index++;
|
index++;
|
||||||
let value = parseValue();
|
let value = parseValue();
|
||||||
obj[key] = value;
|
obj[key] = value;
|
||||||
|
@ -40,13 +44,16 @@ export const jsonParse = (jsonStr) => {
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (index > jsonStr.length) {
|
||||||
|
throwError('Invalid object');
|
||||||
|
}
|
||||||
index++;
|
index++;
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
function parseArray() {
|
function parseArray() {
|
||||||
let arr = [];
|
let arr = [];
|
||||||
index++;
|
index++;
|
||||||
while (jsonStr[index] !== ']') {
|
while (jsonStr[index] !== ']' && index < jsonStr.length) {
|
||||||
arr.push(parseValue());
|
arr.push(parseValue());
|
||||||
if (jsonStr[index] === ',') {
|
if (jsonStr[index] === ',') {
|
||||||
index++;
|
index++;
|
||||||
|
@ -55,6 +62,9 @@ export const jsonParse = (jsonStr) => {
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (index > jsonStr.length) {
|
||||||
|
throwError('Invalid array');
|
||||||
|
}
|
||||||
index++;
|
index++;
|
||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
|
@ -64,9 +74,54 @@ export const jsonParse = (jsonStr) => {
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
index++;
|
index++;
|
||||||
while (jsonStr[index] !== '"') {
|
while (jsonStr[index] !== '"' && index < jsonStr.length) {
|
||||||
str += jsonStr[index];
|
let char = jsonStr[index];
|
||||||
|
if (char === '\\') {
|
||||||
index++;
|
index++;
|
||||||
|
let nextChar = jsonStr[index];
|
||||||
|
switch (nextChar) {
|
||||||
|
case '"':
|
||||||
|
str += '"';
|
||||||
|
break;
|
||||||
|
case '\\':
|
||||||
|
str += '\\';
|
||||||
|
break;
|
||||||
|
case '/':
|
||||||
|
str += '/';
|
||||||
|
break;
|
||||||
|
case 'b':
|
||||||
|
str += '\b';
|
||||||
|
break;
|
||||||
|
case 'f':
|
||||||
|
str += '\f';
|
||||||
|
break;
|
||||||
|
case 'n':
|
||||||
|
str += '\n';
|
||||||
|
break;
|
||||||
|
case 'r':
|
||||||
|
str += '\r';
|
||||||
|
break;
|
||||||
|
case 't':
|
||||||
|
str += '\t';
|
||||||
|
break;
|
||||||
|
case 'u':
|
||||||
|
// eslint-disable-next-line no-case-declarations
|
||||||
|
let unicode = jsonStr.substr(index + 1, 4);
|
||||||
|
str += String.fromCharCode(parseInt(unicode, 16));
|
||||||
|
index += 4;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
// If an unknown escape sequence is encountered, treat it as a literal character
|
||||||
|
str += '\\' + nextChar;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
str += char;
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
if (index > jsonStr.length) {
|
||||||
|
throwError('Invalid string');
|
||||||
}
|
}
|
||||||
index++;
|
index++;
|
||||||
return str;
|
return str;
|
||||||
|
@ -77,6 +132,9 @@ export const jsonParse = (jsonStr) => {
|
||||||
numStr += jsonStr[index];
|
numStr += jsonStr[index];
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
|
if (isNaN(Number(numStr))) {
|
||||||
|
throwError('Invalid number');
|
||||||
|
}
|
||||||
if (!isInteger(numStr) || numStr.length > 15) {
|
if (!isInteger(numStr) || numStr.length > 15) {
|
||||||
return new CustomNum(numStr);
|
return new CustomNum(numStr);
|
||||||
}
|
}
|
||||||
|
|
|
@ -106,7 +106,7 @@ import BatchAddParameter from '../basis/BatchAddParameter';
|
||||||
import Convert from '@/business/commons/json-schema/convert/convert';
|
import Convert from '@/business/commons/json-schema/convert/convert';
|
||||||
import { getApiParamsConfigFields } from 'metersphere-frontend/src/utils/custom_field';
|
import { getApiParamsConfigFields } from 'metersphere-frontend/src/utils/custom_field';
|
||||||
import ApiParamsConfig from '@/business/definition/components/request/components/ApiParamsConfig';
|
import ApiParamsConfig from '@/business/definition/components/request/components/ApiParamsConfig';
|
||||||
import { jsonParse, trimAll } from '@/business/commons/json-schema/convert/jsonParse';
|
import { jsonParse } from '@/business/commons/json-schema/convert/jsonParse'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'MsApiBody',
|
name: 'MsApiBody',
|
||||||
|
@ -282,8 +282,11 @@ export default {
|
||||||
if (this.body.format === 'JSON-SCHEMA') {
|
if (this.body.format === 'JSON-SCHEMA') {
|
||||||
if (this.body.raw) {
|
if (this.body.raw) {
|
||||||
try {
|
try {
|
||||||
const tmpObj = jsonParse(this.body.raw);
|
const isJson = JSON.parse(this.body.raw);
|
||||||
this.body.jsonSchema = MsConvert.format(tmpObj);
|
if(isJson) {
|
||||||
|
const jsonObj = jsonParse(this.body.raw)
|
||||||
|
this.body.jsonSchema = MsConvert.format(jsonObj);
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.body.format = 'JSON';
|
this.body.format = 'JSON';
|
||||||
this.$message.error(this.$t('api_definition.body.json_format_error'));
|
this.$message.error(this.$t('api_definition.body.json_format_error'));
|
||||||
|
|
Loading…
Reference in New Issue