Skip to content

Improve YAML vs JSON parsing #1348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/brown-trainers-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-typescript": patch
---

Improve YAML vs JSON parsing
1 change: 1 addition & 0 deletions packages/openapi-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
},
"scripts": {
"build": "run-s -s build:*",
"build:clean": "del dist",
"build:esm": "tsc -p tsconfig.build.json",
"build:cjs": "esbuild --bundle --platform=node --target=es2019 --outfile=dist/index.cjs --external:js-yaml --external:undici src/index.ts --footer:js=\"module.exports = module.exports.default;\"",
"dev": "tsc -p tsconfig.build.json --watch",
Expand Down
53 changes: 7 additions & 46 deletions packages/openapi-typescript/src/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,9 @@ interface SchemaMap {
const EXT_RE = /\.(yaml|yml|json)#?\/?/i;
export const VIRTUAL_JSON_URL = `file:///_json`; // fake URL reserved for dynamic JSON

function parseYAML(schema: string) {
try {
return yaml.load(schema);
} catch (err) {
error(`YAML: ${String(err)}`);
process.exit(1);
}
}

function parseJSON(schema: string) {
try {
return JSON.parse(schema);
} catch (err) {
error(`JSON: ${String(err)}`);
process.exit(1);
}
/** parse OpenAPI schema s YAML or JSON */
function parseSchema(source: string) {
return source.trim().startsWith("{") ? JSON.parse(source) : yaml.load(source);
}

export function resolveSchema(filename: string): URL {
Expand Down Expand Up @@ -108,8 +95,6 @@ export default async function load(schema: URL | Subschema | Readable, options:
}
options.urlCache.add(schemaID);

const ext = path.extname(schema.pathname).toLowerCase();

// remote
if (schema.protocol.startsWith("http")) {
const headers: Record<string, string> = { "User-Agent": "openapi-typescript" };
Expand All @@ -126,33 +111,13 @@ export default async function load(schema: URL | Subschema | Readable, options:
method: (options.httpMethod as Dispatcher.HttpMethod) || "GET",
headers,
});
const contentType = res.headers.get("content-type");
if (ext === ".json" || contentType?.includes("json")) {
options.schemas[schemaID] = {
hint,
schema: parseJSON(await res.text()),
};
} else if (ext === ".yaml" || ext === ".yml" || contentType?.includes("yaml")) {
options.schemas[schemaID] = {
hint,
schema: parseYAML(await res.text()) as any, // eslint-disable-line @typescript-eslint/no-explicit-any
};
}
const contents = await res.text();
options.schemas[schemaID] = { hint, schema: parseSchema(contents) };
}
// local file
else {
const contents = fs.readFileSync(schema, "utf8");
if (ext === ".yaml" || ext === ".yml") {
options.schemas[schemaID] = {
hint,
schema: parseYAML(contents) as any, // eslint-disable-line @typescript-eslint/no-explicit-any
};
} else if (ext === ".json") {
options.schemas[schemaID] = {
hint,
schema: parseJSON(contents),
};
}
options.schemas[schemaID] = { hint, schema: parseSchema(contents) };
}
}
// 1b. Readable stream
Expand All @@ -169,11 +134,7 @@ export default async function load(schema: URL | Subschema | Readable, options:
resolve(content.trim());
});
});
// if file starts with '{' assume JSON
options.schemas[schemaID] = {
hint: "OpenAPI3",
schema: contents.startsWith("{") ? parseJSON(contents) : parseYAML(contents),
};
options.schemas[schemaID] = { hint: "OpenAPI3", schema: parseSchema(contents) };
}
// 1c. inline
else if (typeof schema === "object") {
Expand Down