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

[Feature Req] Additional property on `file` object #256

Closed JaiPe closed 6 months ago

JaiPe commented 2 years ago

Hi Great library, thanks for your hard work on this.

I was wondering if we could expose something akin to this change:

diff --git a/node_modules/@apidevtools/json-schema-ref-parser/lib/parse.js b/node_modules/@apidevtools/json-schema-ref-parser/lib/parse.js
index b9160df..48d56bd 100644
--- a/node_modules/@apidevtools/json-schema-ref-parser/lib/parse.js
+++ b/node_modules/@apidevtools/json-schema-ref-parser/lib/parse.js
@@ -18,8 +18,13 @@ module.exports = parse;
  * The promise resolves with the parsed file contents, NOT the raw (Buffer) contents.
  */
 async function parse (path, $refs, options) {
-  // Remove the URL fragment, if any
-  path = url.stripHash(path);
+  let hashIndex = path.indexOf("#");
+  let hash = "";
+  if (hashIndex >= 0) {
+    hash = path.substr(hashIndex);
+    // Remove the URL fragment, if any
+    path = path.substr(0, hashIndex);
+  }

   // Add a new $Ref for this file, even though we don't have the value yet.
   // This ensures that we don't simultaneously read & parse the same file multiple times
@@ -28,6 +33,7 @@ async function parse (path, $refs, options) {
   // This "file object" will be passed to all resolvers and parsers.
   let file = {
     url: path,
+    hash,
     extension: url.getExtension(path),
   };

I am currently building a wildcard ref resolver, but am in need of the original hash item inside the resolver function to achieve this. I can't see a way to obtain it with the current API - but I imagine it could be useful for other situations within a file resolver too - so would be useful to add.

jonluca commented 6 months ago

Great suggestion, included in the latest version