Yapi Bug修复汇总

转载请注明出处
由于官方早已不维护,因此博主会长期维护最新版本
这是博主对yapi修复后的github地址:yapi, 欢迎fork

频繁升级提示

路径: /client/components/Notify/Notify.js

const isShow = this.state.newVersion > this.state.version;

初始密码可配置

路径:/server/install.js
新增yapi.WEBCONFIG.adminPassword配置项
在config.json中配置 "adminPassword":"xxxxx"

password: yapi.commons.generatePassword(yapi.WEBCONFIG.adminPassword || "ymfe.org", passsalt)

result.then(
    function() {
        fs.ensureFileSync(yapi.path.join(yapi.WEBROOT_RUNTIME, 'init.lock'));
        console.log(
            `初始化管理员账号成功,账号名:"${yapi.WEBCONFIG.adminAccount}",密码:${yapi.WEBCONFIG.adminPassword || "ymfe.org"}"`
        ); // eslint-disable-line
        process.exit(0);
    },
    function(err) {
        throw new Error(`初始化管理员账号 "${yapi.WEBCONFIG.adminAccount}" 失败, ${err.message}`);
    }
);

修改接口后提示报错,而实际保存成功

路径:/server/models/project.js
handleEnvNullData方法开头处判空

handleEnvNullData(data)
{
    if (data == null) {
        return false;
    }
    
    ... 其他代码
}

调接口上传swagger时,提示不存在的导入方式 和 自动同步不生效

路径:/server/controllers/open.js
将yapi.emitHook('import_data', importDataModule); 放到openController的constructor的最后执行

class openController extends baseController {
  constructor(ctx) {
    ... 其他代码
    yapi.emitHook('import_data', importDataModule);
  }

上传接口,自动同步均发邮件提醒

路径:/server/controllers/interface.js
去掉params.switch_notice的判断
删除以下代码:

if (params.switch_notice === true)

method Promise.prototype.then called on incompatible receiver object Object

路径: /package.json
将vm2版本调整为3.5.2

 "vm2": "^3.5.2",

swagger的$ref无法解析,导致接口参数丢失

路径: /package.json
将swagger-client版本调整为3.10.0

 "swagger-client": "3.10.0",

请求配置开关导致的客户端打包报错和配置脚本无效

例:A Cool Image 原因:client无法正确导入并解析配置文件json

路径:/common/postmanLib.js

try {
const yapi = require('../server/yapi');
scriptEnable = yapi.WEBCONFIG.scriptEnable === true;
} catch (err) {}

这里是“请求配置”脚本是否生效的判断条件,其实没必要有这个判断,毕竟这是基础功能,scriptEnable纯属多此一举

解决: 删掉以上代码,并将let scriptEnable = true;即可 之后重新运行build命令,则不再报错

NODE_ENV=production ykit pack -m

A Cool Image

全方面修复升级沙盒导致高级mock和自动化测试一系列bug

路径: /server/utils/sandbox.js

const Safeify = require('safeify').default;

module.exports = async function sandboxFn(context, script, autoTest) {
    // 创建 safeify 实例
    const safeVm = new Safeify({
        timeout: 3000,          //超时时间
        asyncTimeout: 10000,    //包含异步操作的超时时间
        unrestricted: true,
        quantity: 4,          //沙箱进程数量,默认同 CPU 核数
        memoryQuota: 500,     //沙箱最大能使用的内存(单位 m),默认 500m
        cpuQuota: 0.5,        //沙箱的 cpu 资源配额(百分比),默认 50%
        unsafe: {
            modules: {
                assert: 'assert',
                mockjs: 'mockjs'
            }
        }
    })
    safeVm.preset('const assert = require("assert");const Mock = require("mockjs"); const Random = Mock.Random;');

    // 执行动态代码·
    if (autoTest) {
        script += "\n return {}";
    } else {
        script += "\n  return {mockJson, resHeader, httpCode, delay}";
    }

    const result = await safeVm.run(script, context)

    // 释放资源
    safeVm.destroy()
    return result
}

在高级mock调用sandboxFn处,入参autoTest = false

sandbox = await sandboxFn(sandbox, script, false);
sandbox.delay = isNaN(sandbox.delay) ? 0 : +sandbox.delay;

context.mockJson = sandbox.mockJson;
context.resHeader = sandbox.resHeader;
context.httpCode = sandbox.httpCode;
context.delay = sandbox.delay;

在自动化测试调用sandboxFn处,入参autoTest = true

if (colData.checkScript.enable) {
    let globalScript = colData.checkScript.content;
    // script 是断言
    if (globalScript) {
        logs.push('执行脚本:' + globalScript)
        result = await sandboxFn(context, globalScript, true);
    }
}

let script = params.script;
// script 是断言
if (script) {
    logs.push('执行脚本:' + script)
    result = await sandboxFn(context, script, true);
}

总结

  • 经过以上的修复,所有正常功能都已恢复使用
  • 具体代码可看github源码:yapi
    • 同时还添加了其他组件,并修复组件内的问题:例如添加用户,接口自动鉴权,通知, 自动化通知等功能
    • 下载源码,并解压node_modules, 运行项目即可
    • config.json参考
{
   "port": "3000",
   "adminAccount": "admin@admin.com",
   "adminPassword":"admin",
   "timeout": 120000,
   "closeRegister":true,
   "db": {
      "servername": "127.0.0.1",
      "DATABASE": "yapi",
      "port": 27017
   },
   "mail": {
      "enable": false
   },
   "plugins": [
      {
         "name": "notifier",
         "options": {
            "host": "http://localhost:3000"
         }
      },
      {
         "name": "interface-oauth2-token"
      },
      {
         "name": "add-user"
      },
      {
         "name": "pl-auto-test",
         "options": {
            "host": "http://localhost:3000"
         }
      },
      {
         "name": "api-watch"
      }
   ]
}