Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

fix: replace custom YAML parser with js-yaml for correct OpenAPI spec parsing

diegosouzapw21d6a0a2dd7d
diff --git a/src/app/api/openapi/spec/route.ts b/src/app/api/openapi/spec/route.ts
index c7cb97ac0f..f6ed412df7 100644
--- a/src/app/api/openapi/spec/route.ts
+++ b/src/app/api/openapi/spec/route.ts
@@ -1,95 +1,12 @@
/**
* API: OpenAPI Spec
- * GET — returns the parsed openapi.yaml as JSON
+ * GET — returns the parsed openapi.yaml as structured JSON catalog
*/
import { NextResponse } from "next/server";
import fs from "fs";
import path from "path";
-
-// Simple YAML parser for OpenAPI spec (handles common YAML patterns)
-function parseYamlLite(content: string): any {
- try {
- // Try JSON first (in case file is already JSON)
- return JSON.parse(content);
- } catch {
- // Fall through to YAML parsing
- }
-
- const lines = content.split("\n");
- const result: any = {};
- const stack: { indent: number; obj: any; key: string }[] = [{ indent: -1, obj: result, key: "" }];
-
- for (const rawLine of lines) {
- const line = rawLine.replace(/\r$/, "");
- if (!line.trim() || line.trim().startsWith("#")) continue;
-
- const indent = line.search(/\S/);
- const trimmed = line.trim();
-
- // Pop stack to find parent
- while (stack.length > 1 && stack[stack.length - 1].indent >= indent) {
- stack.pop();
- }
- const parent = stack[stack.length - 1].obj;
-
- // Array item
- if (trimmed.startsWith("- ")) {
- const parentKey = stack[stack.length - 1].key;
- if (parentKey && !Array.isArray(parent[parentKey])) {
- parent[parentKey] = [];
- }
- const val = trimmed.slice(2).trim();
- if (val.includes(": ")) {
- const obj: any = {};
- const [k, ...v] = val.split(": ");
- obj[k.trim()] = parseYamlValue(v.join(": ").trim());
- const arr = parentKey ? parent[parentKey] : parent;
- if (Array.isArray(arr)) arr.push(obj);
- } else {
- const arr = parentKey ? parent[parentKey] : parent;
- if (Array.isArray(arr)) arr.push(parseYamlValue(val));
- }
- continue;
- }
-
- // Key-value pair
- const colonIdx = trimmed.indexOf(":");
- if (colonIdx > 0) {
- const key = trimmed.slice(0, colonIdx).trim();
- const val = trimmed.slice(colonIdx + 1).trim();
- if (val) {
- parent[key] = parseYamlValue(val);
- } else {
- parent[key] = {};
- stack.push({ indent, obj: parent, key });
- }
- }
- }
-
- return result;
-}
-
-function parseYamlValue(val: string): any {
- if (val === "true") return true;
- if (val === "false") return false;
- if (val === "null") return null;
- if (/^\d+$/.test(val)) return parseInt(val, 10);
- if (/^\d+\.\d+$/.test(val)) return parseFloat(val);
- // Remove surrounding quotes
- if ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith("'") && val.endsWith("'"))) {
- return val.slice(1, -1);
- }
- // JSON array inline
- if (val.startsWith("[") && val.endsWith("]")) {
- try {
- return JSON.parse(val);
- } catch {
- /* use as-is */
- }
- }
- return val;
-}
+import yaml from "js-yaml";
let cachedSpec: { data: any; mtime: number } | null = null;
@@ -103,10 +20,7 @@ export async function GET() {
let specPath = "";
for (const p of candidates) {
- if (fs.existsSync(p)) {
- specPath = p;
- break;
- }
+ if (fs.existsSync(p)) { specPath = p; break; }
}
if (!specPath) {
@@ -122,15 +36,13 @@ export async function GET() {
}
const content = fs.readFileSync(specPath, "utf-8");
-
- // Group endpoints by tag for the catalog
- const raw = parseYamlLite(content);
+ const raw: any = yaml.load(content);
// Build a structured catalog
const catalog: any = {
info: raw.info || {},
servers: raw.servers || [],
- tags: raw.tags || [],
+ tags: Array.isArray(raw.tags) ? raw.tags : [],
endpoints: [] as any[],
schemas: Object.keys(raw.components?.schemas || {}),
};
@@ -144,7 +56,7 @@ export async function GET() {
catalog.endpoints.push({
method: method.toUpperCase(),
path: pathStr,
- tags: spec.tags || [],
+ tags: Array.isArray(spec.tags) ? spec.tags : [],
summary: spec.summary || "",
description: spec.description || "",
security: spec.security ? true : false,