/swagger-mock

Swagger mock server with fake data generation support

Primary LanguagePHPMIT LicenseMIT

Swagger Mock Server

Build Status Scrutinizer Code Quality Code Coverage StyleCI Docker Pulls GitHub release (latest by date)

Swagger API mock server with fake data generation with main features.

  • OpenAPI 3.x support.
  • Load specification from local file or URL.
  • JSON and YAML format supported.
  • Generates fake response data by provided schemas.
  • Content negotiation by Accept header.
  • Runs in Docker container.

Supported features

Feature Support status
generating xml response basic support (without xml tags)
generating json response supported
generation of basic types supported
generation of enums supported
generation of associative arrays supported
generation of combined types supported (without tag not and discriminator)
local reference resolving supported
remote reference resolving not supported
URL reference resolving not supported
validating request data not supported
force using custom response schema not supported (schema detected automatically)

How to use

Recommended way is to use Docker container.

docker pull swaggermock/swagger-mock

# with remote file
docker run -p 8080:8080 -e "SWAGGER_MOCK_SPECIFICATION_URL=https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml" --rm swaggermock/swagger-mock

# with local file
docker run -p 8080:8080 -v $PWD/examples/petstore.yaml:/openapi/petstore.yaml -e "SWAGGER_MOCK_SPECIFICATION_URL=/openapi/petstore.yaml" --rm swaggermock/swagger-mock

Also, you can use Docker Compose. Example of docker-compose.yml

version: '3.0'

services:
  swagger_mock:
    container_name: swagger_mock
    image: swaggermock/swagger-mock
    environment:
      SWAGGER_MOCK_SPECIFICATION_URL: 'https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml'
    ports:
      - "8080:8080"

To start up container run command

docker-compose up -d

Configuration

Environment variables

Mock server options can be set via environment variables.

SWAGGER_MOCK_SPECIFICATION_URL

  • Path to file with OpenAPI v3 specification (required)
  • Possible values: any valid URL or path to file

SWAGGER_MOCK_LOG_LEVEL

  • Error log level
  • Default value: warning
  • Possible values: error, warning, info, debug

SWAGGER_MOCK_CACHE_DIRECTORY

  • Directory for OpenAPI specification cache
  • Default value: /dev/shm/openapi-cache
  • Possible values: any valid path

SWAGGER_MOCK_CACHE_TTL

  • Time to live for OpenAPI specification cache in seconds
  • Default value: 0
  • Possible values: positive integer

SWAGGER_MOCK_CACHE_STRATEGY

  • Caching strategy for OpenAPI specification cache
  • Default value: disabled
  • Possible values: disabled, url_md5, url_and_timestamp_md5

Specification cache

To speed up server response time you can use caching mechanism for OpenAPI. There are several caching strategies. Specific strategy can be set by environment variable SWAGGER_MOCK_CACHE_STRATEGY.

  • url_md5 calculates hash from specification URL and if specification URL was not changed uses parsed objects from cache.
  • url_and_timestamp_md5 calculates hash from specification URL and timestamp (file timestamp or value of Last-Modified header). If you are using file from remote server make sure that valid Last-Modified header is present.

Recommended options for use with remote file (accessible by URL).

  • SWAGGER_MOCK_CACHE_STRATEGY='url_md5'
  • SWAGGER_MOCK_CACHE_TTL=3600

Recommended options for use with local file (at local server).

  • SWAGGER_MOCK_CACHE_STRATEGY='url_and_timestamp_md5'
  • SWAGGER_MOCK_CACHE_TTL=3600

License

This project is licensed under the MIT License - see the LICENSE file for details.

Features roadmap for next versions

  • extra response negotiation (return of 405 code)
    • path parser
    • route matcher in path object
    • routing by path and endpoints
  • response cache
  • faker expression extension for numbers
  • faker expression extension for strings
  • request body validation
  • remote reference support
  • url reference support
  • discriminator in combined types