Skip to content

Commit f6fdd2f

Browse files
authored
Improve YAML vs JSON parsing (#1348)
* Improve YAML vs JSON parsing * Clean dist dir
1 parent 6f078c1 commit f6fdd2f

File tree

3 files changed

+13
-46
lines changed

3 files changed

+13
-46
lines changed

.changeset/brown-trainers-wink.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-typescript": patch
3+
---
4+
5+
Improve YAML vs JSON parsing

packages/openapi-typescript/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
},
4242
"scripts": {
4343
"build": "run-s -s build:*",
44+
"build:clean": "del dist",
4445
"build:esm": "tsc -p tsconfig.build.json",
4546
"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;\"",
4647
"dev": "tsc -p tsconfig.build.json --watch",

packages/openapi-typescript/src/load.ts

+7-46
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,9 @@ interface SchemaMap {
1414
const EXT_RE = /\.(yaml|yml|json)#?\/?/i;
1515
export const VIRTUAL_JSON_URL = `file:///_json`; // fake URL reserved for dynamic JSON
1616

17-
function parseYAML(schema: string) {
18-
try {
19-
return yaml.load(schema);
20-
} catch (err) {
21-
error(`YAML: ${String(err)}`);
22-
process.exit(1);
23-
}
24-
}
25-
26-
function parseJSON(schema: string) {
27-
try {
28-
return JSON.parse(schema);
29-
} catch (err) {
30-
error(`JSON: ${String(err)}`);
31-
process.exit(1);
32-
}
17+
/** parse OpenAPI schema s YAML or JSON */
18+
function parseSchema(source: string) {
19+
return source.trim().startsWith("{") ? JSON.parse(source) : yaml.load(source);
3320
}
3421

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

111-
const ext = path.extname(schema.pathname).toLowerCase();
112-
11398
// remote
11499
if (schema.protocol.startsWith("http")) {
115100
const headers: Record<string, string> = { "User-Agent": "openapi-typescript" };
@@ -126,33 +111,13 @@ export default async function load(schema: URL | Subschema | Readable, options:
126111
method: (options.httpMethod as Dispatcher.HttpMethod) || "GET",
127112
headers,
128113
});
129-
const contentType = res.headers.get("content-type");
130-
if (ext === ".json" || contentType?.includes("json")) {
131-
options.schemas[schemaID] = {
132-
hint,
133-
schema: parseJSON(await res.text()),
134-
};
135-
} else if (ext === ".yaml" || ext === ".yml" || contentType?.includes("yaml")) {
136-
options.schemas[schemaID] = {
137-
hint,
138-
schema: parseYAML(await res.text()) as any, // eslint-disable-line @typescript-eslint/no-explicit-any
139-
};
140-
}
114+
const contents = await res.text();
115+
options.schemas[schemaID] = { hint, schema: parseSchema(contents) };
141116
}
142117
// local file
143118
else {
144119
const contents = fs.readFileSync(schema, "utf8");
145-
if (ext === ".yaml" || ext === ".yml") {
146-
options.schemas[schemaID] = {
147-
hint,
148-
schema: parseYAML(contents) as any, // eslint-disable-line @typescript-eslint/no-explicit-any
149-
};
150-
} else if (ext === ".json") {
151-
options.schemas[schemaID] = {
152-
hint,
153-
schema: parseJSON(contents),
154-
};
155-
}
120+
options.schemas[schemaID] = { hint, schema: parseSchema(contents) };
156121
}
157122
}
158123
// 1b. Readable stream
@@ -169,11 +134,7 @@ export default async function load(schema: URL | Subschema | Readable, options:
169134
resolve(content.trim());
170135
});
171136
});
172-
// if file starts with '{' assume JSON
173-
options.schemas[schemaID] = {
174-
hint: "OpenAPI3",
175-
schema: contents.startsWith("{") ? parseJSON(contents) : parseYAML(contents),
176-
};
137+
options.schemas[schemaID] = { hint: "OpenAPI3", schema: parseSchema(contents) };
177138
}
178139
// 1c. inline
179140
else if (typeof schema === "object") {

0 commit comments

Comments
 (0)