Skip to content

Build from YAML OpenAPI Spec Not Working #494

@ryancocuzzo

Description

@ryancocuzzo

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):**

  • OS: MacOS Sonoma 14.4.1
  • Node.js: v20.11.0
  • npm: 10.2.4

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions