swagui 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 50e4fe6a730fae9960c9038449f113c3cfef72e6
4
- data.tar.gz: f31291c54b4bea9ef5f75c3fc4d2d2de4ca30ad1
3
+ metadata.gz: 5ada5af68f79789855e02b8739f466125d9281d6
4
+ data.tar.gz: 5fab5cef417da06e01af99fbdadfb44fa87c097d
5
5
  SHA512:
6
- metadata.gz: e2d25877a39a5ae885ca6ac9eaa06573b3db7b96afc7d3ef40cf369593332cda51d694a64d41e816414f49f708bcc60f89a2950a20f12674b5d6a122cdab8c87
7
- data.tar.gz: ebaf37b73fe27809e2e18a35c2d60b79815f5083cef08b63b09b80a949dae586e669825acbd6981309591c281a283dfb9c07d07df89d38aff49db64c28837bf0
6
+ metadata.gz: 2724cf00bc4bf66c060829b45a0177c9c740d07bebeae0f97e16d45ef7153629a9cd192ec2289aed1cf819b2247eee7c77b1f187c6a27904428edb4466cc12f1
7
+ data.tar.gz: 551f0eda01c8fd4a8006a697d9f5693a109074d3a09ce44d4fc1b8e42adb32fe52757cfe16726b7459ceb5bf3a3603561ff906e10421a4521d01b4b8c6cf45a8
data/README.md CHANGED
@@ -42,9 +42,18 @@ You will be able to access the [swagger-ui] loaded with your api documentation a
42
42
  1. JSON documents with no extension ([original swagger doc format]), such as `api-docs` and `account`.
43
43
  2. JSON documents with `.json` extension, such as `api-docs.json` and `account.json`.
44
44
  3. YAML documents. This the recommended approach as it is naturally more concise than json and also tries to be more opinionated in the following ways:
45
- 1. if not provided in `api-docs.yml`, the list of apis under `apis` is automatically populated by the names of all the .yml files in the same directory.
46
- 2. the `basePath` attribute of each doc, used for `try it out` feature, if unprovided, is automatically provided based on the host application. this assumes the doc is hosted as part of the application.
47
- 3. to circumvent all the rather complex and tedious syntax for `models`, schema attributes under `apis/operations/parameters` (for request body json schema) and `apis/operations/responseMessages` (for response body json schema), and it will be used to automatically populate the `models` under root.
45
+ 1. __auto listing__: if not provided in `api-docs.yml`, the list of apis under `apis` is automatically populated by the names of all the .yml files in the same directory.
46
+ 2. __auto "Try it out"__: the `basePath` attribute of each doc, used for `try it out` feature, if unprovided, is automatically provided based on the host application. this assumes the doc is hosted as part of the application.
47
+ 3. __schema-based models__: to circumvent all the rather complex and tedious syntax for `models`, schema attributes under `apis/operations/parameters` (for request body json schema) and `apis/operations/responseMessages` (for response body json schema), and it will be used to automatically populate the `models` under root.
48
+ 4. __template__: in `api-docs.yml`, a `template` section can be added that lists all the general attributes for all the apis, for example if you apis is a JSON api, chances are that they all consumes JSON requests and produces JSON responses. IN this case, instead of adding the following line in each service yml file, you can simply add this section to `api-docs.yml`.
49
+
50
+ ```javascript
51
+ template:
52
+ produces:
53
+ - application/json
54
+ consumes:
55
+ - application/json
56
+ ```
48
57
 
49
58
  With this approach, swagger api docs are now a lot more concise and readible, let alone having dynamic `basePath`.
50
59
 
@@ -1,3 +1,3 @@
1
1
  module Swagui
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -5,6 +5,7 @@ module Swagui
5
5
 
6
6
  def initialize(app)
7
7
  @app = app
8
+ @api_json_template = nil # used to store the template settings that applies to all apis
8
9
  end
9
10
 
10
11
  def call(env)
@@ -16,8 +17,8 @@ module Swagui
16
17
  body = []
17
18
  response[2].each do |f|
18
19
  body << YAML::load(f).tap do |response_hash|
19
- process_schemas(response_hash)
20
20
  process_api_docs_api_listing(response_hash, response[2].path )
