{"record":{"id":"2400c2ab61022d5d","repo":"alibaba/nacos","slug":"invalid-json-yaml-format-2400c2","errorCode":null,"errorMessage":"Invalid JSON/YAML format","messagePattern":"Invalid JSON/YAML format","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"console-ui-next/src/utils/openapi/parseOpenApi.ts","lineNumber":62,"sourceCode":"  for (const [key, value] of Object.entries(obj)) {\n    result[key] = resolveRefs(value, root, visited);\n  }\n  return result;\n}\n\n/**\n * Parse OpenAPI/Swagger content (JSON or YAML) and return an OpenAPI 3.x document.\n */\nexport async function parseOpenAPI(content: string): Promise<any> {\n  let parsed: any;\n\n  try {\n    parsed = JSON.parse(content);\n  } catch {\n    try {\n      parsed = YAML.load(content);\n    } catch {\n      throw new Error('Invalid JSON/YAML format');\n    }\n  }\n\n  parsed = resolveRefs(parsed, parsed);\n\n  // Swagger 2.x -> OpenAPI 3.x\n  if (parsed.swagger) {\n    const converted = await swagger2openapi.convertObj(parsed, {});\n    return converted.openapi;\n  }\n\n  if (parsed.openapi) {\n    return parsed;\n  }\n\n  throw new Error('File format invalid: not a valid OpenAPI or Swagger document');\n}\n","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/alibaba/nacos/blob/9b989acdf181d00898f2e8839257bb2b2a3cefe3/console-ui-next/src/utils/openapi/parseOpenApi.ts#L44-L80","documentation":"Thrown by parseOpenAPI() when the supplied content cannot be parsed as JSON (JSON.parse throws) AND cannot be parsed as YAML (YAML.load throws). The function tries JSON first, then falls back to YAML; only when both fail does it throw 'Invalid JSON/YAML format'. This is a content-level parse failure, before any OpenAPI structural validation.","triggerScenarios":"Passing a string that is neither valid JSON nor valid YAML — e.g. plain prose, a fragment of code, a binary/base64 blob, or a file with syntax errors (unbalanced braces, bad indentation, stray tabs). Reached via the OpenAPI import flow in the console.","commonSituations":"User uploads a non-spec file (README, CSV, screenshot-dump text). YAML with tab indentation (YAML forbids tabs). Truncated/corrupted download. JSON with trailing commas or single quotes.","solutions":["Validate the file is actually an OpenAPI/Swagger spec before uploading.","Run the content through a JSON/YAML linter to find the syntax error.","For YAML, replace tabs with spaces and check indentation.","Catch the error and prompt the user to re-upload a corrected file."],"exampleFix":"// before\nconst doc = await parseOpenAPI(content);\n\n// after\ntry {\n  const doc = await parseOpenAPI(content);\n} catch (e) {\n  if (e.message === 'Invalid JSON/YAML format') {\n    throw new Error('The uploaded file is not valid JSON or YAML. Please check the syntax and try again.');\n  }\n  throw e;\n}","handlingStrategy":"validation","validationCode":"function isParsableSpec(content: string): boolean {\n  try { JSON.parse(content); return true; } catch {}\n  try { YAML.load(content); return true; } catch {}\n  return false;\n}","typeGuard":null,"tryCatchPattern":"try {\n  const doc = await parseOpenAPI(content);\n} catch (e) {\n  if (e.message === 'Invalid JSON/YAML format') {\n    setError('The file is not valid JSON or YAML. Check syntax and re-upload.');\n    return;\n  }\n  throw e;\n}","preventionTips":["Lint the file as JSON or YAML before importing.","For YAML, ensure spaces (not tabs) for indentation.","Reject obviously non-text files (binary, images) at the upload boundary.","Show parse errors with line numbers when possible."],"tags":["openapi","swagger","typescript","json","yaml","parsing"],"backgroundTag":null,"analyzedSha":"9b989acdf181d00898f2e8839257bb2b2a3cefe3","analyzedAt":"2026-08-14T07:17:31.569Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}