diff --git a/lib/bundle.ts b/lib/bundle.ts index d062efd9..520321f6 100644 --- a/lib/bundle.ts +++ b/lib/bundle.ts @@ -101,8 +101,12 @@ function crawl = 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); + } } } } diff --git a/test/specs/bundle-null-value/bundle-null-value-example.yaml b/test/specs/bundle-null-value/bundle-null-value-example.yaml new file mode 100644 index 00000000..d2417270 --- /dev/null +++ b/test/specs/bundle-null-value/bundle-null-value-example.yaml @@ -0,0 +1,12 @@ +defintions: + Pet: + type: object + properties: + name: + type: string + breed: + type: string + enum: [dog, cat] + example: + name: + breed: dog diff --git a/test/specs/bundle-null-value/bundle-null-value.spec.ts b/test/specs/bundle-null-value/bundle-null-value.spec.ts new file mode 100644 index 00000000..4068f7ce --- /dev/null +++ b/test/specs/bundle-null-value/bundle-null-value.spec.ts @@ -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); + }); +}); diff --git a/test/specs/bundle-null-value/bundled.ts b/test/specs/bundle-null-value/bundled.ts new file mode 100644 index 00000000..39cc14a7 --- /dev/null +++ b/test/specs/bundle-null-value/bundled.ts @@ -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;