Skip to content

Custom Resolver in dereferenceDocument is ignored #971

@dslovinsky

Description

@dslovinsky

Describe the bug
The documentation specifies that the second arg of dereferenceDocument is a custom resolved. It doesn't specify what the type of that resolver is but it seems to be using an outdated class instance from json-schema-tools/dereferencer. But no matter what you pass to the arg the output is never different. Methods don't run even when the protocols match specified ones.

To Reproduce
Steps to reproduce the behavior:

  1. Create an OpenRPC spec with at least one $ref field to an external location (separate file or https link for example).
  2. Create a custom resolver.
  3. Call dereferenceDocument and pass the custom resolver into the second argument.

Here's an example that just console logs method args:

const customResolver = {
  protocolHandlerMap: {
    file: async (uri: string, _root: JSONSchema) => {
      console.log("URI", uri);
      console.log("ROOT", _root);
      return Promise.resolve({});
    },
  },
};

const resolvedSpec = await dereferenceDocument(spec, customResolver)

Expected behavior
The file method should run first, before custom specified protocols. But neither it nor other protocols in protocolHandlerMap ever run. The console.logs in the example above never occur.

However, if you instead override the default protocolHanderMap it does work. This example works:

  const defaultFileResolver = defaultResolver.protocolHandlerMap.file;
  defaultResolver.protocolHandlerMap.file = async (
    uri: string,
    _root: JSONSchema,
  ) => {
    console.log("URI", uri);
    console.log("root", _root);
    return Promise.resolve({});
  };

const resolvedSpec = await dereferenceDocument(spec)

We shouldn't have to do this just to resolve non-local files.
The resolver arg should also have its type documented.

Desktop (please complete the following information):

  • OS: macOS 14.7.6
  • Browser: Chrome but running this in local scripts
  • Version: 2.1.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions