Swagger mock server

匿名 (未验证) 提交于 2019-12-03 07:50:05

问题:

I have an API reference in Swagger file. I want to create a very simple mock server, so that when I call eg.:

mymockurl.com/users it will return a predefined json (no need to connect to a database).

What's the easiest way to do this? I'm not a backend guy.

回答1:

SwaggerHub provides a mock server for OpenAPI 2.0 and 3.0 specs. Mocking is supported on both free and paid plans.

To use the mock server, import your spec into SwaggerHub and enable "API Auto Mocking". Mock responses can be JSON, YAML and XML, and are generated based on your response schemas and the example, default and enum values defined in the schemas.

Disclosure: I work for the company that makes SwaggerHub.



回答2:

An easy way to create simple mock from an Open API (fka. Swagger) spec without code is to use a tool call prism available at http://stoplight.io/platform/prism/

This command line is all you need:

./prism run --mock --list --spec 

The mock server will return dynamic response based on the swagger spec. If examples are provided in the spec, prism will return them, if not it will generate dummy data based on the spec.



回答3:

Imposter is a A scriptable, multipurpose mock server.

Very easy to setup in a Docker environment and provides a Swagger UI to play with your mock api.

Let's see an example setup

  1. Have a swagger configuration ready in a file in the folder config/petstore.yaml

    swagger: "2.0" info: version: 1.0.0 title: Swagger Petstore     .     .     .

    You can copy the example swagger specification from here.

  2. Create a configuration file for Imposter in config/openapi-plugin-petstore-config.json

    {   "plugin": "com.gatehill.imposter.plugin.openapi.OpenApiPluginImpl",   "specFile": "petstore.yaml" }

    Name of the configuration file has to end with -config.json.

  3. Run Imposter with Docker

    Ensure that your shell is placed in the parent directory of config and run

    docker run -ti -p 8443:8443 \ -v $(pwd)/config:/opt/imposter/config \ outofcoffee/imposter-openapi
  4. Open http://localhost:8443/_spec/ to play with your Mock Server and Swagger UI



回答4:

Given the OpenAPI/Swagger spec, you can use Swagger Codegen to generate server stub in different server frameworks (e.g. Java Spring. PHP Slim, Ruby on Rails5, etc).

Here is the related documentation:

https://github.com/swagger-api/swagger-codegen/wiki/Server-stub-generator-HOWTO



回答5:

Here is the docker container for mock api server from swagger yaml.

docker run -i \     -p 8000:8000 \     -v /path/to/file.yaml:/data/swagger.yaml \     -t palo/swagger-api-mock:latest

This is internally using swagger-mock-api



回答6:

openapi-mock is a CLI wrapper for swagger-node-runner and sway specifically to start a mock server from an openapi/swagger spec file.



转载请标明出处:Swagger mock server
文章来源: Swagger mock server
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!