fix(接口测试): jsonpath提取数字及对象字段问题修复

This commit is contained in:
baiqi 2024-08-13 10:38:40 +08:00 committed by Craftsman
parent 0b83bfbbd2
commit 6705ae6a7d
2 changed files with 53 additions and 11 deletions

View File

@ -38,7 +38,7 @@
json.value = props.data;
if (typeof props.data === 'string') {
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
return `Number(${value.toString()})`;
}
@ -76,7 +76,7 @@
jsonPath.value,
json.value,
JSONPath({ json: json.value, path: jsonPath.value }).map((e: any) =>
`${e}`.replace(/Number\(([^)]+)\)/g, '$1')
JSON.stringify(e).replace(/Number\(([^)]+)\)/g, '$1')
)
);
}

View File

@ -119,7 +119,7 @@
</div>
<div class="match-result">
<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>
</template>
<template v-else>
@ -188,7 +188,7 @@
(
e: 'apply',
config: (RegexExtract | JSONPathExtract | XPathExtract) & Record<string, any>,
matchResult: any[]
matchResult: any[] | string
): void;
}>();
@ -227,6 +227,26 @@
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,
wrap: false,
});
matchResult.value = Array.isArray(results)
? results.map((e: any) =>
if (Array.isArray(results)) {
matchResult.value = results.map((e: any) => {
let res;
if (typeof e === 'object' && e !== null && e !== undefined) {
res = JSON.parse(
JSON.stringify(e)
.replace(/Number\(([^)]+)\)/g, '$1')
.replace(/^"|"$/g, '')
)
: results;
);
} else {
res = JSON.stringify(e)
.replace(/Number\(([^)]+)\)/g, '$1')
.replace(/^"|"$/g, '');
if (!Number.isNaN(Number(res))) {
res = Number(res);
}
}
return res;
});
} else if (typeof results === 'object') {
traverseJSONObject(results);
matchResult.value = results;
} else {
matchResult.value = results === null ? `${results}` : results || [];
}
} catch (error) {
matchResult.value = JSONPath({ json: props.response || '', path: expressionForm.value.expression }) || [];
}
@ -311,7 +349,11 @@
function confirmHandler() {
expressionFormRef.value?.validate(async (errors: undefined | Record<string, ValidatedError>) => {
if (!errors) {
emit('apply', expressionForm.value, matchResult.value);
emit(
'apply',
expressionForm.value,
typeof matchResult.value === 'object' ? JSON.stringify(matchResult.value) : matchResult.value
);
}
});
}