From 76adb0d402029cf668b1492d4c1a15d624a8c1d3 Mon Sep 17 00:00:00 2001 From: baiqi Date: Sun, 28 Apr 2024 18:23:26 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=20=E6=96=87=E4=BB=B6=20id=20=E9=97=AE=E9=A2=98=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/models/apiTest/common.ts | 2 +- .../src/views/api-test/components/config.ts | 2 + .../components/requestAndResponse.vue | 1 + .../components/requestComposition/index.vue | 7 ++ .../requestComposition/response/edit.vue | 7 +- .../requestComposition/response/index.vue | 1 + .../src/views/api-test/components/utils.ts | 45 +++++++++- frontend/src/views/api-test/debug/index.vue | 82 ------------------- .../components/management/api/index.vue | 2 +- .../components/common/customApiDrawer.vue | 3 +- .../components/common/customCaseDrawer.vue | 1 + 11 files changed, 61 insertions(+), 92 deletions(-) diff --git a/frontend/src/models/apiTest/common.ts b/frontend/src/models/apiTest/common.ts index 94dd2cf57a..7af8752bd2 100644 --- a/frontend/src/models/apiTest/common.ts +++ b/frontend/src/models/apiTest/common.ts @@ -115,7 +115,7 @@ export interface RequestFileInfo { export interface ExecuteBinaryBody { description: string; file?: RequestFileInfo; - sendAsBody?: boolean; // 是否作为正文发送,只有 mock 有此字段 + sendAsBody?: boolean; // 是否作为正文发送,只有 定义/mock 的响应体有此字段 } // 接口请求-JsonSchema export interface JsonSchema { diff --git a/frontend/src/views/api-test/components/config.ts b/frontend/src/views/api-test/components/config.ts index e8fe5021b6..b20cb6b1e8 100644 --- a/frontend/src/views/api-test/components/config.ts +++ b/frontend/src/views/api-test/components/config.ts @@ -92,6 +92,7 @@ export const defaultResponseItem: ResponseDefinition = { binaryBody: { description: '', file: undefined, + sendAsBody: false, }, }, }; @@ -112,6 +113,7 @@ export const defaultBodyParams: ExecuteBody = { binaryBody: { description: '', file: undefined, + sendAsBody: false, }, rawBody: { value: '' }, }; diff --git a/frontend/src/views/api-test/components/requestAndResponse.vue b/frontend/src/views/api-test/components/requestAndResponse.vue index 14af55689e..36d9b74a30 100644 --- a/frontend/src/views/api-test/components/requestAndResponse.vue +++ b/frontend/src/views/api-test/components/requestAndResponse.vue @@ -456,6 +456,7 @@ ).validParams; parseRequestBodyResult = parseRequestBodyFiles( requestVModel.value.body, + requestVModel.value.responseDefinition, requestVModel.value.uploadFileIds, // 外面解析详情的时候传入 requestVModel.value.linkFileIds // 外面解析详情的时候传入 ); diff --git a/frontend/src/views/api-test/components/requestComposition/index.vue b/frontend/src/views/api-test/components/requestComposition/index.vue index 5fac1c2cbf..f5075a0aa3 100644 --- a/frontend/src/views/api-test/components/requestComposition/index.vue +++ b/frontend/src/views/api-test/components/requestComposition/index.vue @@ -1083,6 +1083,7 @@ ).validParams; parseRequestBodyResult = parseRequestBodyFiles( requestVModel.value.body, + requestVModel.value.responseDefinition, requestVModel.value.uploadFileIds, // 外面解析详情的时候传入 requestVModel.value.linkFileIds // 外面解析详情的时候传入 ); @@ -1292,6 +1293,12 @@ Message.success(t('common.updateSuccess')); requestVModel.value.updateTime = res.updateTime; requestVModel.value.unSaved = false; + const parseRequestBodyResult = parseRequestBodyFiles( + requestVModel.value.body, + requestVModel.value.responseDefinition + ); + requestVModel.value.uploadFileIds = parseRequestBodyResult.uploadFileIds; + requestVModel.value.linkFileIds = parseRequestBodyResult.linkFileIds; emit('addDone'); } catch (error) { // eslint-disable-next-line no-console diff --git a/frontend/src/views/api-test/components/requestComposition/response/edit.vue b/frontend/src/views/api-test/components/requestComposition/response/edit.vue index 996cd06309..c39d3aa5e4 100644 --- a/frontend/src/views/api-test/components/requestComposition/response/edit.vue +++ b/frontend/src/views/api-test/components/requestComposition/response/edit.vue @@ -380,7 +380,7 @@ async function handleFileChange() { try { - if (fileList.value[0]?.local && fileList.value[0].file && props.uploadTempFileApi) { + if (fileList.value[0] && fileList.value[0].local && fileList.value[0].file && props.uploadTempFileApi) { appStore.showLoading(); const res = await props.uploadTempFileApi(fileList.value[0].file); activeResponse.value.body.binaryBody.file = { @@ -391,7 +391,7 @@ local: true, }; appStore.hideLoading(); - } else { + } else if (fileList.value[0]) { activeResponse.value.body.binaryBody.file = { ...fileList.value[0], fileId: fileList.value[0].uid, @@ -399,8 +399,7 @@ fileAlias: fileList.value[0]?.name || '', local: false, }; - } - if (activeResponse.value.body.binaryBody.file && !activeResponse.value.body.binaryBody.file.fileId) { + } else { activeResponse.value.body.binaryBody.file = undefined; } emit('change'); diff --git a/frontend/src/views/api-test/components/requestComposition/response/index.vue b/frontend/src/views/api-test/components/requestComposition/response/index.vue index 7280a8ab07..428dcdafe7 100644 --- a/frontend/src/views/api-test/components/requestComposition/response/index.vue +++ b/frontend/src/views/api-test/components/requestComposition/response/index.vue @@ -163,6 +163,7 @@ item.body.binaryBody = { description: '', file: undefined, + sendAsBody: false, }; hasInvalid = true; } diff --git a/frontend/src/views/api-test/components/utils.ts b/frontend/src/views/api-test/components/utils.ts index aa4e9c4105..50ba53364e 100644 --- a/frontend/src/views/api-test/components/utils.ts +++ b/frontend/src/views/api-test/components/utils.ts @@ -1,13 +1,17 @@ import { cloneDeep, isEqual } from 'lodash-es'; -import { type ExecuteAssertionConfig, ExecuteBody, type ExecuteConditionConfig } from '@/models/apiTest/common'; -import { RequestConditionProcessor, RequestParamsType, ResponseBodyAssertionType } from '@/enums/apiEnum'; +import { + type ExecuteAssertionConfig, + ExecuteBody, + type ExecuteConditionConfig, + type ResponseDefinition, +} from '@/models/apiTest/common'; +import { RequestConditionProcessor, RequestParamsType } from '@/enums/apiEnum'; import { assertDefaultParamsItem, defaultAssertParamsItem, defaultBodyParamsItem, - defaultExtractParamItem, defaultHeaderParamsItem, defaultKeyValueParamItem, defaultRequestParamsItem, @@ -29,6 +33,7 @@ export interface ParseResult { */ export function parseRequestBodyFiles( body: ExecuteBody, + response?: ResponseDefinition[], saveUploadFileIds?: string[], saveLinkFileIds?: string[] ): ParseResult { @@ -107,6 +112,40 @@ export function parseRequestBodyFiles( linkFileIds.add(fileId); } } + if (response) { + response.forEach((res) => { + if (res.body.binaryBody && res.body.binaryBody.file) { + const { fileId } = res.body.binaryBody.file; + if (res.body.binaryBody.file?.local) { + if (saveUploadFileIds) { + // 如果有已保存的上传文件id集合 + if (saveUploadFileIds.includes(fileId)) { + // 当前文件是已保存的文件,存入 tempSaveUploadFileIds + tempSaveUploadFileIds.add(fileId); + } else { + // 当前文件不是已保存的文件,存入 uploadFileIds + uploadFileIds.add(fileId); + } + } else { + // 没有已保存的文件id集合,直接存入 uploadFileIds + uploadFileIds.add(fileId); + } + } else if (saveLinkFileIds) { + // 如果有已保存的关联文件id集合 + if (saveLinkFileIds.includes(fileId)) { + // 当前文件是已保存的文件,存入 + tempSaveLinkFileIds.add(fileId); + } else { + // 当前文件不是已保存的文件,存入 uploadFileIds + linkFileIds.add(fileId); + } + } else { + // 关联的文件 + linkFileIds.add(fileId); + } + } + }); + } return { uploadFileIds: Array.from(uploadFileIds), linkFileIds: Array.from(linkFileIds), diff --git a/frontend/src/views/api-test/debug/index.vue b/frontend/src/views/api-test/debug/index.vue index 96be31251d..ae65233f8a 100644 --- a/frontend/src/views/api-test/debug/index.vue +++ b/frontend/src/views/api-test/debug/index.vue @@ -92,64 +92,6 @@ -