-
-
Notifications
You must be signed in to change notification settings - Fork 233
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
fix: allow $ref
pointers to point to a null
value
#374
fix: allow $ref
pointers to point to a null
value
#374
Conversation
Pull Request Test Coverage Report for Build 13576865917Details
💛 - Coveralls |
This is a great point. I think we use Maybe if we change |
Actually wait, is that a valid schema? It might not be a valid schem. In openapi 3.x, to make a field nullable you set the schema to be |
Yeah that is a valid schema. In this case the thing isn't formally As far as resolving the resolver is able to handle this case without my fix if It's just when this value we want to splice in is
|
Interesting, that makes sense. If we swap out the null checks with a null symbol just for internal tracking that might work. I like your solution for now, I'm fairly open to merging this in. I'll think about it but it feels low risk |
Doing it as |
This diff --git a/lib/pointer.ts b/lib/pointer.ts
index f1660a5..d7fa143 100644
--- a/lib/pointer.ts
+++ b/lib/pointer.ts
@@ -125,7 +125,7 @@ class Pointer<S extends object = JSONSchema, O extends ParserOptions<S> = Parser
// actually instead pointing to an existing `null` value then we should use that
// `null` value.
if (token in this.value && this.value[token] === null) {
- this.value = null;
+ this.value = Symbol('null');
continue;
}
diff --git a/lib/ref.ts b/lib/ref.ts
index 0ad40b4..f84f8f1 100644
--- a/lib/ref.ts
+++ b/lib/ref.ts
@@ -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 (typeof resolved.value === 'symbol' && resolved.value.toString() === 'Symbol(null)') {
+ resolved.value = null;
+ }
+
+ return resolved;
} catch (err: any) {
if (!options || !options.continueOnError || !isHandledError(err)) {
throw err;
@@ -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 (typeof this.value === 'symbol' && this.value.toString() === 'Symbol(null)') {
+ this.value = null;
+ }
}
/** |
For this one I was thinking of having an explicit symbol like export const nullSymbol = Symbol('null') and then checking if (resolved.value === nullSymbol) {
resolved.value = null;
} |
🙃 of course, time for afternoon coffee apparently. I've just pushed that refactor up! |
Awesome, this is great. Merged in, thanks for the great PR! |
🎉 This PR is included in version 11.9.3 🎉 The release is available on: Your semantic-release bot 📦🚀 |
I've run into a curious issue where the following schema throws a
MissingPointerError
exception:I'm having a hard time understanding why the
Pointer
class is running this substring lookup for if a token does exist but it'snull
, but because the token we're looking for is the last in the list in this block it never ends up running thefor
loop below this line to look for a substring -- resulting in it falling through to throw aMissingPointerError
.json-schema-ref-parser/lib/pointer.ts
Lines 108 to 122 in 5303da9
I've managed to fix this by doing a secondary "does this token exist and is it null? ok return that." check and that has resolved this problem in the dereferencer but I also don't know if this is the best way to fix this, or if it will have downstream effects (no tests break with it), or would be considered as a breaking change.
Changing line 108 to only check
this.value[token] === undefined
also fixes this issue but even though no tests break with that I don't know if it's the right fix.Looking for guidance here!