Skip to content
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