swagger-docs 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/CHANGELOG.md +5 -0
- data/README.md +28 -0
- data/lib/swagger/docs/config.rb +5 -0
- data/lib/swagger/docs/generator.rb +19 -8
- data/lib/swagger/docs/methods.rb +2 -2
- data/lib/swagger/docs/version.rb +1 -1
- data/spec/fixtures/controllers/sample_controller.rb +1 -0
- data/spec/lib/swagger/docs/generator_spec.rb +19 -7
- metadata +2 -2
- metadata.gz.sig +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f824965e424cbfdcd8551dea96dbabe88e7368fd
|
4
|
+
data.tar.gz: 2a02f6c05ea8aa87659b022f4807bf2a9874639f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 81971dd258cb50fae6a2a0e8c6837deff20da9f9067e6a4a433b6c04e4a3351fa035d59871fb263f96bae53db2164b9b053013d487e427129619ad3956c7b8bd
|
7
|
+
data.tar.gz: 817550eddc45f754818bebdfa134fc6bff885a6fa6796116bf2f7da4530bba0458f2cc076e569d16dc602129177c14f7969bb8ca3b7a33eb9f0f8a4a80a950e5
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
Binary file
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
## 0.1.1
|
2
|
+
- Add support for Rails engines (@fotinakis)
|
3
|
+
- Filter out path parameters if the parameter is not in the path (@stevschmid)
|
4
|
+
|
1
5
|
## 0.1.0
|
2
6
|
|
3
7
|
- Add CHANGELOG.md
|
@@ -8,6 +12,7 @@
|
|
8
12
|
- Custom response message error text can now be set
|
9
13
|
- Ability to override base controller with `base_api_controller` method
|
10
14
|
- Default configuration for Generator
|
15
|
+
- Fix typo in README.md
|
11
16
|
|
12
17
|
##0.0.3
|
13
18
|
|
data/README.md
CHANGED
@@ -183,6 +183,34 @@ class Swagger::Docs::Config
|
|
183
183
|
end
|
184
184
|
```
|
185
185
|
|
186
|
+
#### Custom route discovery for supporting Rails Engines
|
187
|
+
|
188
|
+
By default, swagger-docs finds controllers by traversing routes in `Rails.application`.
|
189
|
+
To override this, you can customize the `base_application` config in an initializer:
|
190
|
+
|
191
|
+
```ruby
|
192
|
+
class Swagger::Docs::Config
|
193
|
+
def self.base_application; Api::Engine end
|
194
|
+
end
|
195
|
+
```
|
196
|
+
|
197
|
+
=======
|
198
|
+
#### Transforming the `path` variable
|
199
|
+
|
200
|
+
Swagger allows a distinction between the API documentation server and the hosted API
|
201
|
+
server through the `path` variable (see [Swagger: No server Integrations](https://github.com/wordnik/swagger-core/wiki/No-server-Integrations)). To override the default swagger-docs behavior, you can provide a `transform_path`
|
202
|
+
class method in your initializer:
|
203
|
+
|
204
|
+
```ruby
|
205
|
+
class Swagger::Docs::Config
|
206
|
+
def self.transform_path(path)
|
207
|
+
"http://example.com/api-docs/#{path}"
|
208
|
+
end
|
209
|
+
end
|
210
|
+
```
|
211
|
+
|
212
|
+
The transformation will be applied to all API `path` values in the generated `api-docs.json` file.
|
213
|
+
|
186
214
|
#### Precompile
|
187
215
|
|
188
216
|
It is best-practice *not* to keep documentation in version control. An easy way
|
data/lib/swagger/docs/config.rb
CHANGED
@@ -3,6 +3,7 @@ module Swagger
|
|
3
3
|
class Config
|
4
4
|
class << self
|
5
5
|
def base_api_controller; ApplicationController end
|
6
|
+
def base_application; Rails.application end
|
6
7
|
def register_apis(versions)
|
7
8
|
base_api_controller.send(:include, ImpotentMethods)
|
8
9
|
@versions = versions
|
@@ -10,6 +11,10 @@ module Swagger
|
|
10
11
|
def registered_apis
|
11
12
|
@versions ||= {}
|
12
13
|
end
|
14
|
+
def transform_path(path)
|
15
|
+
# This is only for overriding, so don't perform any path transformations by default.
|
16
|
+
path
|
17
|
+
end
|
13
18
|
end
|
14
19
|
end
|
15
20
|
end
|
@@ -4,9 +4,9 @@ module Swagger
|
|
4
4
|
|
5
5
|
DEFAULT_VER = "1.0"
|
6
6
|
DEFAULT_CONFIG = {
|
7
|
-
:api_file_path => "public/",
|
8
|
-
:base_path => "/",
|
9
|
-
:clean_directory => false,
|
7
|
+
:api_file_path => "public/",
|
8
|
+
:base_path => "/",
|
9
|
+
:clean_directory => false,
|
10
10
|
:formatting => :pretty
|
11
11
|
}
|
12
12
|
|
@@ -91,7 +91,7 @@ module Swagger
|
|
91
91
|
header = { :api_version => api_version, :swagger_version => "1.2", :base_path => base_path + "/"}
|
92
92
|
resources = header.merge({:apis => []})
|
93
93
|
|
94
|
-
paths =
|
94
|
+
paths = Config.base_application.routes.routes.map{|i| "#{i.defaults[:controller]}" }
|
95
95
|
paths = paths.uniq.select{|i| i.start_with?(controller_base_path)}
|
96
96
|
paths.each do |path|
|
97
97
|
next if path.empty?
|
@@ -102,14 +102,16 @@ module Swagger
|
|
102
102
|
end
|
103
103
|
apis = []
|
104
104
|
debased_path = path.gsub("#{controller_base_path}", "")
|
105
|
-
|
105
|
+
Config.base_application.routes.routes.select{|i| i.defaults[:controller] == path}.each do |route|
|
106
106
|
action = route.defaults[:action]
|
107
107
|
verb = route.verb.source.to_s.delete('$'+'^').downcase.to_sym
|
108
108
|
next if !operations = klass.swagger_actions[action.to_sym]
|
109
|
-
operations = Hash[operations.map {|k, v| [k.to_s.gsub("@","").to_sym, v] }] # rename :@instance hash keys
|
109
|
+
operations = Hash[operations.map {|k, v| [k.to_s.gsub("@","").to_sym, v.respond_to?(:deep_dup) ? v.deep_dup : v.dup] }] # rename :@instance hash keys
|
110
110
|
operations[:method] = verb
|
111
111
|
operations[:nickname] = "#{path.camelize}##{action}"
|
112
|
-
|
112
|
+
api_path = trim_slashes(get_api_path(trim_leading_slash(route.path.spec.to_s), config[:api_extension_type]).gsub("#{controller_base_path}",""))
|
113
|
+
operations[:parameters] = filter_path_params(api_path, operations[:parameters])
|
114
|
+
apis << {:path => api_path, :operations => [operations]}
|
113
115
|
end
|
114
116
|
demod = "#{debased_path.to_s.camelize}".demodulize.camelize.underscore
|
115
117
|
resource = header.merge({:resource_path => "#{demod}", :apis => apis})
|
@@ -117,7 +119,7 @@ module Swagger
|
|
117
119
|
# write controller resource file
|
118
120
|
write_to_file "#{api_file_path}/#{demod}.json", resource, config
|
119
121
|
# append resource to resources array (for writing out at end)
|
120
|
-
resources[:apis] << {path: "#{trim_leading_slash(debased_path)}.{format}", description: klass.swagger_config[:description]}
|
122
|
+
resources[:apis] << {path: "#{Config.transform_path(trim_leading_slash(debased_path))}.{format}", description: klass.swagger_config[:description]}
|
121
123
|
results[:processed] << path
|
122
124
|
end
|
123
125
|
# write master resource file
|
@@ -134,6 +136,15 @@ module Swagger
|
|
134
136
|
end
|
135
137
|
File.open(path, 'w') { |file| file.write content }
|
136
138
|
end
|
139
|
+
|
140
|
+
private
|
141
|
+
|
142
|
+
def filter_path_params(path, params)
|
143
|
+
params.reject do |param|
|
144
|
+
param_as_variable = "{#{param[:name]}}"
|
145
|
+
param[:param_type] == :path && !path.include?(param_as_variable)
|
146
|
+
end
|
147
|
+
end
|
137
148
|
end
|
138
149
|
end
|
139
150
|
end
|
data/lib/swagger/docs/methods.rb
CHANGED
@@ -29,11 +29,11 @@ module Swagger
|
|
29
29
|
@swagger_dsl ||= {}
|
30
30
|
controller_action = "#{name}##{action} #{self.class}"
|
31
31
|
return if @swagger_dsl[action]
|
32
|
-
route =
|
32
|
+
route = Swagger::Docs::Config.base_application.routes.routes.select{|i| "#{i.defaults[:controller].to_s.camelize}Controller##{i.defaults[:action]}" == controller_action }.first
|
33
33
|
dsl = SwaggerDSL.call(action, self, &block)
|
34
34
|
@swagger_dsl[action] = dsl
|
35
35
|
end
|
36
36
|
end
|
37
37
|
end
|
38
38
|
end
|
39
|
-
end
|
39
|
+
end
|
data/lib/swagger/docs/version.rb
CHANGED
@@ -8,6 +8,7 @@ module Api
|
|
8
8
|
swagger_api :index do
|
9
9
|
summary "Fetches all User items"
|
10
10
|
param :query, :page, :integer, :optional, "Page number"
|
11
|
+
param :path, :nested_id, :integer, :optional, "Team Id"
|
11
12
|
response :unauthorized
|
12
13
|
response :not_acceptable, "The request you made is not acceptable"
|
13
14
|
response :requested_range_not_satisfiable
|
@@ -23,6 +23,7 @@ describe Swagger::Docs::Generator do
|
|
23
23
|
let(:routes) {[
|
24
24
|
stub_route("^GET$", "index", "api/v1/ignored", "/api/v1/ignored(.:format)"),
|
25
25
|
stub_route("^GET$", "index", "api/v1/sample", "/api/v1/sample(.:format)"),
|
26
|
+
stub_route("^GET$", "index", "api/v1/sample", "/api/v1/nested/:nested_id/sample(.:format)"),
|
26
27
|
stub_route("^POST$", "create", "api/v1/sample", "/api/v1/sample(.:format)"),
|
27
28
|
stub_route("^GET$", "show", "api/v1/sample", "/api/v1/sample/:id(.:format)"),
|
28
29
|
stub_route("^PUT$", "update", "api/v1/sample", "/api/v1/sample/:id(.:format)"),
|
@@ -67,7 +68,7 @@ describe Swagger::Docs::Generator do
|
|
67
68
|
expect(response["resourcePath"]).to eq "sample"
|
68
69
|
end
|
69
70
|
it "writes out expected api count" do
|
70
|
-
expect(response["apis"].count).to eq
|
71
|
+
expect(response["apis"].count).to eq 6
|
71
72
|
end
|
72
73
|
context "first api" do
|
73
74
|
#"apis":[{"path":" /sample","operations":[{"summary":"Fetches all User items"
|
@@ -168,8 +169,7 @@ describe Swagger::Docs::Generator do
|
|
168
169
|
context "resource file" do
|
169
170
|
let(:resource) { FILE_RESOURCE.read }
|
170
171
|
let(:response) { JSON.parse(resource) }
|
171
|
-
let(:
|
172
|
-
let(:operations) { first["operations"] }
|
172
|
+
let(:operations) { api["operations"] }
|
173
173
|
let(:params) { operations.first["parameters"] }
|
174
174
|
let(:response_msgs) { operations.first["responseMessages"] }
|
175
175
|
# {"apiVersion":"1.0","swaggerVersion":"1.2","basePath":"/api/v1","resourcePath":"/sample"
|
@@ -186,18 +186,19 @@ describe Swagger::Docs::Generator do
|
|
186
186
|
expect(response["resourcePath"]).to eq "sample"
|
187
187
|
end
|
188
188
|
it "writes out expected api count" do
|
189
|
-
expect(response["apis"].count).to eq
|
189
|
+
expect(response["apis"].count).to eq 6
|
190
190
|
end
|
191
191
|
context "first api" do
|
192
|
+
let(:api) { response["apis"][0] }
|
192
193
|
#"apis":[{"path":" /sample","operations":[{"summary":"Fetches all User items"
|
193
194
|
#,"method":"get","nickname":"Api::V1::Sample#index"}]
|
194
195
|
it "writes path correctly when api extension type is not set" do
|
195
|
-
expect(
|
196
|
+
expect(api["path"]).to eq "sample"
|
196
197
|
end
|
197
198
|
it "writes path correctly when api extension type is set" do
|
198
199
|
config[DEFAULT_VER][:api_extension_type] = :json
|
199
200
|
generate(config)
|
200
|
-
expect(
|
201
|
+
expect(api["path"]).to eq "sample.json"
|
201
202
|
end
|
202
203
|
it "writes summary correctly" do
|
203
204
|
expect(operations.first["summary"]).to eq "Fetches all User items"
|
@@ -208,7 +209,10 @@ describe Swagger::Docs::Generator do
|
|
208
209
|
it "writes nickname correctly" do
|
209
210
|
expect(operations.first["nickname"]).to eq "Api::V1::Sample#index"
|
210
211
|
end
|
211
|
-
#"parameters"
|
212
|
+
#"parameters"=>[
|
213
|
+
# {"paramType"=>"query", "name"=>"page", "type"=>"integer", "description"=>"Page number", "required"=>false},
|
214
|
+
# {"paramType"=>"path", "name"=>"nested_id", "type"=>"integer", "description"=>"Team Id", "required"=>false}], "responseMessages"=>[{"code"=>401, "message"=>"Unauthorized"}, {"code"=>406, "message"=>"The request you made is not acceptable"}, {"code"=>416, "message"=>"Requested Range Not Satisfiable"}], "method"=>"get", "nickname"=>"Api::V1::Sample#index"}
|
215
|
+
#]
|
212
216
|
context "parameters" do
|
213
217
|
it "has correct count" do
|
214
218
|
expect(params.count).to eq 1
|
@@ -245,6 +249,14 @@ describe Swagger::Docs::Generator do
|
|
245
249
|
end
|
246
250
|
end
|
247
251
|
end
|
252
|
+
context "second api (nested)" do
|
253
|
+
let(:api) { response["apis"][1] }
|
254
|
+
context "parameters" do
|
255
|
+
it "has correct count" do
|
256
|
+
expect(params.count).to eq 2
|
257
|
+
end
|
258
|
+
end
|
259
|
+
end
|
248
260
|
end
|
249
261
|
end
|
250
262
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: swagger-docs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rich Hollis
|
@@ -30,7 +30,7 @@ cert_chain:
|
|
30
30
|
RYcsqDfanYBx7QcftOnbeQq7/Ep7Zx+W9+Ph3TiJLMLdAr7bLkgN1SjvrjTL5mQR
|
31
31
|
FuQtYvE4LKiUQpG7vLTRB78dQBlSj9fnv2OM9w==
|
32
32
|
-----END CERTIFICATE-----
|
33
|
-
date: 2014-02-
|
33
|
+
date: 2014-02-14 00:00:00.000000000 Z
|
34
34
|
dependencies:
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
36
|
name: bundler
|
metadata.gz.sig
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
[
|
2
|
-
����
|
3
|
-
|
1
|
+
D���v�v���"���o���/C˞�/rc��HU�>*7�6�T;UCap��F�4�H��j��#u�|5�V���nt0��������ن�Γ���L5�G���cΞ�j������=t �悰[B��I۾�:+76�}���[ݙcXL���v�����dl�
|
2
|
+
6`�����ѽ��m����p�����������l��+�XK|`1�ds�B�{ɫ1'�.m��i��f�7�qY��
|
3
|
+
��tpS��
|