Skip to content

fix(bundle): don't look for a $ref pointer on a null value #393

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 1 commit into from
Jul 15, 2025
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
8 changes: 6 additions & 2 deletions lib/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,12 @@ function crawl<S extends object = JSONSchema, O extends ParserOptions<S> = Parse
crawl(obj, key, keyPath, keyPathFromRoot, indirections, inventory, $refs, options);
}

if (value["$ref"]) {
bundleOptions?.onBundle?.(value["$ref"], obj[key], obj as any, key);
// We need to ensure that we have an object to work with here because we may be crawling
// an `examples` schema and `value` may be nullish.
if (value && typeof value === "object" && !Array.isArray(value)) {
if ("$ref" in value) {
bundleOptions?.onBundle?.(value["$ref"], obj[key], obj as any, key);
}
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions test/specs/bundle-null-value/bundle-null-value-example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defintions:
Pet:
type: object
properties:
name:
type: string
breed:
type: string
enum: [dog, cat]
example:
name:
breed: dog
14 changes: 14 additions & 0 deletions test/specs/bundle-null-value/bundle-null-value.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, it } from "vitest";
import { expect } from "vitest";
import $RefParser from "../../../lib/index.js";
import path from "../../utils/path";
import dereferencedSchema from "./bundled";

describe("Bundling schema with nullish values", () => {
it("should bundle correctly", async () => {
const parser = new $RefParser();
const schema = path.rel("test/specs/bundle-null-value/bundle-null-value-example.yaml");
const bundled = await parser.bundle(schema);
expect(bundled).to.deep.equal(dereferencedSchema);
});
});
22 changes: 22 additions & 0 deletions test/specs/bundle-null-value/bundled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const bundledSchema = {
defintions: {
Pet: {
example: {
breed: "dog",
name: null,
},
properties: {
breed: {
enum: ["dog", "cat"],
type: "string",
},
name: {
type: "string",
},
},
type: "object",
},
},
};

export default bundledSchema;
Loading