{"record":{"id":"38ac9dae8b927338","repo":"alibaba/nacos","slug":"invalid-json-yaml-format","errorCode":null,"errorMessage":"Invalid JSON/YAML format","messagePattern":"Invalid JSON/YAML format","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"console-ui/src/pages/AI/services/OpenApiService.js","lineNumber":73,"sourceCode":"    for (const [key, value] of Object.entries(obj)) {\n        result[key] = resolveRefs(value, root, visited);\n    }\n    return result;\n};\n\n// 校验格式并解析 OpenAPI\nexport const parseOpenAPI = async content => {\n    try {\n        // 自动识别 JSON/YAML 格式\n        let parsedContent;\n        try {\n            parsedContent = JSON.parse(content);\n        } catch (jsonError) {\n            // 尝试 YAML 解析\n            try {\n                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};","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/alibaba/nacos/blob/9b989acdf181d00898f2e8839257bb2b2a3cefe3/console-ui/src/pages/AI/services/OpenApiService.js#L55-L91","documentation":"Thrown by parseOpenAPI when the supplied content cannot be parsed as either JSON or YAML. The function first attempts JSON.parse; on failure it falls back to YAML.load; if both throw, this error is raised before any Swagger/OpenAPI document validation happens. It is a purely input-format error: the bytes are not syntactically valid JSON or YAML.","triggerScenarios":"A user pastes an OpenAPI document into the AI service tool importer with a stray trailing comma, mismatched brace, or unquoted key (invalid JSON) AND a tab-based indentation / special-character problem (invalid YAML). Copy-paste from a rich-text editor can inject non-breaking spaces or smart quotes that break both parsers.","commonSituations":"Importing a Swagger/OpenAPI spec file that was corrupted during copy-paste, downloaded with HTML entities, or encoded in a non-UTF-8 charset. Empty content string passed from a textarea. Content truncated by a file upload size limit, leaving half a JSON object.","solutions":["Validate the content with an external JSON/YAML linter (e.g. jsonlint.com or yamllint) before importing.","Ensure the input is a plain UTF-8 string with no smart quotes, non-breaking spaces, or BOM prefix; re-paste as plain text.","If pasting from a browser/IDE, disable rich-text paste or pipe through a sanitizer that strips zero-width characters.","Add a pre-check that strips a UTF-8 BOM and normalizes whitespace before calling parseOpenAPI."],"exampleFix":"// before\nconst doc = await parseOpenAPI(content);\n\n// after\nconst clean = content.replace(/^\\uFEFF/, '').replace(/[\\u200B-\\u200D\\uFEFF]/g, '');\nconst doc = await parseOpenAPI(clean);","handlingStrategy":"validation","validationCode":"function tryParse(content) {\n  try { JSON.parse(content); return { ok: true, format: 'json' }; } catch {}\n  try { YAML.load(content); return { ok: true, format: 'yaml' }; } catch (e) { return { ok: false, error: e }; }\n}\nconst check = tryParse(raw);\nif (!check.ok) { showToast('Content is neither valid JSON nor YAML'); }","typeGuard":"function isParsable(content: string): boolean {\n  try { JSON.parse(content); return true; } catch {}\n  try { YAML.load(content); return true; } catch { return false; }\n}","tryCatchPattern":"try {\n  const doc = await parseOpenAPI(cleanContent);\n} catch (e) {\n  if (/Invalid JSON\\/YAML format/.test(e.message)) { alert('Please paste valid JSON or YAML.'); }\n  else { throw e; }\n}","preventionTips":["Paste as plain text only","Strip BOM and zero-width chars","Run a linter before import"],"tags":["parsing","openapi","validation","json","yaml","input-format"],"backgroundTag":null,"analyzedSha":"9b989acdf181d00898f2e8839257bb2b2a3cefe3","analyzedAt":"2026-08-14T07:17:31.569Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}