Skip to content
Swagger mock server with fake data generation support
Branch: master
Clone or download
strider2038 Merge pull request #7 from swagger-mock/6-random-float
#6 bugfix: random float generation improved
Latest commit 10a50dc Mar 29, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.dev docker image build in ci Jan 29, 2019
.docker
bin
build final travis config for release Feb 18, 2019
config #5 bugfix: translation component dependency removed Mar 26, 2019
docs mock parameters renamed to mock endpoint Feb 13, 2019
examples persons example added Feb 22, 2019
src #6 bugfix: random float generation improved Mar 29, 2019
tests #6 bugfix: random float generation improved Mar 29, 2019
translations behat test packages added Sep 27, 2018
.dockerignore dependencies updated and some bugs fixed Feb 18, 2019
.gitignore url decoding added for references Feb 21, 2019
.scrutinizer.yml scrutinizer config fix Oct 28, 2018
.travis.yml travis config for tag release Feb 18, 2019
Dockerfile dependencies updated and some bugs fixed Feb 18, 2019
LICENSE
README.md cache strategy keys changed Feb 24, 2019
behat.yml.dist string type generation with OpenAPI parameters Nov 10, 2018
composer.json dependencies updated + phpunit 8 migration Feb 21, 2019
composer.lock dependencies updated + phpunit 8 migration Feb 21, 2019
docker-compose.dev.yaml dependencies updated and some bugs fixed Feb 18, 2019
docker-compose.yml
easy-coding-standard.yml style improvement for styleci Jan 25, 2019
phpunit.xml.dist
road-runner.dist.yaml spiral roadrunner integration Jan 26, 2019
symfony.lock dependencies updated + phpunit 8 migration Feb 21, 2019

README.md

Swagger Mock Server

Build Status Scrutinizer Code Quality Code Coverage StyleCI

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
You can’t perform that action at this time.