Skip to content

Fix js-yaml $ref #1146

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
May 25, 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/gold-berries-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-typescript": patch
---

Fix js-yaml $refs
34 changes: 25 additions & 9 deletions packages/openapi-typescript/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,31 @@ export function comment(text: string, indentLv?: number): string {
/** handle any valid $ref */
export function parseRef(ref: string): { filename: string; path: string[] } {
if (typeof ref !== "string") return { filename: ".", path: [] };
if (!ref.includes("#")) return { filename: ref, path: [] };
const [filename, path] = ref.split("#");
return {
filename: filename || ".",
path: path
.split("/") // split by special character
.filter((p) => !!p && p !== "properties") // remove empty parts and "properties" (gets flattened by TS)
.map(decodeRef), // decode encoded chars
};

// OpenAPI $ref
if (ref.includes("#/")) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was able to get a 2x speedup of parseRef out of refactoring it a little. Which is something, because this is used quite a lot.

const [filename, pathStr] = ref.split("#");
const parts = pathStr.split("/");
const path: string[] = [];
for (const part of parts) {
if (!part || part === "properties") continue; // remove empty parts and "properties" (gets flattened by TS)
path.push(decodeRef(part));
}
return { filename: filename || ".", path };
}
// js-yaml $ref
else if (ref.includes('["')) {
const parts = ref.split('["');
const path: string[] = [];
for (const part of parts) {
const sanitized = part.replace('"]', "").trim();
if (!sanitized || sanitized === "properties") continue;
path.push(sanitized);
}
return { filename: ".", path };
}
// remote $ref
return { filename: ref, path: [] };
}

/** Parse TS index */
Expand Down
6 changes: 6 additions & 0 deletions packages/openapi-typescript/test/utils.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { bench } from "vitest";
import { parseRef } from "../src/utils.js";

bench("parseRef", () => {
parseRef("#/test/schema-object");
});
21 changes: 11 additions & 10 deletions packages/openapi-typescript/test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { bench } from "vitest";
import { parseRef, tsIntersectionOf, tsUnionOf } from "../src/utils.js";

describe("utils", () => {
Expand Down Expand Up @@ -48,11 +49,7 @@ describe("utils", () => {
["identity for unknown type", ["unknown"], "unknown"],
["unknown for no types passed", [], "unknown"],
["parentheses around types with union", ["4", `string | number`], "4 & (string | number)"],
[
"parentheses around types with intersection",
["{ red: string }", "{ blue: string } & { green: string }"],
"{ red: string } & ({ blue: string } & { green: string })",
],
["parentheses around types with intersection", ["{ red: string }", "{ blue: string } & { green: string }"], "{ red: string } & ({ blue: string } & { green: string })"],
];

tests.forEach(([name, input, output]) => {
Expand All @@ -64,19 +61,23 @@ describe("utils", () => {

describe("parseRef", () => {
it("basic", () => {
expect(parseRef("#/test/schema-object")).toStrictEqual({ filename: ".", path: [ "test", "schema-object" ] });
expect(parseRef("#/test/schema-object")).toStrictEqual({ filename: ".", path: ["test", "schema-object"] });
});

it("double quote", () => {
expect(parseRef("#/test/\"")).toStrictEqual({ filename: ".", path: [ "test", '\\\"' ] });
expect(parseRef('#/test/"')).toStrictEqual({ filename: ".", path: ["test", '\\"'] });
});

it("escaped double quote", () => {
expect(parseRef("#/test/\\\"")).toStrictEqual({ filename: ".", path: [ "test", '\\\"' ] });
expect(parseRef('#/test/\\"')).toStrictEqual({ filename: ".", path: ["test", '\\"'] });
});

it("tilde escapes", () => {
expect(parseRef("#/test/~1~0")).toStrictEqual({ filename: ".", path: [ "test", "/~" ] });
expect(parseRef("#/test/~1~0")).toStrictEqual({ filename: ".", path: ["test", "/~"] });
});

it("js-yaml $ref", () => {
expect(parseRef('components["schemas"]["SchemaObject"]')).toStrictEqual({ filename: ".", path: ["components", "schemas", "SchemaObject"] });
});
});
});
});