mocks-server / main

Node.js mock server running live, interactive mocks in place of real APIs
https://www.mocks-server.org
Apache License 2.0
288 stars 16 forks source link

Build from YAML OpenAPI Spec Not Working #494

Open Rcuz8 opened 4 months ago

Rcuz8 commented 4 months ago

Describe the bug I'm just trying to run a mock server that builds from a YAML OpenAPI spec. The setup steps in the OpenAPI guide seem to work perfectly for JSON.. but not for YAML.

Here's my config: (Bear mind, my usage of these entities may not be correct)

mocks/documents/api.yaml: (Basic api spec, validated on http://www.yamllint.com/)

openapi: 3.0.0
info:
  title: API
  version: '1.0'
  description: ...
servers:
  - url: /
paths:
  /users:
    get:
      tags:
    ...

mocks/openapi/definitions.yaml: (Basic definitions file)

- basePath: "/testing-api"
  collection:
    id: "testing-api"
    from: "base"
  document:
    $ref: ../documents/api.yaml

mocks/collections.json: (Basic collections file)

[]

mocks.config.js: (Default mocks-server config file)


module.exports = {
  config: {},
  plugins: {
    proxyRoutesHandler: {
    },
    adminApi: {
      https: {
      }
    },
    inquirerCli: {
    },
    openapi: {
      collection: {
      }
    }
  },
  mock: {
    routes: {
    },
    collections: {
    }
  },
  server: {
    cors: {
    },
    jsonBodyParser: {
    },
    urlEncodedBodyParser: {
    },
    https: {
    }
  },
  files: {
    babelRegister: {
    }
  },
  variantHandlers: {
  }
}

When I run npx mocks-server, I see this error:

‧ Error: [plugins:openapi:documents:0] Error resolving openapi $ref: Unexpected token 'o', "openapi: ../documents/api.yaml
     Error: Unexpected token 'o', "openapi: ../documents/api.yaml

Now, I have one other OpenAPI spec file, mocks/documents/alt.json. When I switch to using that one instead of mocks/documents/api.yaml, it works without issue.

My guess is it's an issue with the YAML support in the mocks-server library (Which is why I'm bringing it here).

To Reproduce Follow the steps in the Installation and OpenAPI integration guides

Expected behavior I expect the server to start without error and serve the endpoints defined in api.yaml.

Logs If applicable, add logs to help explain your problem.

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

Additional context Add any other context about the problem here.

javierbrea commented 4 months ago

Hi @Rcuz8 , I suppose it may be related to the usage of $ref in yaml files. Just to confirm, does it work if you replace the ref directly by the yaml content of the other file?

Rcuz8 commented 4 months ago

@javierbrea Yes, it does! When the yaml content is injected directly, it works.

Rcuz8 commented 4 months ago

Is there any more info you would need? @javierbrea

javierbrea commented 4 months ago

No, thank you @Rcuz8 , I think that it is enough to confirm where is the problem.