Skip to content
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

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

Open
bogi158 opened this issue May 3, 2023 · 2 comments
Labels
stage: awaiting response Solution was proposed. Awaiting response. stage: needs investigation Needs further investigation

Comments

@bogi158
Copy link

bogi158 commented May 3, 2023

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',
  },
};
@javierbrea
Copy link
Member

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!

@javierbrea javierbrea added stage: awaiting response Solution was proposed. Awaiting response. stage: needs investigation Needs further investigation labels May 12, 2023
@bogi158
Copy link
Author

bogi158 commented Jun 1, 2023

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.

@javierbrea javierbrea moved this to To do in Backlog May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stage: awaiting response Solution was proposed. Awaiting response. stage: needs investigation Needs further investigation
Projects
Status: To do
Development

No branches or pull requests

2 participants