LoginSignup
1
4

More than 5 years have passed since last update.

swaggerを用いたスタブ作成

Last updated at Posted at 2019-01-22

ローカルスタブを作成する手順

node.jsをインストールする

Windowsなのでこれを参考にしたが…、
https://qiita.com/taiponrock/items/9001ae194571feb63a5e
Linuxならyumでよいと思う。

$ sudo yum install epel-release
$ sudo yum install nodejs
$ node -v
v0.10.32

Gitをインストールする。

省略

Swagger Editorをインストールする。

NDAなどに引っかかる場合は、https://editor.swagger.io/ は利用できないため。
Windowsだとgit bashを開き以下コマンド。(よく考えたら、linuxでも一緒だな。)

$ git clone https://github.com/swagger-api/swagger-editor.git
$ cd swagger-editor/
$ npm install
$ npm start

補足

npm install時にnode-gyp周りでエラーになる場合は、以下を参照すること。
https://qiita.com/AkihiroTakamura/items/25ba516f8ec624e66ee7
これに加え、私の場合はpython3.6が別件で既にinstallされていたので、3.6は一旦uninstallする必要があった…。

インストールログ

PC MINGW64 ~
$ git clone https://github.com/swagger-api/swagger-editor.git
Cloning into 'swagger-editor'...
remote: Counting objects: 32385, done.
remote: Total 32385 (delta 0), reused 0 (delta 0), pack-reused 32385
Receiving objects: 100% (32385/32385), 130.69 MiB | 3.21 MiB/s, done.
Resolving deltas: 100% (18224/18224), done.

PC MINGW64 ~
$ cd swagger-editor/

PC MINGW64 ~/swagger-editor (master)
$ npm install
npm WARN deprecated react-file-download@0.3.5: WARNING: This project has been renamed to js-file-download. Use js-file-download instead.
(省略)
PC MINGW64 ~/swagger-editor (master)
$ npm start

> swagger-editor@3.1.12 prestart C:\Users\hoge\swagger-editor
> npm install

swagger-editor@3.1.12 C:\Users\hoge\swagger-editor
+-- UNMET PEER DEPENDENCY react@15.6.2
`-- swagger-ui@3.4.0
  +-- brace@0.7.0
  +-- reselect@2.5.3
  +-- UNMET PEER DEPENDENCY webpack@^2.2.0
  `-- yaml-js@0.2.0

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules\chokidar\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.1.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm WARN commonmark-react-renderer@4.3.3 requires a peer of commonmark@^0.27.0 || ^0.26.0 || ^0.24.0 but none was installed.
npm WARN eslint-config-standard@6.2.1 requires a peer of eslint@>=3.8.1 but none was installed.
npm WARN eslint-config-standard-jsx@3.2.0 requires a peer of eslint@>=3.0.0 but none was installed.
npm WARN eslint-plugin-standard@2.0.1 requires a peer of eslint@>=3.0.0 but none was installed.
npm WARN react-addons-shallow-compare@0.14.8 requires a peer of react@^0.14.8 but none was installed.
npm WARN react-object-inspector@0.2.1 requires a peer of react@^0.14.0 but none was installed.
npm WARN webpack-dev-server@2.5.0 requires a peer of webpack@^2.2.0 but none was installed.

> swagger-editor@3.1.12 start C:\Users\hoge\swagger-editor
> npm-run-all --parallel serve-static open-static


> swagger-editor@3.1.12 serve-static C:\Users\hoge\swagger-editor
> http-server -i -a 0.0.0.0 -p 3001


> swagger-editor@3.1.12 open-static C:\Users\hoge\swagger-editor
> node -e 'require("open")("http://localhost:3001")'

Starting up http-server, serving ./
Available on:
  http://192.168.60.60:3001
  http://127.0.0.1:3001
Hit CTRL-C to stop the server

swaggerエディタでyamlから出力を行う。

まずはAPIのyamlを入手

swagger editorを起動

上記例でいうと、htp://127.0.0.1:3001にアクセス。
image.png

editor上からServerStubを作成

GenerateServer→nodejsserver
image.png

作成したServerStubからnode.jsを起動

zipファイルを解凍し、配置。
git bash(でもコマンドプロンプトでもなんでもいいが) を起動して、当該ディレクトリに移動して
$npm startを実行

 MINGW64 ~/stub/nodejs-server-server
$ npm start

> swagger-petstore@1.0.0 prestart C:\Users\hoge\stub\nodejs-server-server
> npm install

npm notice created a lockfile as package-lock.json. You should commit this file.
added 147 packages from 80 contributors and audited 400 packages in 24.937s
found 4 vulnerabilities (2 low, 1 moderate, 1 high)
  run `npm audit fix` to fix them, or `npm audit` for details

> swagger-petstore@1.0.0 start C:\Users\hoge\stub\nodejs-server-server
> node index.js

Your server is listening on port 8080 (http://localhost:8080)
Swagger-ui is available on http://localhost:8080/docs

 MINGW64 ~/stub/nodejs-server-server

SwaggerUIを立ち上げる。

localhost:8080/docsにアクセス。APIドキュメントやお試しリクエストなどはこれでわかる。
image.png

これでローカル側でスタブが立ち上がったので、スタブURLをこのURL(localhost:8080)に設定。

1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4