21
+ process_schemas(response_hash)
21
22
  process_base_path(response_hash, response[2].path, env)
22
23
  end.to_json
23
24
  end
@@ -51,6 +52,7 @@ module Swagui
51
52
  end
52
53
  end
53
54
  end
55
+ deep_merge!(response_hash, @api_json_template) if @api_json_template
54
56
  end
55
57
 
56
58
  def merge_schema!(parent_hash, response_hash, name, response_model = false)
@@ -74,6 +76,9 @@ module Swagui
74
76
 
75
77
  def process_api_docs_api_listing(response_hash, doc_path)
76
78
  if doc_path.end_with?('api-docs.yml') && response_hash['models'].nil? # requesting the root
79
+
80
+ @api_json_template = response_hash.delete('template')
81
+
77
82
  dir_path = File.dirname(doc_path)
78
83
  files = Dir["#{File.dirname(doc_path)}/*.yml"].map {|x| x.gsub(dir_path, '').gsub('.yml', '')}
79
84
  files.each do |file|
@@ -91,5 +96,19 @@ module Swagui
91
96
  end
92
97
  end
93
98
 
99
+ def deep_merge!(actual, template)
100
+ merger = proc { |key, v1, v2|
101
+ if (Hash === v1 && Hash === v2)
102
+ v1.merge!(v2, &merger)
103
+ elsif (Array === v2 && Array === v1)
104
+ v1 = v1 + v2
105
+ elsif (Hash === v2 && Array === v1)
106
+ v1.map {|x| x.merge!(v2, &merger)}
107
+ else
108
+ v1
109
+ end
110
+ }
111
+ actual.merge!(template, &merger)
112
+ end
94
113
  end
95
114
  end
data/test/doc/account.yml CHANGED
@@ -1,6 +1,4 @@
1
1
  ---
2
- produces:
3
- - application/json
4
2
  apis:
5
3
  -
6
4
  path: /account
@@ -21,10 +19,6 @@
21
19
  code: 200
22
20
  message: account creation success
23
21
  schema: test/doc/schemas/account_post_creation_success.schema.json
24
- -
25
- code: 400
26
- message: account creation failure
27
- schema: test/doc/schemas/account_post_creation_failure.schema.json
28
22
  -
29
23
  method: GET
30
24
  summary: Get all the accounts
@@ -6,3 +6,15 @@
6
6
  apiVersion: "1.0.0"
7
7
  swaggerVersion: "1.2"
8
8
  authorizations: {}
9
+ template:
10
+ produces:
11
+ - application/json
12
+ consumes:
13
+ - application/json
14
+ apis:
15
+ operations:
16
+ responseMessages:
17
+ -
18
+ code: 400
19
+ message: account creation failure
20
+ schema: test/doc/schemas/account_post_creation_failure.schema.json
@@ -5,7 +5,7 @@ class TestSwaggerDocHandler < Minitest::Test
5
5
  include Rack::Test::Methods
6
6
 
7
7
  def app
8
- Rack::Lint.new(mounted_app)
8
+ @app ||= Swagui::App.new(Rack::Lobster.new, url: '/doc', path: 'test/doc')
9
9
  end
10
10
 
11
11
  def test_loading_api_docs_root
@@ -32,4 +32,22 @@ class TestSwaggerDocHandler < Minitest::Test
32
32
  assert_equal response_json['apis'].first['operations'].last['items'], {'$ref' => 'GETAccounts'}
33
33
  end
34
34
 
35
+ def test_template_attributes_removed_from_api_docs
36
+ get '/doc/api-docs'
37
+ assert last_response.ok?
38
+ response_json = JSON.parse(last_response.body)
39
+ assert_nil response_json['template']
40
+ end
41
+
42
+ def test_template_attributes
43
+ get '/doc/api-docs'
44
+ get '/doc/account'
45
+ assert last_response.ok?
46
+ response_json = JSON.parse(last_response.body)
47
+ assert_equal response_json['produces'], ['application/json']
48
+ assert_equal response_json['consumes'], ['application/json']
49
+ assert_equal response_json['apis'].first['operations'].first['responseMessages'].map {|x| x['code']}, [200, 400]
50
+ end
51
+
52
+
35
53
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swagui
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jack Xu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-15 00:00:00.000000000 Z
11
+ date: 2014-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack