alibaba/nacos · error · Error
File format invalid
Error message
File format invalid
What it means
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.
Source
Thrown at console-ui/src/pages/AI/services/OpenApiService.js:89
parsedContent = YAML.load(content);
} catch (yamlError) {
throw new Error('Invalid JSON/YAML format');
}
}
parsedContent = resolveRefs(parsedContent, parsedContent);
if (parsedContent.swagger) {
const converted = await swagger2openapi.convertObj(parsedContent, {});
return converted.openapi;
}
// 验证 OpenAPI 3.x 文档
if (parsedContent.openapi) {
// 可以添加更多验证逻辑
return parsedContent;
}
} catch (e) {
console.error('解析失败:', e);
throw new Error('File format invalid');
}
};
// 从 OpenAPI 提取工具逻辑 (之前在 Swagger2Tools.js 中, 但 ShowTools.js 也有部分转换逻辑)
// 这里的逻辑主要是 ShowTools.js 中 handleConfirm 部分的复杂转换
export const transformToolsFromConfig = (config) => {
// 提取 OpenAPI 顶层的 securitySchemes
const securitySchemes = Array.isArray(config?.server?.securitySchemes)
? config.server.securitySchemes
: [];
const toolsMeta = config.tools.reduce((acc, tool) => {
const argsPosition = tool.args.reduce((acc, arg) => {
acc[arg.name] = arg.position;
return acc;
}, {});
acc[tool.name] = {
enabled: true,View on GitHub (pinned to 9b989acdf1)
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.
Example fix
// before
} catch (e) {
console.error('解析失败:', e);
throw new Error('File format invalid');
}
// after
} catch (e) {
console.error('解析失败:', e);
const reason = e?.message || 'unknown';
throw new Error(`File format invalid: ${reason}`);
} Defensive patterns
Strategy: validation
Validate before calling
function looksLikeSpec(parsed) {
return Boolean(parsed && (parsed.openapi || parsed.swagger));
}
const parsed = YAML.load(content);
if (!looksLikeSpec(parsed)) { throw new Error('Not a Swagger/OpenAPI document'); } Type guard
function isOpenApiDoc(v: unknown): v is { openapi: string } | { swagger: string } {
return typeof v === 'object' && v !== null && ('openapi' in v || 'swagger' in v);
} Try / catch
try { const doc = await parseOpenAPI(content); } catch (e) {
console.error(e); // underlying cause is logged
if (/File format invalid/.test(e.message)) { alert('Provide a Swagger 2.0 or OpenAPI 3.x document.'); }
} Prevention
- Confirm top-level swagger/openapi version key
- Validate with swagger-cli before import
- Upload the correct file
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- File format invalid: not a valid OpenAPI or Swagger document
- Invalid namespaceId: {namespaceId}
- Param 'serviceName' is illegal, it should be format as 'grou
- Param 'serviceName' is illegal, groupName can't be empty
- Invalid JSON/YAML format
AI-assisted analysis of alibaba/nacos@9b989acdf1 (2026-08-14).
Data as JSON: /api/errors/32446cd233d36ee2.
Report an issue: GitHub.