swagger_yard 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +59 -23
- data/lib/swagger_yard/api.rb +12 -17
- data/lib/swagger_yard/api_declaration.rb +4 -35
- data/lib/swagger_yard/configuration.rb +12 -3
- data/lib/swagger_yard/model.rb +0 -15
- data/lib/swagger_yard/operation.rb +22 -67
- data/lib/swagger_yard/parameter.rb +1 -18
- data/lib/swagger_yard/property.rb +0 -4
- data/lib/swagger_yard/resource_listing.rb +1 -3
- data/lib/swagger_yard/type.rb +14 -4
- data/lib/swagger_yard/version.rb +1 -1
- data/lib/swagger_yard.rb +0 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fe5802a169475d76a89dfdefa227b4e70f656851
|
4
|
+
data.tar.gz: 5a384463c1798693067ac7379093f28d1f194312
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0bad20c5172a080f1939eb511eac7b2c6930eb7bd3cf5f408a83100432f453bf9645c47bbbddc5bf35b98ab522d5f6d7e31f7a7277c19efd0607afa1ddfce309
|
7
|
+
data.tar.gz: c6d2040c142826db3ecd4e02fe4a9be55d09369addb94db8ed4b0081f0f87119142b7cc6a7ca7a2ddfd3638a224a26f34af3ee9233a1b9d7ee4db5cb3810edde
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# SwaggerYard #
|
1
|
+
# SwaggerYard [![Build Status](https://travis-ci.org/tpitale/swagger_yard.svg?branch=master)](https://travis-ci.org/tpitale/swagger_yard) #
|
2
2
|
|
3
3
|
SwaggerYard is a gem to convert extended YARD syntax comments into the swagger spec compliant json format.
|
4
4
|
|
@@ -15,30 +15,47 @@ Install the gem with Bunder:
|
|
15
15
|
|
16
16
|
## Getting Started ##
|
17
17
|
|
18
|
-
### Place
|
18
|
+
### Place configuration in a rails initializer ###
|
19
19
|
|
20
20
|
# config/initializers/swagger_yard.rb
|
21
21
|
SwaggerYard.configure do |config|
|
22
|
-
config.swagger_version = "1.2"
|
23
22
|
config.api_version = "1.0"
|
24
23
|
config.reload = Rails.env.development?
|
25
24
|
|
26
25
|
# where your actual api is hosted from
|
27
26
|
config.api_base_path = "http://localhost:3000/api"
|
28
|
-
|
29
|
-
# where your swagger spec json will show up
|
30
|
-
# configure this to match SwaggerYard::Rails mount point
|
31
|
-
config.swagger_spec_base_path = "http://localhost:3000/swagger/api"
|
32
27
|
end
|
33
28
|
|
34
|
-
|
35
|
-
|
29
|
+
## SwaggerYard Usage ##
|
30
|
+
|
31
|
+
### Types ###
|
32
|
+
|
33
|
+
Types of things (parameters or responses of an operation, properties of a model)
|
34
|
+
are indicated inside square-brackets (e.g., `[string]`) as part of a YARD tag.
|
35
|
+
|
36
|
+
- Model references should be Capitalized or CamelCased by convention.
|
37
|
+
- Basic types (integer, boolean, string, etc) should be lowercased.
|
38
|
+
- An array of models or basic types is specified with `[array<...>]`.
|
39
|
+
- An enum of allowed string values is specified with `[enum<one,two,three>]`.
|
40
|
+
|
41
|
+
### Options ###
|
36
42
|
|
37
|
-
|
43
|
+
Parameter or property _options_ are expressed inside parenthesis immediately
|
44
|
+
following the parameter or property name.
|
38
45
|
|
39
|
-
|
46
|
+
Examples:
|
40
47
|
|
41
|
-
|
48
|
+
# @parameter name(required) [string] Name of the package
|
49
|
+
# @parameter package(body) [Package] Package object
|
50
|
+
|
51
|
+
Possible parameters include:
|
52
|
+
|
53
|
+
- `required`: indicates a required parameter or property.
|
54
|
+
- `multiple`: indicates a parameter may appear multiple times (usually in a
|
55
|
+
query string, e.g., `param=a¶m=b¶m=c`)
|
56
|
+
- `body`/`query`/`path`/`formData`: Indicates where the parameter is located.
|
57
|
+
|
58
|
+
### Example of using SwaggerYard in a Controller ###
|
42
59
|
|
43
60
|
```ruby
|
44
61
|
# @resource Account ownership
|
@@ -51,17 +68,13 @@ class Accounts::OwnershipsController < ActionController::Base
|
|
51
68
|
##
|
52
69
|
# Returns a list of ownerships associated with the account.
|
53
70
|
#
|
54
|
-
#
|
71
|
+
# Status can be -1(Deleted), 0(Inactive), 1(Active), 2(Expired) and 3(Cancelled).
|
55
72
|
#
|
56
|
-
# @path [GET] /accounts/ownerships
|
73
|
+
# @path [GET] /accounts/ownerships
|
57
74
|
#
|
58
75
|
# @parameter offset [integer] Used for pagination of response data (default: 25 items per response). Specifies the offset of the next block of data to receive.
|
59
|
-
# @parameter status [array<string>]
|
60
|
-
# @
|
61
|
-
# [List] id
|
62
|
-
# [List] begin_at
|
63
|
-
# [List] end_at
|
64
|
-
# [List] created_at
|
76
|
+
# @parameter status [array<string>] Filter by status. (e.g. status[]=1&status[]=2&status[]=3).
|
77
|
+
# @parameter sort_order [enum<id,begin_at,end_at,created_at>] Orders response by fields. (e.g. sort_order=created_at).
|
65
78
|
# @parameter sort_descending [boolean] Reverse order of sort_order sorting, make it descending.
|
66
79
|
# @parameter begin_at_greater [date] Filters response to include only items with begin_at >= specified timestamp (e.g. begin_at_greater=2012-02-15T02:06:56Z).
|
67
80
|
# @parameter begin_at_less [date] Filters response to include only items with begin_at <= specified timestamp (e.g. begin_at_less=2012-02-15T02:06:56Z).
|
@@ -75,7 +88,7 @@ class Accounts::OwnershipsController < ActionController::Base
|
|
75
88
|
##
|
76
89
|
# Returns an ownership for an account by id
|
77
90
|
#
|
78
|
-
# @path [GET] /accounts/ownerships/{id}
|
91
|
+
# @path [GET] /accounts/ownerships/{id}
|
79
92
|
# @response_type [Ownership]
|
80
93
|
# @error_message [EmptyOwnership] 404 Ownership not found
|
81
94
|
# @error_message 400 Invalid ID supplied
|
@@ -86,7 +99,7 @@ class Accounts::OwnershipsController < ActionController::Base
|
|
86
99
|
end
|
87
100
|
```
|
88
101
|
|
89
|
-
###
|
102
|
+
### Example of using SwaggerYard in a Model ###
|
90
103
|
|
91
104
|
```ruby
|
92
105
|
#
|
@@ -104,7 +117,7 @@ end
|
|
104
117
|
To then use your `Model` in your `Controller` documentation, add `@parameter`s:
|
105
118
|
|
106
119
|
```ruby
|
107
|
-
# @parameter [Pet]
|
120
|
+
# @parameter pet(body) [Pet] The pet object
|
108
121
|
```
|
109
122
|
|
110
123
|
## Authorization ##
|
@@ -144,6 +157,7 @@ To generate JSON from your code on request, checkout the [swagger_yard-rails](ht
|
|
144
157
|
|
145
158
|
## Current Parsing "Tree" Structure ##
|
146
159
|
|
160
|
+
```
|
147
161
|
ResourceListing
|
148
162
|
|
|
149
163
|
-> ApiDeclaration (controller)
|
@@ -159,3 +173,25 @@ ResourceListing
|
|
159
173
|
-> Model (model)
|
160
174
|
|
|
161
175
|
-> Properties (model attributes)
|
176
|
+
```
|
177
|
+
|
178
|
+
### Path Discovery Function ##
|
179
|
+
|
180
|
+
SwaggerYard configuration allows setting of a "path discovery function" which
|
181
|
+
will be called for controller action method documentation that have no `@path`
|
182
|
+
tag. The function should return an array containing `["<method>", "<path>"]` if
|
183
|
+
any can be determined.
|
184
|
+
|
185
|
+
```ruby
|
186
|
+
SwaggerYard.configure do |config|
|
187
|
+
config.path_discovery_function = ->(yard_obj) do
|
188
|
+
# code here to inspect the yard doc object
|
189
|
+
# and return a [method, path] for the api
|
190
|
+
end
|
191
|
+
end
|
192
|
+
```
|
193
|
+
|
194
|
+
In [swagger_yard-rails][], this hook is used to set a function that inspects the
|
195
|
+
Rails routing tables to reverse look up and compute paths.
|
196
|
+
|
197
|
+
[swagger_yard-rails]: https://github.com/tpitale/swagger-yard_rails
|
data/lib/swagger_yard/api.rb
CHANGED
@@ -1,27 +1,30 @@
|
|
1
1
|
module SwaggerYard
|
2
2
|
class Api
|
3
|
-
attr_accessor :path, :
|
3
|
+
attr_accessor :path, :operations, :api_declaration
|
4
4
|
|
5
5
|
def self.path_from_yard_object(yard_object)
|
6
6
|
if tag = yard_object.tags.detect {|t| t.tag_name == "path"}
|
7
7
|
tag.text
|
8
|
-
|
9
|
-
|
8
|
+
elsif fn = SwaggerYard.config.path_discovery_function
|
9
|
+
begin
|
10
|
+
method, path = fn[yard_object]
|
11
|
+
yard_object.add_tag YARD::Tags::Tag.new("path", path, [method]) if path
|
12
|
+
path
|
13
|
+
rescue => e
|
14
|
+
YARD::Logger.instance.warn e.message
|
15
|
+
nil
|
16
|
+
end
|
10
17
|
end
|
11
18
|
end
|
12
19
|
|
13
20
|
def self.from_yard_object(yard_object, api_declaration)
|
14
21
|
path = path_from_yard_object(yard_object)
|
15
|
-
|
16
|
-
|
17
|
-
new(path, description, api_declaration)
|
22
|
+
new(path, api_declaration)
|
18
23
|
end
|
19
24
|
|
20
|
-
def initialize(path,
|
25
|
+
def initialize(path, api_declaration)
|
21
26
|
@api_declaration = api_declaration
|
22
|
-
@description = description
|
23
27
|
@path = path
|
24
|
-
|
25
28
|
@operations = []
|
26
29
|
end
|
27
30
|
|
@@ -29,14 +32,6 @@ module SwaggerYard
|
|
29
32
|
@operations << Operation.from_yard_object(yard_object, self)
|
30
33
|
end
|
31
34
|
|
32
|
-
def model_names
|
33
|
-
@operations.map(&:model_names).flatten.compact.uniq
|
34
|
-
end
|
35
|
-
|
36
|
-
def ref?(data_type)
|
37
|
-
@api_declaration.ref?(data_type)
|
38
|
-
end
|
39
|
-
|
40
35
|
def operations_hash
|
41
36
|
Hash[@operations.map {|op| [op.http_method.downcase, op.to_h]}]
|
42
37
|
end
|
@@ -5,13 +5,13 @@ module SwaggerYard
|
|
5
5
|
|
6
6
|
def initialize(resource_listing)
|
7
7
|
@resource_listing = resource_listing
|
8
|
-
|
9
|
-
@apis
|
10
|
-
@authorizations
|
8
|
+
@resource = nil
|
9
|
+
@apis = {}
|
10
|
+
@authorizations = {}
|
11
11
|
end
|
12
12
|
|
13
13
|
def valid?
|
14
|
-
!@
|
14
|
+
!@resource.nil?
|
15
15
|
end
|
16
16
|
|
17
17
|
def add_yard_objects(yard_objects)
|
@@ -56,18 +56,6 @@ module SwaggerYard
|
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
|
-
def resource_name
|
60
|
-
@resource_path
|
61
|
-
end
|
62
|
-
|
63
|
-
def models
|
64
|
-
(api_models + property_models).uniq
|
65
|
-
end
|
66
|
-
|
67
|
-
def ref?(name)
|
68
|
-
@resource_listing.models.map(&:id).include?(name)
|
69
|
-
end
|
70
|
-
|
71
59
|
def apis_hash
|
72
60
|
Hash[apis.map {|path, api| [path, api.operations_hash]}]
|
73
61
|
end
|
@@ -76,24 +64,5 @@ module SwaggerYard
|
|
76
64
|
{ "name" => resource,
|
77
65
|
"description" => description }
|
78
66
|
end
|
79
|
-
|
80
|
-
private
|
81
|
-
def model_names_from_apis
|
82
|
-
apis.values.map(&:model_names).flatten.uniq
|
83
|
-
end
|
84
|
-
|
85
|
-
# models selected by the names of models referenced in APIs
|
86
|
-
def api_models
|
87
|
-
@resource_listing.models.select {|m| model_names_from_apis.include?(m.id)}
|
88
|
-
end
|
89
|
-
|
90
|
-
def model_names_from_model_properties
|
91
|
-
api_models.map{|model| model.recursive_properties_model_names(@resource_listing.models) }.flatten.uniq
|
92
|
-
end
|
93
|
-
|
94
|
-
# models selected by names used in properties in models used in APIs
|
95
|
-
def property_models
|
96
|
-
@resource_listing.models.select {|m| model_names_from_model_properties.include?(m.id)}
|
97
|
-
end
|
98
67
|
end
|
99
68
|
end
|
@@ -1,18 +1,27 @@
|
|
1
1
|
module SwaggerYard
|
2
2
|
class Configuration
|
3
|
-
attr_accessor :
|
4
|
-
attr_accessor :swagger_version
|
3
|
+
attr_accessor :api_version, :api_base_path
|
4
|
+
attr_accessor :swagger_version
|
5
5
|
attr_accessor :title, :description
|
6
6
|
attr_accessor :enable, :reload
|
7
7
|
attr_accessor :controller_path, :model_path
|
8
|
+
attr_accessor :path_discovery_function
|
8
9
|
|
9
10
|
def initialize
|
10
|
-
self.swagger_version = "
|
11
|
+
self.swagger_version = "2.0"
|
11
12
|
self.api_version = "0.1"
|
12
13
|
self.enable = false
|
13
14
|
self.reload = true
|
14
15
|
self.title = "Configure title with SwaggerYard.config.title"
|
15
16
|
self.description = "Configure description with SwaggerYard.config.description"
|
16
17
|
end
|
18
|
+
|
19
|
+
def swagger_spec_base_path=(ignored)
|
20
|
+
warn "DEPRECATED: swagger_spec_base_path is no longer necessary."
|
21
|
+
end
|
22
|
+
|
23
|
+
def api_path=(ignored)
|
24
|
+
warn "DEPRECATED: api_path is no longer necessary."
|
25
|
+
end
|
17
26
|
end
|
18
27
|
end
|
data/lib/swagger_yard/model.rb
CHANGED
@@ -41,21 +41,6 @@ module SwaggerYard
|
|
41
41
|
self
|
42
42
|
end
|
43
43
|
|
44
|
-
def properties_model_names
|
45
|
-
@properties.map(&:model_name).compact
|
46
|
-
end
|
47
|
-
|
48
|
-
def recursive_properties_model_names(model_list)
|
49
|
-
properties_model_names + properties_model_names.map do |model_name|
|
50
|
-
child_model = model_from_model_list(model_list, model_name)
|
51
|
-
child_model.recursive_properties_model_names(model_list) if child_model
|
52
|
-
end.compact
|
53
|
-
end
|
54
|
-
|
55
|
-
def model_from_model_list(model_list, model_name)
|
56
|
-
model_list.find{|model| model.id == model_name}
|
57
|
-
end
|
58
|
-
|
59
44
|
def to_h
|
60
45
|
{}.tap do |h|
|
61
46
|
h["properties"] = Hash[@properties.map {|p| [p.name, p.to_h]}]
|
@@ -1,55 +1,50 @@
|
|
1
1
|
module SwaggerYard
|
2
2
|
class Operation
|
3
|
-
attr_accessor :
|
4
|
-
|
3
|
+
attr_accessor :description, :ruby_method
|
4
|
+
attr_writer :summary
|
5
|
+
attr_reader :path, :http_method, :error_messages, :response_type, :response_desc
|
5
6
|
attr_reader :parameters, :model_names
|
6
7
|
|
7
|
-
PARAMETER_LIST_REGEX = /\A\[(\w*)\]\s*(\w*)(\(required\))?\s*(.*)\n([.\s\S]*)\Z/
|
8
|
-
|
9
8
|
# TODO: extract to operation builder?
|
10
9
|
def self.from_yard_object(yard_object, api)
|
11
10
|
new(api).tap do |operation|
|
12
11
|
operation.ruby_method = yard_object.name(false)
|
12
|
+
operation.description = yard_object.docstring
|
13
13
|
yard_object.tags.each do |tag|
|
14
14
|
case tag.tag_name
|
15
15
|
when "path"
|
16
16
|
operation.add_path_params_and_method(tag)
|
17
17
|
when "parameter"
|
18
18
|
operation.add_parameter(tag)
|
19
|
-
when "parameter_list"
|
20
|
-
operation.add_parameter_list(tag)
|
21
19
|
when "response_type"
|
22
|
-
operation.add_response_type(Type.from_type_list(tag.types))
|
20
|
+
operation.add_response_type(Type.from_type_list(tag.types), tag.text)
|
23
21
|
when "error_message"
|
24
22
|
operation.add_error_message(tag)
|
25
23
|
when "summary"
|
26
24
|
operation.summary = tag.text
|
27
|
-
when "notes"
|
28
|
-
operation.notes = tag.text.gsub("\n", "<br\>")
|
29
25
|
end
|
30
26
|
end
|
31
27
|
|
32
28
|
operation.sort_parameters
|
33
|
-
operation.append_format_parameter
|
34
29
|
end
|
35
30
|
end
|
36
31
|
|
37
32
|
def initialize(api)
|
38
|
-
@api
|
39
|
-
@
|
40
|
-
@
|
33
|
+
@api = api
|
34
|
+
@summary = nil
|
35
|
+
@description = ""
|
36
|
+
@parameters = []
|
37
|
+
@model_names = []
|
41
38
|
@error_messages = []
|
42
39
|
end
|
43
40
|
|
44
|
-
def
|
45
|
-
@
|
41
|
+
def summary
|
42
|
+
@summary || description.split("\n\n").first
|
46
43
|
end
|
47
44
|
|
48
45
|
def to_h
|
49
|
-
method = http_method.downcase
|
50
|
-
description = notes || ""
|
51
46
|
params = parameters.map(&:to_h)
|
52
|
-
responses = { "default" => { "description" =>
|
47
|
+
responses = { "default" => { "description" => response_desc || summary } }
|
53
48
|
|
54
49
|
if response_type
|
55
50
|
responses["default"]["schema"] = response_type.to_h
|
@@ -65,13 +60,13 @@ module SwaggerYard
|
|
65
60
|
end
|
66
61
|
|
67
62
|
{
|
68
|
-
"summary" => summary || @api.description,
|
69
63
|
"tags" => [@api.api_declaration.resource].compact,
|
70
64
|
"operationId" => "#{@api.api_declaration.resource}-#{ruby_method}",
|
71
65
|
"parameters" => params,
|
72
66
|
"responses" => responses,
|
73
67
|
}.tap do |h|
|
74
|
-
h["description"] = description unless description.
|
68
|
+
h["description"] = description unless description.empty?
|
69
|
+
h["summary"] = summary unless summary.empty?
|
75
70
|
|
76
71
|
authorizations = @api.api_declaration.authorizations
|
77
72
|
unless authorizations.empty?
|
@@ -81,8 +76,8 @@ module SwaggerYard
|
|
81
76
|
end
|
82
77
|
|
83
78
|
##
|
84
|
-
# Example: [GET] /api/v2/ownerships
|
85
|
-
# Example: [PUT] /api/v1/accounts/{account_id}
|
79
|
+
# Example: [GET] /api/v2/ownerships
|
80
|
+
# Example: [PUT] /api/v1/accounts/{account_id}
|
86
81
|
def add_path_params_and_method(tag)
|
87
82
|
@path = tag.text
|
88
83
|
@http_method = tag.types.first
|
@@ -102,27 +97,12 @@ module SwaggerYard
|
|
102
97
|
end
|
103
98
|
|
104
99
|
##
|
105
|
-
# Example:
|
106
|
-
#
|
107
|
-
|
108
|
-
# [List] end_at
|
109
|
-
# [List] created_at
|
110
|
-
def add_parameter_list(tag)
|
111
|
-
# TODO: switch to using Parameter.from_yard_tag
|
112
|
-
data_type, name, required, description, list_string = parse_parameter_list(tag)
|
113
|
-
allowable_values = parse_list_values(list_string)
|
114
|
-
|
115
|
-
@parameters << Parameter.new(name, Type.new(data_type.downcase), description, {
|
116
|
-
required: !!required,
|
117
|
-
param_type: "query",
|
118
|
-
allow_multiple: false,
|
119
|
-
allowable_values: allowable_values
|
120
|
-
})
|
121
|
-
end
|
122
|
-
|
123
|
-
def add_response_type(type)
|
100
|
+
# Example:
|
101
|
+
# @response_type [Ownership] the requested ownership
|
102
|
+
def add_response_type(type, desc)
|
124
103
|
model_names << type.model_name
|
125
104
|
@response_type = type
|
105
|
+
@response_desc = desc
|
126
106
|
end
|
127
107
|
|
128
108
|
def add_error_message(tag)
|
@@ -137,34 +117,9 @@ module SwaggerYard
|
|
137
117
|
@parameters.sort_by! {|p| p.name}
|
138
118
|
end
|
139
119
|
|
140
|
-
def append_format_parameter
|
141
|
-
@parameters << format_parameter
|
142
|
-
end
|
143
|
-
|
144
|
-
def ref?(data_type)
|
145
|
-
@api.ref?(data_type)
|
146
|
-
end
|
147
|
-
|
148
120
|
private
|
149
121
|
def parse_path_params(path)
|
150
|
-
path.scan(/\{([^\}]+)\}/).flatten
|
151
|
-
end
|
152
|
-
|
153
|
-
def parse_parameter_list(tag)
|
154
|
-
tag.text.match(PARAMETER_LIST_REGEX).captures
|
155
|
-
end
|
156
|
-
|
157
|
-
def parse_list_values(list_string)
|
158
|
-
list_string.split("[List]").map(&:strip).reject { |string| string.empty? }
|
159
|
-
end
|
160
|
-
|
161
|
-
def format_parameter
|
162
|
-
Parameter.new("format_type", Type.new("string"), "Response format either JSON or XML", {
|
163
|
-
required: true,
|
164
|
-
param_type: "path",
|
165
|
-
allow_multiple: false,
|
166
|
-
allowable_values: ["json", "xml"]
|
167
|
-
})
|
122
|
+
path.scan(/\{([^\}]+)\}/).flatten
|
168
123
|
end
|
169
124
|
end
|
170
125
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module SwaggerYard
|
2
2
|
class Parameter
|
3
3
|
attr_accessor :name, :description
|
4
|
-
attr_reader :param_type, :required, :allow_multiple
|
4
|
+
attr_reader :param_type, :required, :allow_multiple
|
5
5
|
|
6
6
|
def self.from_yard_tag(tag, operation)
|
7
7
|
description = tag.text
|
@@ -38,20 +38,6 @@ module SwaggerYard
|
|
38
38
|
@required = options[:required] || false
|
39
39
|
@param_type = options[:param_type] || 'query'
|
40
40
|
@allow_multiple = options[:allow_multiple] || false
|
41
|
-
@allowable_values = options[:allowable_values] || []
|
42
|
-
end
|
43
|
-
|
44
|
-
def type
|
45
|
-
@type.name
|
46
|
-
end
|
47
|
-
|
48
|
-
def allowable_values_hash
|
49
|
-
return nil if allowable_values.empty?
|
50
|
-
|
51
|
-
{
|
52
|
-
"valueType" => "LIST",
|
53
|
-
"values" => allowable_values
|
54
|
-
}
|
55
41
|
end
|
56
42
|
|
57
43
|
def to_h
|
@@ -60,9 +46,6 @@ module SwaggerYard
|
|
60
46
|
"required" => required,
|
61
47
|
"in" => param_type
|
62
48
|
}.tap do |h|
|
63
|
-
if !Array(allowable_values).empty?
|
64
|
-
h["enum"] = allowable_values
|
65
|
-
end
|
66
49
|
if h["in"] == "body"
|
67
50
|
h["schema"] = @type.to_h
|
68
51
|
else
|
data/lib/swagger_yard/type.rb
CHANGED
@@ -2,13 +2,20 @@ module SwaggerYard
|
|
2
2
|
class Type
|
3
3
|
def self.from_type_list(types)
|
4
4
|
parts = types.first.split(/[<>]/)
|
5
|
-
|
5
|
+
args = [parts.last]
|
6
|
+
case parts.first
|
7
|
+
when /^array$/i
|
8
|
+
args << true
|
9
|
+
when /^enum$/i
|
10
|
+
args = [nil, false, parts.last.split(/[,|]/)]
|
11
|
+
end if parts.size > 1
|
12
|
+
new(*args)
|
6
13
|
end
|
7
14
|
|
8
|
-
attr_reader :name, :array
|
15
|
+
attr_reader :name, :array, :enum
|
9
16
|
|
10
|
-
def initialize(name, array=false)
|
11
|
-
@name, @array = name, array
|
17
|
+
def initialize(name, array = false, enum = nil)
|
18
|
+
@name, @array, @enum = name, array, enum
|
12
19
|
end
|
13
20
|
|
14
21
|
# TODO: have this look at resource listing?
|
@@ -21,6 +28,7 @@ module SwaggerYard
|
|
21
28
|
end
|
22
29
|
|
23
30
|
alias :array? :array
|
31
|
+
alias :enum? :enum
|
24
32
|
|
25
33
|
def json_type
|
26
34
|
type, format = name, nil
|
@@ -41,6 +49,8 @@ module SwaggerYard
|
|
41
49
|
def to_h
|
42
50
|
type = if ref?
|
43
51
|
{ "$ref" => "#/definitions/#{name}"}
|
52
|
+
elsif enum?
|
53
|
+
{ "type" => "string", "enum" => @enum }
|
44
54
|
else
|
45
55
|
json_type
|
46
56
|
end
|
data/lib/swagger_yard/version.rb
CHANGED
data/lib/swagger_yard.rb
CHANGED
@@ -52,9 +52,7 @@ module SwaggerYard
|
|
52
52
|
::YARD::Tags::Library.define_tag("Resource path", :resource_path)
|
53
53
|
::YARD::Tags::Library.define_tag("Api path", :path, :with_types)
|
54
54
|
::YARD::Tags::Library.define_tag("Parameter", :parameter, :with_types_name_and_default)
|
55
|
-
::YARD::Tags::Library.define_tag("Parameter list", :parameter_list)
|
56
55
|
::YARD::Tags::Library.define_tag("Status code", :status_code)
|
57
|
-
::YARD::Tags::Library.define_tag("Implementation notes", :notes)
|
58
56
|
::YARD::Tags::Library.define_tag("Response type", :response_type, :with_types)
|
59
57
|
::YARD::Tags::Library.define_tag("Error response message", :error_message, :with_types_and_name)
|
60
58
|
::YARD::Tags::Library.define_tag("Api Summary", :summary)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: swagger_yard
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- chtrinh (Chris Trinh)
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-11-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: yard
|