{"record":{"id":"c03350272363c4c2","repo":"YMFE/yapi","slug":"context-caseid","errorCode":null,"errorMessage":"context.caseId 不能被赋值","messagePattern":"context\\.caseId 不能被赋值","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"common/postmanLib.js","lineNumber":274,"sourceCode":"    get href() {\n      return urlObj.href;\n    },\n    set href(val) {\n      throw new Error('context.href 不能被赋值');\n    },\n    get hostname() {\n      return urlObj.hostname;\n    },\n    set hostname(val) {\n      throw new Error('context.hostname 不能被赋值');\n    },\n\n    get caseId() {\n      return options.caseId;\n    },\n\n    set caseId(val) {\n      throw new Error('context.caseId 不能被赋值');\n    },\n\n    method: options.method,\n    pathname: urlObj.pathname,\n    query: query,\n    requestHeader: options.headers || {},\n    requestBody: options.data,\n    promise: false,\n    storage: await getStorage(taskId)\n  };\n\n  Object.assign(context, commonContext)\n\n  context.utils = Object.freeze({\n    _: _,\n    CryptoJS: CryptoJS,\n    jsrsasign: jsrsasign,\n    base64: utils.base64,","sourceCodeStart":256,"sourceCodeEnd":292,"githubUrl":"https://github.com/YMFE/yapi/blob/59bade3a8a43e7db077d38a4b0c7c584f30ddf8c/common/postmanLib.js#L256-L292","documentation":"context.caseId in the test-script sandbox is read-only; its setter exists only to throw. caseId is supplied by the runner via options and must not be overwritten by scripts.","triggerScenarios":"A script assigns context.caseId = <value> during a case run.","commonSituations":"Scripts trying to fake or reuse another case's id for cross-case assertions; copy-pasted template code mutating context fields.","solutions":["Remove the context.caseId assignment from the script","Pass the intended caseId through the runner options, not by mutation","Use context.caseId only for reading"],"exampleFix":"// before\ncontext.caseId = 42;\n// after\nconst id = context.caseId;","handlingStrategy":"type-guard","validationCode":"if (typeof context.caseId === 'number' && context.caseId !== expectedCaseId) throw new Error('running wrong case');","typeGuard":"function isReadOnlyCtxProp(obj, key){ const d = Object.getOwnPropertyDescriptor(obj, key); return !!d && d.get && d.set && d.set.toString().includes('throw'); }","tryCatchPattern":"try {\n  context.caseId = id;\n} catch (e) {\n  console.warn('caseId is set by the runner; cannot be overridden');\n}","preventionTips":["Read context.caseId; never write it","Set case ids via the runner/API, not scripts","Lint test scripts for context assignments","Keep per-case logic within one case instead of rewriting ids"],"tags":["script-sandbox","immutable-context","postman-lib"],"backgroundTag":"readonly-property-assignment","analyzedSha":"59bade3a8a43e7db077d38a4b0c7c584f30ddf8c","analyzedAt":"2026-08-29T08:45:22.203Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}