APIDevTools / json-schema-ref-parser

Parse, Resolve, and Dereference JSON Schema $ref pointers in Node and browsers
https://apitools.dev/json-schema-ref-parser
MIT License
952 stars 227 forks source link

onDereference callback mentioned in documentation, yet missing from the latest release #287

Closed gdaszuta closed 1 year ago

gdaszuta commented 1 year ago

So yeah, everything is in the title: https://apitools.dev/json-schema-ref-parser/docs/options.html mentions options.reference.onDereference callback, yet it's missing from latest release:


$ grep '"version"' node_modules/@apidevtools/json-schema-ref-parser/package.json 
  "version": "9.0.9",
$ diff -u node_modules/@apidevtools/json-schema-ref-parser/lib/dereference.js ../../json-schema-ref-parser/lib/dereference.js
--- node_modules/@apidevtools/json-schema-ref-parser/lib/dereference.js 2022-11-03 09:52:16.125999000 +0100
+++ ../../json-schema-ref-parser/lib/dereference.js 2022-11-02 18:45:29.807704592 +0100
@@ -41,8 +41,10 @@
     circular: false
   };

+  let isExcludedPath = options.dereference.excludedPathMatcher;
+
   if (options.dereference.circular === "ignore" || !processedObjects.has(obj)) {
-    if (obj && typeof obj === "object" && !ArrayBuffer.isView(obj)) {
+    if (obj && typeof obj === "object" && !ArrayBuffer.isView(obj) && !isExcludedPath(pathFromRoot)) {
       parents.add(obj);
       processedObjects.add(obj);

@@ -55,6 +57,11 @@
         for (const key of Object.keys(obj)) {
           let keyPath = Pointer.join(path, key);
           let keyPathFromRoot = Pointer.join(pathFromRoot, key);
+
+          if (isExcludedPath(keyPathFromRoot)) {
+            continue;
+          }
+
           let value = obj[key];
           let circular = false;

@@ -64,6 +71,9 @@
             // Avoid pointless mutations; breaks frozen objects to no profit
             if (obj[key] !== dereferenced.value) {
               obj[key] = dereferenced.value;
+              if (options.dereference.onDereference) {
+                options.dereference.onDereference(value.$ref, obj[key]);
+              }
             }
           }
           else {
philsturgeon commented 1 year ago

That method was introduced by https://github.com/APIDevTools/json-schema-ref-parser/pull/281 and the release has not yet happened as a few bit PRs are still failing tests. It'll be available when those PRs are solved so if you can pitch in a fix there that'll be awesome.