{"record":{"id":"32446cd233d36ee2","repo":"alibaba/nacos","slug":"file-format-invalid","errorCode":null,"errorMessage":"File format invalid","messagePattern":"File format invalid","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"console-ui/src/pages/AI/services/OpenApiService.js","lineNumber":89,"sourceCode":"                parsedContent = YAML.load(content);\n            } catch (yamlError) {\n                throw new Error('Invalid JSON/YAML format');\n            }\n        }\n        parsedContent = resolveRefs(parsedContent, parsedContent);\n        if (parsedContent.swagger) {\n            const converted = await swagger2openapi.convertObj(parsedContent, {});\n            return converted.openapi;\n        }\n\n        // 验证 OpenAPI 3.x 文档\n        if (parsedContent.openapi) {\n            // 可以添加更多验证逻辑\n            return parsedContent;\n        }\n    } catch (e) {\n        console.error('解析失败:', e);\n        throw new Error('File format invalid');\n    }\n};\n\n// 从 OpenAPI 提取工具逻辑 (之前在 Swagger2Tools.js 中, 但 ShowTools.js 也有部分转换逻辑)\n// 这里的逻辑主要是 ShowTools.js 中 handleConfirm 部分的复杂转换\nexport const transformToolsFromConfig = (config) => {\n    // 提取 OpenAPI 顶层的 securitySchemes\n    const securitySchemes = Array.isArray(config?.server?.securitySchemes)\n        ? config.server.securitySchemes\n        : [];\n\n    const toolsMeta = config.tools.reduce((acc, tool) => {\n        const argsPosition = tool.args.reduce((acc, arg) => {\n            acc[arg.name] = arg.position;\n            return acc;\n        }, {});\n        acc[tool.name] = {\n            enabled: true,","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/alibaba/nacos/blob/9b989acdf181d00898f2e8839257bb2b2a3cefe3/console-ui/src/pages/AI/services/OpenApiService.js#L71-L107","documentation":"Thrown by the outer catch of parseOpenAPI when the content parsed successfully as JSON or YAML but is neither a Swagger document (no top-level `swagger` key) nor an OpenAPI 3.x document (no top-level `openapi` key). It also catches any unexpected exception from resolveRefs or swagger2openapi.convertObj. The message is a generic wrapper; the real cause is logged via console.error immediately before the throw.","triggerScenarios":"User imports a valid JSON/YAML file that is not an API spec (e.g. a package.json, a config file, or a JSON Schema). User imports an OpenAPI 3.1 doc whose `openapi` key is misspelled or nested. swagger2openapi fails to convert a malformed Swagger 2.0 document.","commonSituations":"Uploading the wrong file. Providing a partial spec fragment that lacks the `openapi`/`swagger` version field. A Swagger 2.0 doc with circular $refs or unsupported constructs that crash swagger2openapi.","solutions":["Confirm the file has a top-level `openapi: 3.x.x` or `swagger: \"2.0\"` field.","Check the browser console for the logged underlying error (the line `console.error('解析失败:', e)`) to see whether it is a missing-version or a conversion failure.","If it is a Swagger 2.0 conversion issue, pre-validate the doc with swagger-cli or openapi-generator validate.","Provide a clearer error to end users by catching and re-throwing with the original cause attached."],"exampleFix":"// before\n} catch (e) {\n  console.error('解析失败:', e);\n  throw new Error('File format invalid');\n}\n\n// after\n} catch (e) {\n  console.error('解析失败:', e);\n  const reason = e?.message || 'unknown';\n  throw new Error(`File format invalid: ${reason}`);\n}","handlingStrategy":"validation","validationCode":"function looksLikeSpec(parsed) {\n  return Boolean(parsed && (parsed.openapi || parsed.swagger));\n}\nconst parsed = YAML.load(content);\nif (!looksLikeSpec(parsed)) { throw new Error('Not a Swagger/OpenAPI document'); }","typeGuard":"function isOpenApiDoc(v: unknown): v is { openapi: string } | { swagger: string } {\n  return typeof v === 'object' && v !== null && ('openapi' in v || 'swagger' in v);\n}","tryCatchPattern":"try { const doc = await parseOpenAPI(content); } catch (e) {\n  console.error(e); // underlying cause is logged\n  if (/File format invalid/.test(e.message)) { alert('Provide a Swagger 2.0 or OpenAPI 3.x document.'); }\n}","preventionTips":["Confirm top-level swagger/openapi version key","Validate with swagger-cli before import","Upload the correct file"],"tags":["openapi","validation","format","swagger"],"backgroundTag":null,"analyzedSha":"9b989acdf181d00898f2e8839257bb2b2a3cefe3","analyzedAt":"2026-08-14T07:17:31.569Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}