3

I have been trying to wrap my head around mock api services, such as the one that Stoplight provides. I want to create something similar, but I keep hitting a wall and I can't imagine why it should be so hard to figure out. Here's the details.

Stoplight has a feature where each project has its own mock api server. The mock server works out of the Open API spec (OAS) specifically for that project. This means that every time someone creates a new project, a new mock server is also created. I assume this happens automatically behind the scenes through some scripting, unless it's a truly dynamic process. Stoplight has an open source mock server project called Prism, and I can only assume thats is what is used as the tool for the mock servers.

When you run Prism, you must give it a url for the OAS. That means that one instance of the server can handle one OAS. Based on this info, Stoplight would need to spin up a Docker instance for each new project. That would end up with a lot of containers, most of them probably never used. Thats doesn't seem like a very good solution, so they must do something else.

Prism also has the ability to serve multiple OAS through something they call reverse proxy. This means that you can use one base URL to server many different OAS. However, behind the scenes, Prism is still running one container for each OAS. This is the most likely situation so far, but I can't figure out how new mock servers are created in a dynamic fashion.

Does anyone have any input on how to achieve such a scenario? Is one Docker container per OAS really the way to go? And if so, what would I need to do to start, stop and reload containers from my backend?

jezzdk
  • 31
  • 1
  • You could create a mock api server to support multiple APIs by using prism's libraries. The key one you should look into is https://www.npmjs.com/package/@stoplight/prism-http I've developed an API validation proxy base on prism-http and express to serve multiple APIs in one instance. – aleung Oct 21 '22 at 09:03
  • @aleung I can't believe I missed that lib. It looks like I can use it to make requests on-the-fly instead of booting a server that listens for requests. I might be able to create a webserver in Node with Express, that listens for requests. And then based on the base url of the request I could probably fetch an OAS for that specific request, and then basically use the rest of the path as the mock request with prism-http. Interesting. You should write it as an answer :) – jezzdk Oct 24 '22 at 05:51
  • @jezzdk Specmatic (https://specmatic.in/) is another opensource tool that you can use for creating mock servers with OpenAPI. Here is my answer to a similar question with the details - https://stackoverflow.com/a/74570174/444252. I am the lead developer and CTO at Specmatic. – HariKrishnan Nov 25 '22 at 09:09

2 Answers2

1

You can try using APIGit, a tool that utilizes Git technology to manage API specifications. This allows you to create a repository and import your OpenAPI Specification (OAS) files, and generate mock server scripts directly from the selected OAS. The tool's native Git support enables you to publish and maintain multiple versions of mock servers simultaneously, making it a convenient solution for API development and testing. automatic-mockserver-generation

lhyuan5717
  • 26
  • 4
0

You could create a mock API server in Node.js to support multiple APIs by using prism's libraries. The key one you should look into is https://npmjs.com/package/@stoplight/prism-http.

Create a webserver with Express or any other web framework, and create prism-http instances per OpenAPI definition. Route incoming requests to different prism-http instances based on the base path defined in 'servers' in OpenAPI file.

A note: the OAS file needs to have an entry without host part to make prism-http work.

servers:
  - url: /foo/v1                   # <-- This is mandatory to make prism-http work
  - url: '{serverRoot}/foo/v1'     # <-- Additional entries as needed
    variables:
      serverRoot:
        default: https://my-server:8080

aleung
  • 9,848
  • 3
  • 55
  • 69