fix(接口测试): jsonpath提取数字及对象字段问题修复
This commit is contained in:
parent
0b83bfbbd2
commit
6705ae6a7d
|
@ -38,7 +38,7 @@
|
||||||
json.value = props.data;
|
json.value = props.data;
|
||||||
if (typeof props.data === 'string') {
|
if (typeof props.data === 'string') {
|
||||||
json.value = parse(props.data, undefined, (value) => {
|
json.value = parse(props.data, undefined, (value) => {
|
||||||
if (!isSafeNumber(value) || Number(value).toString().length < value.length) {
|
if (!isSafeNumber(value) || Number(value).toString().length < value.length || !Number.isNaN(Number(value))) {
|
||||||
// 大数、超长小数、科学计数法、小数位全为 0 等情况下,JS 精度丢失,所以需要用字符串存储
|
// 大数、超长小数、科学计数法、小数位全为 0 等情况下,JS 精度丢失,所以需要用字符串存储
|
||||||
return `Number(${value.toString()})`;
|
return `Number(${value.toString()})`;
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,7 @@
|
||||||
jsonPath.value,
|
jsonPath.value,
|
||||||
json.value,
|
json.value,
|
||||||
JSONPath({ json: json.value, path: jsonPath.value }).map((e: any) =>
|
JSONPath({ json: json.value, path: jsonPath.value }).map((e: any) =>
|
||||||
`${e}`.replace(/Number\(([^)]+)\)/g, '$1')
|
JSON.stringify(e).replace(/Number\(([^)]+)\)/g, '$1')
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -119,7 +119,7 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="match-result">
|
<div class="match-result">
|
||||||
<div v-if="isMatched && matchResult.length === 0">{{ t('apiTestDebug.noMatchResult') }}</div>
|
<div v-if="isMatched && matchResult.length === 0">{{ t('apiTestDebug.noMatchResult') }}</div>
|
||||||
<template v-if="props.config.extractType === RequestExtractExpressionEnum.JSON_PATH">
|
<template v-else-if="props.config.extractType === RequestExtractExpressionEnum.JSON_PATH">
|
||||||
<pre>{{ matchResult }}</pre>
|
<pre>{{ matchResult }}</pre>
|
||||||
</template>
|
</template>
|
||||||
<template v-else>
|
<template v-else>
|
||||||
|
@ -188,7 +188,7 @@
|
||||||
(
|
(
|
||||||
e: 'apply',
|
e: 'apply',
|
||||||
config: (RegexExtract | JSONPathExtract | XPathExtract) & Record<string, any>,
|
config: (RegexExtract | JSONPathExtract | XPathExtract) & Record<string, any>,
|
||||||
matchResult: any[]
|
matchResult: any[] | string
|
||||||
): void;
|
): void;
|
||||||
}>();
|
}>();
|
||||||
|
|
||||||
|
@ -227,6 +227,26 @@
|
||||||
expressionFormRef.value?.clearValidate();
|
expressionFormRef.value?.clearValidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 遍历 JSON 对象,将 Number() 转换为数字
|
||||||
|
* @param obj JSON 对象
|
||||||
|
*/
|
||||||
|
function traverseJSONObject(obj: Record<string, any>) {
|
||||||
|
Object.keys(obj).forEach((key) => {
|
||||||
|
const val = obj[key];
|
||||||
|
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
||||||
|
if (typeof val === 'object' && val !== null) {
|
||||||
|
traverseJSONObject(val);
|
||||||
|
} else if (val.includes('Number(')) {
|
||||||
|
obj[key] = val.replace(/Number\(([^)]+)\)/g, '$1');
|
||||||
|
if (!Number.isNaN(Number(obj[key]))) {
|
||||||
|
obj[key] = Number(obj[key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 测试表达式
|
* 测试表达式
|
||||||
*/
|
*/
|
||||||
|
@ -258,13 +278,31 @@
|
||||||
path: expressionForm.value.expression,
|
path: expressionForm.value.expression,
|
||||||
wrap: false,
|
wrap: false,
|
||||||
});
|
});
|
||||||
matchResult.value = Array.isArray(results)
|
if (Array.isArray(results)) {
|
||||||
? results.map((e: any) =>
|
matchResult.value = results.map((e: any) => {
|
||||||
JSON.stringify(e)
|
let res;
|
||||||
|
if (typeof e === 'object' && e !== null && e !== undefined) {
|
||||||
|
res = JSON.parse(
|
||||||
|
JSON.stringify(e)
|
||||||
|
.replace(/Number\(([^)]+)\)/g, '$1')
|
||||||
|
.replace(/^"|"$/g, '')
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
res = JSON.stringify(e)
|
||||||
.replace(/Number\(([^)]+)\)/g, '$1')
|
.replace(/Number\(([^)]+)\)/g, '$1')
|
||||||
.replace(/^"|"$/g, '')
|
.replace(/^"|"$/g, '');
|
||||||
)
|
if (!Number.isNaN(Number(res))) {
|
||||||
: results;
|
res = Number(res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
});
|
||||||
|
} else if (typeof results === 'object') {
|
||||||
|
traverseJSONObject(results);
|
||||||
|
matchResult.value = results;
|
||||||
|
} else {
|
||||||
|
matchResult.value = results === null ? `${results}` : results || [];
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
matchResult.value = JSONPath({ json: props.response || '', path: expressionForm.value.expression }) || [];
|
matchResult.value = JSONPath({ json: props.response || '', path: expressionForm.value.expression }) || [];
|
||||||
}
|
}
|
||||||
|
@ -311,7 +349,11 @@
|
||||||
function confirmHandler() {
|
function confirmHandler() {
|
||||||
expressionFormRef.value?.validate(async (errors: undefined | Record<string, ValidatedError>) => {
|
expressionFormRef.value?.validate(async (errors: undefined | Record<string, ValidatedError>) => {
|
||||||
if (!errors) {
|
if (!errors) {
|
||||||
emit('apply', expressionForm.value, matchResult.value);
|
emit(
|
||||||
|
'apply',
|
||||||
|
expressionForm.value,
|
||||||
|
typeof matchResult.value === 'object' ? JSON.stringify(matchResult.value) : matchResult.value
|
||||||
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue