Skip to content

Commit c1b2916

Browse files
authored
fix(bundle): don't look for a $ref pointer on a null value (#393)
1 parent dfbf4bb commit c1b2916

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

lib/bundle.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,12 @@ function crawl<S extends object = JSONSchema, O extends ParserOptions<S> = Parse
101101
crawl(obj, key, keyPath, keyPathFromRoot, indirections, inventory, $refs, options);
102102
}
103103

104-
if (value["$ref"]) {
105-
bundleOptions?.onBundle?.(value["$ref"], obj[key], obj as any, key);
104+
// We need to ensure that we have an object to work with here because we may be crawling
105+
// an `examples` schema and `value` may be nullish.
106+
if (value && typeof value === "object" && !Array.isArray(value)) {
107+
if ("$ref" in value) {
108+
bundleOptions?.onBundle?.(value["$ref"], obj[key], obj as any, key);
109+
}
106110
}
107111
}
108112
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
defintions:
2+
Pet:
3+
type: object
4+
properties:
5+
name:
6+
type: string
7+
breed:
8+
type: string
9+
enum: [dog, cat]
10+
example:
11+
name:
12+
breed: dog
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { describe, it } from "vitest";
2+
import { expect } from "vitest";
3+
import $RefParser from "../../../lib/index.js";
4+
import path from "../../utils/path";
5+
import dereferencedSchema from "./bundled";
6+
7+
describe("Bundling schema with nullish values", () => {
8+
it("should bundle correctly", async () => {
9+
const parser = new $RefParser();
10+
const schema = path.rel("test/specs/bundle-null-value/bundle-null-value-example.yaml");
11+
const bundled = await parser.bundle(schema);
12+
expect(bundled).to.deep.equal(dereferencedSchema);
13+
});
14+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const bundledSchema = {
2+
defintions: {
3+
Pet: {
4+
example: {
5+
breed: "dog",
6+
name: null,
7+
},
8+
properties: {
9+
breed: {
10+
enum: ["dog", "cat"],
11+
type: "string",
12+
},
13+
name: {
14+
type: "string",
15+
},
16+
},
17+
type: "object",
18+
},
19+
},
20+
};
21+
22+
export default bundledSchema;

0 commit comments

Comments
 (0)