mocks-server/main

Error resolving openapi $ref: JSON Pointer points to missing location: #/components/schemas/Pet

Opened this issue · 2 comments

Describe the bug
Generating mocks from OpenAPI definition is not working whe using $ref to point to some schema.

To Reproduce
Download the Petstore example from Swagger Hub, configure mocks server on a Node project and run.

Expected behavior
Mocks Server should be able to generate the routes.

Logs
One of the many errors:

Error: JSON Pointer points to missing location: #/components/schemas/Pet
             at /.../.../node_modules/@mocks-server/plugin-openapi/dist/openapi.js:172:20
             at Array.map (<anonymous>)...

** Operating system, Node.js an npm versions, or browser version (please complete the following information):**

  • OS: Ubuntu 18.04
  • Node.js: 16.20.0
  • npm: 8.19.4
  • Browser: Chrome

Additional context
The mock server works if I put all the OpenAPI specification inside the document property of definitions.js , but doesn't if I want to have an external file reference in document with $ref .

// definitions.js
module.exports = [
  {
    basePath: '/example',
    document: {
      $ref: '../documents/example.json',
    },
  },
];
// mocks.config.js
module.exports = {
  // Log level. Can be one of silly, debug, verbose, info, warn or error
  log: 'debug',
  plugins: {
    // Plugins to be registered
    openapi: {
      collection: {
        // Name for the collection created from OpenAPI definitions
        id: 'openapi',
        // Name of the collection to extend from
        // from: undefined,
      },
    },
  },
  mock: {
    routes: {
      // Global delay to apply to routes
      delay: 3000,
    },
    collections: {
      // Selected collection
      selected: 'openapi',
    },
  },
  server: {
    // Port number for the server to be listening at
    port: 6000,
    // Host for the server
    host: '0.0.0.0',
  },
};

Hi @bogi158 , I suppose that the error is probably produced because the json-refs module is trying to resolve refs from the root document, which in this case is not the OpenAPI itself, but a ref to the OpenAPI.

Have you tried to fix it using the plugin options refs.subDocPath or refs.location? It also supports defining any other json-refs module options.

{
plugins: {
    openapi: {
      refs: {
         subDocPath: "", // Read the docs for further info about this options
         location: "", // Read the docs for further info about this options
         // You can define any other json-refs option here
       }
    },
  },
}

In any case, it deserves to be investigated further in order to find and document a possible solution. Thanks for reporting it!

sorry but I needed to change the mocks library because of the limitation I had, so at the moment I am not using mocks-server and I cannot test your solution. I might try in the future if I have time.