Giter VIP home page Giter VIP logo

swagger-faker's Introduction

Swagger Faker

Build Status codecov License NPM

Swagger Faker is a tool which can help you generate fake data from your swagger.json file, then it will start a mock server automatically with the generated fake data.

How to Use

  1. Install
npm i swagger-faker -g
  1. Init config file by command:
swagger-faker init

Then it will generate a config file swagger-faker.config.json in current path, update that config file:

{
  "sourcePaths": ["./source/openapi.json"],
  "outputFolder": "mock-server",
  "timeout": 180000,
  "port": 8081
}
  • sourcePaths: required, put your swagger/openapi file path here. Either local path(eg. "./source/openapi.json") or remote path(eg. https://petstore.swagger.io/v2/swagger.json) is supported. Valid format: json, yaml, yml.
  • outputFolder: optional
  • timeout: optional,
  • port: optional
  1. Generate fake data
swagger-faker gen
  1. Start the mock server
swagger-faker run

Examples

git clone https://github.com/reeli/swagger-faker-examples.git
cd  swagger-faker-examples
npm install
npm start

Then you'll see:

  1. A mock-server folder has been generated in your current path, and all mock data is setting in mock-server/data folder, you can customize them if needed.
  2. A mock server has been started in http://localhost:8081, you can open your browser and visit one of the mock API by http://localhost:8081/api/v2/store/order/1 (The mock data is setting in: mock-server/data/getOrderById.json)

swagger-faker's People

Contributors

dzannotti avatar fanjiarong-patsnap avatar reeli avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

swagger-faker's Issues

Cannot found config file while init

When I global install, and run swagger-init, it will throw a error:
.config/yarn/global/node_modules/swagger-faker/dist/lib/index.js:36
throw new Error(ERROR_MESSAGES.NOT_FOUND_CONFIG_FILE);
^

Error: Cannot found config file swagger-faker.config.json
at readSwaggerFakerConfig (/Users/crab.huang/.config/yarn/global/node_modules/swagger-faker/dist/lib/index.js:36:15)
at Object. (/Users/crab.huang/.config/yarn/global/node_modules/swagger-faker/dist/lib/index.js:486:26)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object. (/Users/crab.huang/.config/yarn/global/node_modules/swagger-faker/bin/cli.js:3:1)
at Module._compile (internal/modules/cjs/loader.js:999:30)

No data folder from generate

I am running swagger-faker gen and it completes with the mock server folder but it does not have a data file. Is there anything specific that is supposed to be in the json file for it to generate? I can't really share the whole swagger spec sheet but I can provide a snippet.

swagger-faker.config.json

{
  "sourcePaths": ["./openapi.json"],
  "outputFolder": "mock-server",
  "timeout": 180000,
  "port": 8081
}

A snippet of the spec

"Role": {
  "type": "object",
  "properties": {
    "id": {
      "type": "string",
      "format": "string",
      "example": "607f1f77bcf86cd799439011"
    },
    "organizationId": {
      "type": "string",
      "format": "string",
      "example": "6255d6ae4140045153386fea"
    },
    "name": {
      "type": "string",
      "format": "string",
      "example": "role A"
    },
    "permisssions": {
      "type": "array",
      "items": {
        "$ref": "#/components/schemas/RoleKeyValue"
      }
    }
  }
},
"RoleKeyValue": {
  "type": "object",
  "properties": {
    "key": {
      "type": "string",
      "format": "string",
      "example": "canInviteOperator"
    },
    "value": {
      "type": "string",
      "format": "string",
      "example": "enabled"
    }
  }
},

Error while trying the example

I have tried to run the example code on win10 in the Git-bash
I got the following error:

$ npm start

@reeli/[email protected] start
rm -rf mock-server && swagger-faker gen && swagger-faker run

Generate config to mock-server folder successfully!

{^_^}/ hi!

Loading mock-server/db.js
Loading mock-server/middlewares/*.js
Error: Cannot find module 'F:\Sources\IdolSecurity\swagger-faker-examples\mock-server\middlewares*.js'
Require stack:

  • F:\Sources\IdolSecurity\swagger-faker-examples\node_modules\json-server\lib\cli\run.js
  • F:\Sources\IdolSecurity\swagger-faker-examples\node_modules\json-server\lib\cli\index.js
  • F:\Sources\IdolSecurity\swagger-faker-examples\node_modules\json-server\lib\cli\bin.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at F:\Sources\IdolSecurity\swagger-faker-examples\node_modules\json-server\lib\cli\run.js:127:18
    at Array.map ()
    at F:\Sources\IdolSecurity\swagger-faker-examples\node_modules\json-server\lib\cli\run.js:125:40
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
    code: 'MODULE_NOT_FOUND',
    requireStack: [
    'F:\Sources\IdolSecurity\swagger-faker-examples\node_modules\json-server\lib\cli\run.js',
    'F:\Sources\IdolSecurity\swagger-faker-examples\node_modules\json-server\lib\cli\index.js',
    'F:\Sources\IdolSecurity\swagger-faker-examples\node_modules\json-server\lib\cli\bin.js'
    ]
    }

Can't run mock service

description
install the template,and copy the JSON to swagger.json,runstart and fail
ps:becuase windows didnot support rm-rf,installed rimraf-3.0.2

error
(node:34700) UnhandledPromiseRejectionWarning: S2OError: (Patchable) parameter.type is mandatory for non-body parameters
TypeError: app.use() requires a middleware function

image
image

Log:
0 info it worked if it ends with ok
1 verbose cli [
1 verbose cli 'C:\Program Files\nodejs\node.exe',
1 verbose cli 'C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js',
1 verbose cli 'run',
1 verbose cli 'start',
1 verbose cli '--scripts-prepend-node-path=auto'
1 verbose cli ]
2 info using [email protected]
3 info using [email protected]
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle @reeli/[email protected]prestart: @reeli/[email protected]
6 info lifecycle @reeli/[email protected]
start: @reeli/[email protected]
7 verbose lifecycle @reeli/[email protected]start: unsafe-perm in lifecycle true
8 verbose lifecycle @reeli/[email protected]
start: PATH: C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;D:\Projects\swagger-faker-examples\node_modules.bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Program Files\TortoiseSVN\bin;C:\Program Files\nodejs;C:\Program Files (x86)\Yarn\bin;C:\Program Files\Git\cmd;%adb%;C:\Users\EnochJiang\AppData\Local\Programs\Python\Python310\Scripts;C:\Users\EnochJiang\AppData\Local\Programs\Python\Python310;C:\Users\EnochJiang\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Bandizip;C:\Program Files\JetBrains\WebStorm 2021.3.2\bin;;C:\Users\EnochJiang\AppData\Roaming\npm;C:\Users\EnochJiang\AppData\Local\Yarn\bin
9 verbose lifecycle @reeli/[email protected]start: CWD: D:\Projects\swagger-faker-examples
10 silly lifecycle @reeli/[email protected]
start: Args: [
10 silly lifecycle '/d /s /c',
10 silly lifecycle 'rimraf mock-server && swagger-faker gen && swagger-faker run'
10 silly lifecycle ]
11 silly lifecycle @reeli/[email protected]start: Returned: code: 1 signal: null
12 info lifecycle @reeli/[email protected]
start: Failed to exec start script
13 verbose stack Error: @reeli/[email protected] start: rimraf mock-server && swagger-faker gen && swagger-faker run
13 verbose stack Exit status 1
13 verbose stack at EventEmitter. (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\index.js:332:16)
13 verbose stack at EventEmitter.emit (events.js:400:28)
13 verbose stack at ChildProcess. (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
13 verbose stack at ChildProcess.emit (events.js:400:28)
13 verbose stack at maybeClose (internal/child_process.js:1058:16)
13 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:293:5)
14 verbose pkgid @reeli/[email protected]
15 verbose cwd D:\Projects\swagger-faker-examples
16 verbose Windows_NT 10.0.22000
17 verbose argv "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" "run" "start" "--scripts-prepend-node-path=auto"
18 verbose node v14.19.0
19 verbose npm v6.14.16
20 error code ELIFECYCLE
21 error errno 1
22 error @reeli/[email protected] start: rimraf mock-server && swagger-faker gen && swagger-faker run
22 error Exit status 1
23 error Failed at the @reeli/[email protected] start script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.