Skip to content

fix: allow $ref pointers to point to a null value #374

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
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
12 changes: 12 additions & 0 deletions lib/pointer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import * as url from "./util/url.js";
import { JSONParserError, InvalidPointerError, MissingPointerError, isHandledError } from "./util/errors.js";
import type { JSONSchema } from "./types";

export const nullSymbol = Symbol('null');

const slashes = /\//g;
const tildes = /~/g;
const escapedSlash = /~1/g;
Expand Down Expand Up @@ -121,6 +123,16 @@ class Pointer<S extends object = JSONSchema, O extends ParserOptions<S> = Parser
continue;
}

// If the token we're looking for ended up not containing any slashes but is
// actually instead pointing to an existing `null` value then we should use that
// `null` value.
if (token in this.value && this.value[token] === null) {
// We use a `null` symbol for internal tracking to differntiate between a general `null`
// value and our expected `null` value.
this.value = nullSymbol;
continue;
}

this.value = null;

const path = this.$ref.path || "";
Expand Down
12 changes: 10 additions & 2 deletions lib/ref.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Pointer from "./pointer.js";
import Pointer, { nullSymbol } from "./pointer.js";
import type { JSONParserError, MissingPointerError, ParserError, ResolverError } from "./util/errors.js";
import { InvalidPointerError, isHandledError, normalizeError } from "./util/errors.js";
import { safePointerToPath, stripHash, getHash } from "./util/url.js";
Expand Down Expand Up @@ -119,7 +119,12 @@ class $Ref<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOpt
resolve(path: string, options?: O, friendlyPath?: string, pathFromRoot?: string) {
const pointer = new Pointer<S, O>(this, path, friendlyPath);
try {
return pointer.resolve(this.value, options, pathFromRoot);
const resolved = pointer.resolve(this.value, options, pathFromRoot);
if (resolved.value === nullSymbol) {
resolved.value = null;
}

return resolved;
} catch (err: any) {
if (!options || !options.continueOnError || !isHandledError(err)) {
throw err;
Expand Down Expand Up @@ -148,6 +153,9 @@ class $Ref<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOpt
set(path: string, value: any) {
const pointer = new Pointer(this, path);
this.value = pointer.set(this.value, value);
if (this.value === nullSymbol) {
this.value = null;
}
}

/**
Expand Down
14 changes: 14 additions & 0 deletions test/specs/dereference-null-ref/dereference-null-ref.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 dereferenced from "./dereferenced.js";

describe("dereferencing a `$ref` that points to a `null` value", () => {
it("should dereference successfully", async () => {
const parser = new $RefParser();
const schema = await parser.dereference(path.rel("test/specs/dereference-null-ref/dereference-null-ref.yaml"));
expect(schema).to.equal(parser.schema);
expect(schema).to.deep.equal(dereferenced);
});
});
8 changes: 8 additions & 0 deletions test/specs/dereference-null-ref/dereference-null-ref.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
components:
examples:
product:
value:
data:
admission:
$ref: "#/components/examples/product/value/data/pas"
pas: null
14 changes: 14 additions & 0 deletions test/specs/dereference-null-ref/dereferenced.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default {
components: {
examples: {
product: {
value: {
data: {
admission: null,
pas: null,
},
},
},
},
},
};