From 00c105c79631b883dc63cb4467a5945d3190c251 Mon Sep 17 00:00:00 2001 From: zhangguoyun Date: Thu, 12 Mar 2026 14:22:43 +0800 Subject: [PATCH] =?UTF-8?q?feat(apifox):=20=E5=A2=9E=E5=8A=A0=E6=8F=90?= =?UTF-8?q?=E5=8F=96=E8=AF=B7=E6=B1=82=E5=8F=82=E6=95=B0=20schema=20?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 实现 extractRequestSchema 方法,根据请求方法提取相应的参数 schema - GET 请求从 parameters.query 中生成 JSON Schema - POST/PUT/PATCH 请求从 requestBody.jsonSchema 解析 schema 引用并获取完整定义 - 在 getApiDetailResp 中使用 extractRequestSchema 替代原有逻辑 - 修改接口目录描述,从“接口路径”更新为“目录路径” --- apifox.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- index.js | 2 +- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/apifox.js b/apifox.js index 8be74c2..24fd92f 100644 --- a/apifox.js +++ b/apifox.js @@ -353,12 +353,56 @@ export class ApifoxService { return resolved; } + /** + * 从 apiDetail 中提取请求参数的 schema + * GET 请求从 parameters.query 中提取 + * POST/PUT/PATCH 请求从 requestBody.jsonSchema 中提取 + * @param {Object} apiDetail - API 详情对象 + * @param {Object} context - 包含 projectId 和 allSchemas 的上下文 + * @returns {Promise} 请求参数的 schema + */ + async extractRequestSchema(apiDetail, context) { + const method = (apiDetail.method || '').toLowerCase(); + + // GET 请求:从 parameters.query 中提取参数定义 + if (method === 'get') { + const queryParams = apiDetail.parameters?.query; + if (!queryParams || queryParams.length === 0) { + return null; + } + + // 将 query 参数转换为 JSON Schema 格式 + const properties = {}; + const required = []; + + for (const param of queryParams) { + properties[param.name] = param.schema || { type: param.type || 'string' }; + if (param.required) { + required.push(param.name); + } + } + + return { + type: 'object', + properties, + required: required.length > 0 ? required : undefined + }; + } + + // POST/PUT/PATCH 请求:从 requestBody.jsonSchema 中提取 + const requestSchemaId = this.getSchemaRefId(apiDetail.requestBody?.jsonSchema); + if (requestSchemaId) { + return await this.getSchemaById(requestSchemaId, context); + } + + return null; + } + async getApiDetailResp(apiDetail, projectId) { const pid = projectId || this.defaultProjectId; const dataSchema = await this.getDataSchema(pid); const context = { projectId: pid, allSchemas: dataSchema }; - const requestSchemaId = this.getSchemaRefId(apiDetail.requestBody.jsonSchema); const responseSchema = Array.isArray(apiDetail.responses) ? apiDetail.responses[0].jsonSchema : apiDetail.responses.jsonSchema; const responseSchemaId = this.getSchemaRefId(responseSchema); @@ -402,7 +446,7 @@ export class ApifoxService { name: apiDetail.name, method: apiDetail.method, url: apiDetail.url, - requestSchema: await this.getSchemaById(requestSchemaId, context), + requestSchema: await this.extractRequestSchema(apiDetail, context), responseSchema: (responseDataSchema && responseDataSchema.type === 'array') ? { type: 'array', items: respDataSchemaId ? await this.getSchemaById(respDataSchemaId, context) : null, diff --git a/index.js b/index.js index 65ceed3..0652e83 100644 --- a/index.js +++ b/index.js @@ -83,7 +83,7 @@ server.registerTool( { description: "根据接口目录获取Apifox目录下所有接口定义数据", inputSchema: z.object({ - folderName: z.string().describe("接口路径"), + folderName: z.string().describe("目录路径"), projectId: z.number().optional().describe("Apifox项目ID"), }), },