fix(接口测试): 自定义Json Parse 方法增加转义字符判断
This commit is contained in:
parent
c57c49da6c
commit
cc5d536c6f
|
@ -28,8 +28,12 @@ export const jsonParse = (jsonStr) => {
|
|||
function parseObject() {
|
||||
let obj = {};
|
||||
index++;
|
||||
while (jsonStr[index] !== '}') {
|
||||
while (jsonStr[index] !== '}' && index < jsonStr.length) {
|
||||
let key = parseString();
|
||||
if (key === '}') {
|
||||
index++;
|
||||
return obj;
|
||||
}
|
||||
index++;
|
||||
let value = parseValue();
|
||||
obj[key] = value;
|
||||
|
@ -40,13 +44,16 @@ export const jsonParse = (jsonStr) => {
|
|||
index++;
|
||||
}
|
||||
}
|
||||
if (index > jsonStr.length) {
|
||||
throwError('Invalid object');
|
||||
}
|
||||
index++;
|
||||
return obj;
|
||||
}
|
||||
function parseArray() {
|
||||
let arr = [];
|
||||
index++;
|
||||
while (jsonStr[index] !== ']') {
|
||||
while (jsonStr[index] !== ']' && index < jsonStr.length) {
|
||||
arr.push(parseValue());
|
||||
if (jsonStr[index] === ',') {
|
||||
index++;
|
||||
|
@ -55,6 +62,9 @@ export const jsonParse = (jsonStr) => {
|
|||
index++;
|
||||
}
|
||||
}
|
||||
if (index > jsonStr.length) {
|
||||
throwError('Invalid array');
|
||||
}
|
||||
index++;
|
||||
return arr;
|
||||
}
|
||||
|
@ -64,10 +74,55 @@ export const jsonParse = (jsonStr) => {
|
|||
index++;
|
||||
}
|
||||
index++;
|
||||
while (jsonStr[index] !== '"') {
|
||||
str += jsonStr[index];
|
||||
while (jsonStr[index] !== '"' && index < jsonStr.length) {
|
||||
let char = jsonStr[index];
|
||||
if (char === '\\') {
|
||||
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++;
|
||||
return str;
|
||||
}
|
||||
|
@ -77,6 +132,9 @@ export const jsonParse = (jsonStr) => {
|
|||
numStr += jsonStr[index];
|
||||
index++;
|
||||
}
|
||||
if (isNaN(Number(numStr))) {
|
||||
throwError('Invalid number');
|
||||
}
|
||||
if (!isInteger(numStr) || numStr.length > 15) {
|
||||
return new CustomNum(numStr);
|
||||
}
|
||||
|
|
|
@ -106,7 +106,7 @@ import BatchAddParameter from '../basis/BatchAddParameter';
|
|||
import Convert from '@/business/commons/json-schema/convert/convert';
|
||||
import { getApiParamsConfigFields } from 'metersphere-frontend/src/utils/custom_field';
|
||||
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 {
|
||||
name: 'MsApiBody',
|
||||
|
@ -282,8 +282,11 @@ export default {
|
|||
if (this.body.format === 'JSON-SCHEMA') {
|
||||
if (this.body.raw) {
|
||||
try {
|
||||
const tmpObj = jsonParse(this.body.raw);
|
||||
this.body.jsonSchema = MsConvert.format(tmpObj);
|
||||
const isJson = JSON.parse(this.body.raw);
|
||||
if(isJson) {
|
||||
const jsonObj = jsonParse(this.body.raw)
|
||||
this.body.jsonSchema = MsConvert.format(jsonObj);
|
||||
}
|
||||
} catch (e) {
|
||||
this.body.format = 'JSON';
|
||||
this.$message.error(this.$t('api_definition.body.json_format_error'));
|
||||
|
|
Loading…
Reference in New Issue