apipie-rails 0.3.6 → 0.5.17
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +23 -7
- data/CHANGELOG.md +147 -2
- data/Gemfile +1 -0
- data/Gemfile.rails41 +2 -0
- data/Gemfile.rails42 +10 -1
- data/Gemfile.rails50 +9 -0
- data/Gemfile.rails51 +9 -0
- data/Gemfile.rails60 +14 -0
- data/PROPOSAL_FOR_RESPONSE_DESCRIPTIONS.md +244 -0
- data/README.rst +570 -17
- data/apipie-rails.gemspec +3 -3
- data/app/controllers/apipie/apipies_controller.rb +48 -17
- data/app/views/apipie/apipies/_method_detail.erb +21 -0
- data/app/views/apipie/apipies/_params.html.erb +4 -2
- data/app/views/apipie/apipies/index.html.erb +5 -1
- data/app/views/apipie/apipies/resource.html.erb +3 -0
- data/app/views/layouts/apipie/apipie.html.erb +1 -1
- data/config/locales/en.yml +1 -0
- data/config/locales/fr.yml +31 -0
- data/config/locales/it.yml +31 -0
- data/config/locales/ja.yml +31 -0
- data/lib/apipie/apipie_module.rb +22 -4
- data/lib/apipie/application.rb +55 -28
- data/lib/apipie/configuration.rb +19 -3
- data/lib/apipie/core_ext/route.rb +9 -0
- data/lib/apipie/dsl_definition.rb +151 -10
- data/lib/apipie/error_description.rb +9 -2
- data/lib/apipie/errors.rb +34 -0
- data/lib/apipie/extractor/collector.rb +4 -0
- data/lib/apipie/extractor/recorder.rb +13 -12
- data/lib/apipie/extractor/writer.rb +83 -55
- data/lib/apipie/extractor.rb +10 -4
- data/lib/apipie/method_description.rb +51 -4
- data/lib/apipie/param_description.rb +56 -2
- data/lib/apipie/resource_description.rb +10 -3
- data/lib/apipie/response_description.rb +131 -0
- data/lib/apipie/response_description_adapter.rb +200 -0
- data/lib/apipie/routes_formatter.rb +1 -1
- data/lib/apipie/rspec/response_validation_helper.rb +194 -0
- data/lib/apipie/static_dispatcher.rb +3 -2
- data/lib/apipie/swagger_generator.rb +708 -0
- data/lib/apipie/tag_list_description.rb +11 -0
- data/lib/apipie/validator.rb +69 -8
- data/lib/apipie/version.rb +1 -1
- data/lib/apipie-rails.rb +7 -0
- data/lib/tasks/apipie.rake +103 -8
- data/spec/controllers/apipies_controller_spec.rb +52 -12
- data/spec/controllers/concerns_controller_spec.rb +2 -2
- data/spec/controllers/extended_controller_spec.rb +14 -0
- data/spec/controllers/memes_controller_spec.rb +10 -0
- data/spec/controllers/users_controller_spec.rb +115 -75
- data/spec/dummy/app/controllers/application_controller.rb +5 -1
- data/spec/dummy/app/controllers/concerns/extending_concern.rb +12 -0
- data/spec/dummy/app/controllers/concerns/sample_controller.rb +5 -5
- data/spec/dummy/app/controllers/extended_controller.rb +14 -0
- data/spec/dummy/app/controllers/pets_controller.rb +408 -0
- data/spec/dummy/app/controllers/pets_using_auto_views_controller.rb +73 -0
- data/spec/dummy/app/controllers/pets_using_self_describing_classes_controller.rb +95 -0
- data/spec/dummy/app/controllers/tagged_cats_controller.rb +32 -0
- data/spec/dummy/app/controllers/tagged_dogs_controller.rb +15 -0
- data/spec/dummy/app/controllers/twitter_example_controller.rb +5 -0
- data/spec/dummy/app/controllers/users_controller.rb +19 -11
- data/spec/dummy/components/test_engine/Gemfile +6 -0
- data/spec/dummy/components/test_engine/app/controllers/test_engine/application_controller.rb +4 -0
- data/spec/dummy/components/test_engine/app/controllers/test_engine/memes_controller.rb +37 -0
- data/spec/dummy/components/test_engine/config/routes.rb +3 -0
- data/spec/dummy/components/test_engine/db/.gitkeep +0 -0
- data/spec/dummy/components/test_engine/lib/test_engine.rb +7 -0
- data/spec/dummy/components/test_engine/test_engine.gemspec +11 -0
- data/spec/dummy/config/application.rb +5 -0
- data/spec/dummy/config/environments/development.rb +3 -0
- data/spec/dummy/config/environments/production.rb +3 -0
- data/spec/dummy/config/environments/test.rb +3 -0
- data/spec/dummy/config/initializers/apipie.rb +3 -1
- data/spec/dummy/config/routes.rb +24 -1
- data/spec/lib/extractor/writer_spec.rb +32 -4
- data/spec/lib/file_handler_spec.rb +18 -0
- data/spec/lib/method_description_spec.rb +34 -0
- data/spec/lib/swagger/openapi_2_0_schema.json +1607 -0
- data/spec/lib/swagger/rake_swagger_spec.rb +139 -0
- data/spec/lib/swagger/response_validation_spec.rb +104 -0
- data/spec/lib/swagger/swagger_dsl_spec.rb +658 -0
- data/spec/lib/validator_spec.rb +58 -0
- data/spec/lib/validators/array_validator_spec.rb +28 -8
- data/spec/spec_helper.rb +68 -0
- metadata +75 -23
- data/Gemfile +0 -7
- data/Gemfile.rails32 +0 -6
- data/Gemfile.rails40 +0 -5
- data/lib/apipie/client/generator.rb +0 -135
@@ -5,7 +5,7 @@ module Apipie
|
|
5
5
|
|
6
6
|
class Api
|
7
7
|
|
8
|
-
attr_accessor :short_description, :path, :http_method, :from_routes, :options
|
8
|
+
attr_accessor :short_description, :path, :http_method, :from_routes, :options, :returns
|
9
9
|
|
10
10
|
def initialize(method, path, desc, options)
|
11
11
|
@http_method = method.to_s
|
@@ -17,7 +17,8 @@ module Apipie
|
|
17
17
|
|
18
18
|
end
|
19
19
|
|
20
|
-
attr_reader :full_description, :method, :resource, :apis, :examples, :see, :formats, :
|
20
|
+
attr_reader :full_description, :method, :resource, :apis, :examples, :see, :formats, :headers, :show
|
21
|
+
attr_accessor :metadata
|
21
22
|
|
22
23
|
def initialize(method, resource, dsl_data)
|
23
24
|
@method = method.to_s
|
@@ -34,6 +35,12 @@ module Apipie
|
|
34
35
|
Apipie::ErrorDescription.from_dsl_data(args)
|
35
36
|
end
|
36
37
|
|
38
|
+
@tag_list = dsl_data[:tag_list]
|
39
|
+
|
40
|
+
@returns = dsl_data[:returns].map do |code,args|
|
41
|
+
Apipie::ResponseDescription.from_dsl_data(self, code, args)
|
42
|
+
end
|
43
|
+
|
37
44
|
@see = dsl_data[:see].map do |args|
|
38
45
|
Apipie::SeeDescription.new(args)
|
39
46
|
end
|
@@ -46,7 +53,8 @@ module Apipie
|
|
46
53
|
|
47
54
|
@params_ordered = dsl_data[:params].map do |args|
|
48
55
|
Apipie::ParamDescription.from_dsl_data(self, args)
|
49
|
-
end
|
56
|
+
end.reject{|p| p.response_only? }
|
57
|
+
|
50
58
|
@params_ordered = ParamDescription.unify(@params_ordered)
|
51
59
|
@headers = dsl_data[:headers]
|
52
60
|
|
@@ -65,6 +73,10 @@ module Apipie
|
|
65
73
|
params_ordered.reduce(ActiveSupport::OrderedHash.new) { |h,p| h[p.name] = p; h }
|
66
74
|
end
|
67
75
|
|
76
|
+
def params_ordered_self
|
77
|
+
@params_ordered
|
78
|
+
end
|
79
|
+
|
68
80
|
def params_ordered
|
69
81
|
all_params = []
|
70
82
|
parent = Apipie.get_resource_description(@resource.controller.superclass)
|
@@ -81,6 +93,34 @@ module Apipie
|
|
81
93
|
all_params.find_all(&:validator)
|
82
94
|
end
|
83
95
|
|
96
|
+
def returns_self
|
97
|
+
@returns
|
98
|
+
end
|
99
|
+
|
100
|
+
def tag_list
|
101
|
+
all_tag_list = []
|
102
|
+
parent = Apipie.get_resource_description(@resource.controller.superclass)
|
103
|
+
|
104
|
+
# get tags from parent resource description
|
105
|
+
parent_tags = [parent, @resource].compact.flat_map { |resource| resource._tag_list_arg }
|
106
|
+
Apipie::TagListDescription.new((parent_tags + @tag_list).uniq.compact)
|
107
|
+
end
|
108
|
+
|
109
|
+
def returns
|
110
|
+
all_returns = []
|
111
|
+
parent = Apipie.get_resource_description(@resource.controller.superclass)
|
112
|
+
|
113
|
+
# get response descriptions from parent resource description
|
114
|
+
[parent, @resource].compact.each do |resource|
|
115
|
+
resource_returns = resource._returns_args.map do |code, args|
|
116
|
+
Apipie::ResponseDescription.from_dsl_data(self, code, args)
|
117
|
+
end
|
118
|
+
merge_returns(all_returns, resource_returns)
|
119
|
+
end
|
120
|
+
|
121
|
+
merge_returns(all_returns, @returns)
|
122
|
+
end
|
123
|
+
|
84
124
|
def errors
|
85
125
|
return @merged_errors if @merged_errors
|
86
126
|
@merged_errors = []
|
@@ -125,7 +165,7 @@ module Apipie
|
|
125
165
|
:api_url => create_api_url(api),
|
126
166
|
:http_method => api.http_method.to_s,
|
127
167
|
:short_description => Apipie.app.translate(api.short_description, lang),
|
128
|
-
:deprecated => api.options[:deprecated]
|
168
|
+
:deprecated => resource._deprecated || api.options[:deprecated]
|
129
169
|
}
|
130
170
|
end
|
131
171
|
end
|
@@ -147,6 +187,7 @@ module Apipie
|
|
147
187
|
:full_description => Apipie.app.translate(@full_description, lang),
|
148
188
|
:errors => errors.map(&:to_json),
|
149
189
|
:params => params_ordered.map{ |param| param.to_json(lang) }.flatten,
|
190
|
+
:returns => @returns.map{ |return_item| return_item.to_json(lang) }.flatten,
|
150
191
|
:examples => @examples,
|
151
192
|
:metadata => @metadata,
|
152
193
|
:see => see.map(&:to_json),
|
@@ -185,6 +226,12 @@ module Apipie
|
|
185
226
|
params.concat(new_params)
|
186
227
|
end
|
187
228
|
|
229
|
+
def merge_returns(returns, new_returns)
|
230
|
+
new_return_codes = Set.new(new_returns.map(&:code))
|
231
|
+
returns.delete_if { |p| new_return_codes.include?(p.code) }
|
232
|
+
returns.concat(new_returns)
|
233
|
+
end
|
234
|
+
|
188
235
|
def load_recorded_examples
|
189
236
|
(Apipie.recorded_examples[id] || []).
|
190
237
|
find_all { |ex| ex["show_in_doc"].to_i > 0 }.
|
@@ -8,9 +8,14 @@ module Apipie
|
|
8
8
|
# validator - Validator::BaseValidator subclass
|
9
9
|
class ParamDescription
|
10
10
|
|
11
|
-
attr_reader :method_description, :name, :desc, :allow_nil, :validator, :options, :metadata, :show, :as, :validations
|
11
|
+
attr_reader :method_description, :name, :desc, :allow_nil, :allow_blank, :validator, :options, :metadata, :show, :as, :validations, :response_only, :request_only
|
12
|
+
attr_reader :additional_properties, :is_array
|
12
13
|
attr_accessor :parent, :required
|
13
14
|
|
15
|
+
alias_method :response_only?, :response_only
|
16
|
+
alias_method :request_only?, :request_only
|
17
|
+
alias_method :is_array?, :is_array
|
18
|
+
|
14
19
|
def self.from_dsl_data(method_description, args)
|
15
20
|
param_name, validator, desc_or_options, options, block = args
|
16
21
|
Apipie::ParamDescription.new(method_description,
|
@@ -21,6 +26,19 @@ module Apipie
|
|
21
26
|
&block)
|
22
27
|
end
|
23
28
|
|
29
|
+
def to_s
|
30
|
+
"ParamDescription: #{method_description.id}##{name}"
|
31
|
+
end
|
32
|
+
|
33
|
+
def ==(other)
|
34
|
+
return false unless self.class == other.class
|
35
|
+
if method_description == other.method_description && @options == other.options
|
36
|
+
true
|
37
|
+
else
|
38
|
+
false
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
24
42
|
def initialize(method_description, name, validator, desc_or_options = nil, options = {}, &block)
|
25
43
|
|
26
44
|
if desc_or_options.is_a?(Hash)
|
@@ -49,6 +67,10 @@ module Apipie
|
|
49
67
|
|
50
68
|
@required = is_required?
|
51
69
|
|
70
|
+
@response_only = (@options[:only_in] == :response)
|
71
|
+
@request_only = (@options[:only_in] == :request)
|
72
|
+
raise ArgumentError.new("'#{@options[:only_in]}' is not a valid value for :only_in") if (!@response_only && !@request_only) && @options[:only_in].present?
|
73
|
+
|
52
74
|
@show = if @options.has_key? :show
|
53
75
|
@options[:show]
|
54
76
|
else
|
@@ -56,23 +78,45 @@ module Apipie
|
|
56
78
|
end
|
57
79
|
|
58
80
|
@allow_nil = @options[:allow_nil] || false
|
81
|
+
@allow_blank = @options[:allow_blank] || false
|
59
82
|
|
60
83
|
action_awareness
|
61
84
|
|
62
85
|
if validator
|
86
|
+
if (validator != Hash) && (validator.is_a? Hash) && (validator[:array_of])
|
87
|
+
@is_array = true
|
88
|
+
rest_of_options = validator
|
89
|
+
validator = validator[:array_of]
|
90
|
+
options.merge!(rest_of_options.select{|k,v| k != :array_of })
|
91
|
+
raise "an ':array_of =>' validator is allowed exclusively on response-only fields" unless @response_only
|
92
|
+
end
|
63
93
|
@validator = Validator::BaseValidator.find(self, validator, @options, block)
|
64
94
|
raise "Validator for #{validator} not found." unless @validator
|
65
95
|
end
|
66
96
|
|
67
97
|
@validations = Array(options[:validations]).map {|v| concern_subst(Apipie.markup_to_html(v)) }
|
98
|
+
|
99
|
+
@additional_properties = @options[:additional_properties]
|
68
100
|
end
|
69
101
|
|
70
102
|
def from_concern?
|
71
103
|
method_description.from_concern? || @from_concern
|
72
104
|
end
|
73
105
|
|
106
|
+
def normalized_value(value)
|
107
|
+
if value.is_a?(ActionController::Parameters) && !value.is_a?(Hash)
|
108
|
+
value.to_unsafe_hash
|
109
|
+
elsif value.is_a? Array
|
110
|
+
value.map { |v| normalized_value (v) }
|
111
|
+
else
|
112
|
+
value
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
74
116
|
def validate(value)
|
75
117
|
return true if @allow_nil && value.nil?
|
118
|
+
return true if @allow_blank && value.blank?
|
119
|
+
value = normalized_value(value)
|
76
120
|
if (!@allow_nil && value.nil?) || !@validator.valid?(value)
|
77
121
|
error = @validator.error
|
78
122
|
error = ParamError.new(error) unless error.is_a? StandardError
|
@@ -81,6 +125,7 @@ module Apipie
|
|
81
125
|
end
|
82
126
|
|
83
127
|
def process_value(value)
|
128
|
+
value = normalized_value(value)
|
84
129
|
if @validator.respond_to?(:process_value)
|
85
130
|
@validator.process_value(value)
|
86
131
|
else
|
@@ -111,6 +156,7 @@ module Apipie
|
|
111
156
|
:description => preformat_text(Apipie.app.translate(@options[:desc], lang)),
|
112
157
|
:required => required,
|
113
158
|
:allow_nil => allow_nil,
|
159
|
+
:allow_blank => allow_blank,
|
114
160
|
:validator => validator.to_s,
|
115
161
|
:expected_type => validator.expected_type,
|
116
162
|
:metadata => metadata,
|
@@ -131,7 +177,7 @@ module Apipie
|
|
131
177
|
self
|
132
178
|
end
|
133
179
|
|
134
|
-
# merge param
|
180
|
+
# merge param descriptions. Allows defining hash params on more places
|
135
181
|
# (e.g. in param_groups). For example:
|
136
182
|
#
|
137
183
|
# def_param_group :user do
|
@@ -151,6 +197,14 @@ module Apipie
|
|
151
197
|
end.sort_by { |param| ordering.index(param.name) }
|
152
198
|
end
|
153
199
|
|
200
|
+
def self.merge(target_params, source_params)
|
201
|
+
params_to_merge, params_to_add = source_params.partition do |source_param|
|
202
|
+
target_params.any? { |target_param| source_param.name == target_param.name }
|
203
|
+
end
|
204
|
+
unify(target_params + params_to_merge)
|
205
|
+
target_params.concat(params_to_add)
|
206
|
+
end
|
207
|
+
|
154
208
|
# action awareness is being inherited from ancestors (in terms of
|
155
209
|
# nested params)
|
156
210
|
def action_aware?
|
@@ -10,17 +10,19 @@ module Apipie
|
|
10
10
|
# id - resouce name
|
11
11
|
# formats - acceptable request/response format types
|
12
12
|
# headers - array of headers
|
13
|
+
# deprecated - boolean indicating if resource is deprecated
|
13
14
|
class ResourceDescription
|
14
15
|
|
15
16
|
attr_reader :controller, :_short_description, :_full_description, :_methods, :_id,
|
16
|
-
:_path, :_name, :_params_args, :
|
17
|
-
:_headers
|
17
|
+
:_path, :_name, :_params_args, :_returns_args, :_tag_list_arg, :_errors_args,
|
18
|
+
:_formats, :_parent, :_metadata, :_headers, :_deprecated
|
18
19
|
|
19
20
|
def initialize(controller, resource_name, dsl_data = nil, version = nil, &block)
|
20
21
|
|
21
22
|
@_methods = ActiveSupport::OrderedHash.new
|
22
23
|
@_params_args = []
|
23
24
|
@_errors_args = []
|
25
|
+
@_returns_args = []
|
24
26
|
|
25
27
|
@controller = controller
|
26
28
|
@_id = resource_name
|
@@ -39,9 +41,12 @@ module Apipie
|
|
39
41
|
@_formats = dsl_data[:formats]
|
40
42
|
@_errors_args = dsl_data[:errors]
|
41
43
|
@_params_args = dsl_data[:params]
|
44
|
+
@_returns_args = dsl_data[:returns]
|
45
|
+
@_tag_list_arg = dsl_data[:tag_list]
|
42
46
|
@_metadata = dsl_data[:meta]
|
43
47
|
@_api_base_url = dsl_data[:api_base_url]
|
44
48
|
@_headers = dsl_data[:headers]
|
49
|
+
@_deprecated = dsl_data[:deprecated] || false
|
45
50
|
|
46
51
|
if dsl_data[:app_info]
|
47
52
|
Apipie.configuration.app_info[_version] = dsl_data[:app_info]
|
@@ -101,6 +106,7 @@ module Apipie
|
|
101
106
|
|
102
107
|
{
|
103
108
|
:doc_url => doc_url,
|
109
|
+
:id => _id,
|
104
110
|
:api_url => api_url,
|
105
111
|
:name => @_name,
|
106
112
|
:short_description => Apipie.app.translate(@_short_description, lang),
|
@@ -109,7 +115,8 @@ module Apipie
|
|
109
115
|
:formats => @_formats,
|
110
116
|
:metadata => @_metadata,
|
111
117
|
:methods => methods,
|
112
|
-
:headers => _headers
|
118
|
+
:headers => _headers,
|
119
|
+
:deprecated => @_deprecated
|
113
120
|
}
|
114
121
|
end
|
115
122
|
|
@@ -0,0 +1,131 @@
|
|
1
|
+
module Apipie
|
2
|
+
|
3
|
+
class ResponseDescription
|
4
|
+
class ResponseObject
|
5
|
+
include Apipie::DSL::Base
|
6
|
+
include Apipie::DSL::Param
|
7
|
+
|
8
|
+
attr_accessor :additional_properties, :typename
|
9
|
+
|
10
|
+
def initialize(method_description, scope, block, typename)
|
11
|
+
@method_description = method_description
|
12
|
+
@scope = scope
|
13
|
+
@param_group = {scope: scope}
|
14
|
+
@additional_properties = false
|
15
|
+
@typename = typename
|
16
|
+
|
17
|
+
self.instance_exec(&block) if block
|
18
|
+
|
19
|
+
prepare_hash_params
|
20
|
+
end
|
21
|
+
|
22
|
+
# this routine overrides Param#_default_param_group_scope and is called if Param#param_group is
|
23
|
+
# invoked during the instance_exec call in ResponseObject#initialize
|
24
|
+
def _default_param_group_scope
|
25
|
+
@scope
|
26
|
+
end
|
27
|
+
|
28
|
+
def name
|
29
|
+
"response #{@code} for #{@method_description.method}"
|
30
|
+
end
|
31
|
+
|
32
|
+
def params_ordered
|
33
|
+
@params_ordered ||= _apipie_dsl_data[:params].map do |args|
|
34
|
+
options = args.find { |arg| arg.is_a? Hash }
|
35
|
+
options[:param_group] = @param_group
|
36
|
+
Apipie::ParamDescription.from_dsl_data(@method_description, args) unless options[:only_in] == :request
|
37
|
+
end.compact
|
38
|
+
end
|
39
|
+
|
40
|
+
def prepare_hash_params
|
41
|
+
@hash_params = params_ordered.reduce({}) do |h, param|
|
42
|
+
h.update(param.name.to_sym => param)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
class ResponseDescription
|
51
|
+
include Apipie::DSL::Base
|
52
|
+
include Apipie::DSL::Param
|
53
|
+
|
54
|
+
attr_reader :code, :description, :scope, :type_ref, :hash_validator, :is_array_of
|
55
|
+
|
56
|
+
def self.from_dsl_data(method_description, code, args)
|
57
|
+
options, scope, block, adapter = args
|
58
|
+
|
59
|
+
Apipie::ResponseDescription.new(method_description,
|
60
|
+
code,
|
61
|
+
options,
|
62
|
+
scope,
|
63
|
+
block,
|
64
|
+
adapter)
|
65
|
+
end
|
66
|
+
|
67
|
+
def is_array?
|
68
|
+
@is_array_of != false
|
69
|
+
end
|
70
|
+
|
71
|
+
def typename
|
72
|
+
@response_object.typename
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
def initialize(method_description, code, options, scope, block, adapter)
|
77
|
+
|
78
|
+
@type_ref = options[:param_group]
|
79
|
+
@is_array_of = options[:array_of] || false
|
80
|
+
raise ReturnsMultipleDefinitionError, options if @is_array_of && @type_ref
|
81
|
+
|
82
|
+
@type_ref ||= @is_array_of
|
83
|
+
|
84
|
+
@method_description = method_description
|
85
|
+
|
86
|
+
if code.is_a? Symbol
|
87
|
+
@code = Rack::Utils::SYMBOL_TO_STATUS_CODE[code]
|
88
|
+
else
|
89
|
+
@code = code
|
90
|
+
end
|
91
|
+
|
92
|
+
@description = options[:desc]
|
93
|
+
if @description.nil?
|
94
|
+
@description = Rack::Utils::HTTP_STATUS_CODES[@code]
|
95
|
+
raise "Cannot infer description from status code #{@code}" if @description.nil?
|
96
|
+
end
|
97
|
+
@scope = scope
|
98
|
+
|
99
|
+
if adapter
|
100
|
+
@response_object = adapter
|
101
|
+
else
|
102
|
+
@response_object = ResponseObject.new(method_description, scope, block, @type_ref)
|
103
|
+
end
|
104
|
+
|
105
|
+
@response_object.additional_properties ||= options[:additional_properties]
|
106
|
+
end
|
107
|
+
|
108
|
+
def param_description
|
109
|
+
nil
|
110
|
+
end
|
111
|
+
|
112
|
+
def params_ordered
|
113
|
+
@response_object.params_ordered
|
114
|
+
end
|
115
|
+
|
116
|
+
def additional_properties
|
117
|
+
!!@response_object.additional_properties
|
118
|
+
end
|
119
|
+
alias :allow_additional_properties :additional_properties
|
120
|
+
|
121
|
+
def to_json(lang=nil)
|
122
|
+
{
|
123
|
+
:code => code,
|
124
|
+
:description => description,
|
125
|
+
:is_array => is_array?,
|
126
|
+
:returns_object => params_ordered.map{ |param| param.to_json(lang).tap{|h| h.delete(:validations) }}.flatten,
|
127
|
+
:additional_properties => additional_properties,
|
128
|
+
}
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
@@ -0,0 +1,200 @@
|
|
1
|
+
module Apipie
|
2
|
+
|
3
|
+
def self.prop(name, expected_type, options={}, sub_properties=[])
|
4
|
+
Apipie::ResponseDescriptionAdapter::PropDesc.new(name, expected_type, options, sub_properties)
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.additional_properties(yesno)
|
8
|
+
Apipie::ResponseDescriptionAdapter::AdditionalPropertiesModifier.new(yesno)
|
9
|
+
end
|
10
|
+
|
11
|
+
class ResponseDescriptionAdapter
|
12
|
+
class Modifier
|
13
|
+
def apply(adapter)
|
14
|
+
raise "Modifer subclass must implement 'apply' method"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class AdditionalPropertiesModifier < Modifier
|
19
|
+
def initialize(additional_properties_allowed)
|
20
|
+
@additional_properties_allowed = additional_properties_allowed
|
21
|
+
end
|
22
|
+
|
23
|
+
def apply(adapter)
|
24
|
+
adapter.additional_properties = @additional_properties_allowed
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
class ResponseDescriptionAdapter
|
31
|
+
|
32
|
+
#
|
33
|
+
# A ResponseDescriptionAdapter::PropDesc object pretends to be an Apipie::Param in a ResponseDescription
|
34
|
+
#
|
35
|
+
# To successfully masquerade as such, it needs to:
|
36
|
+
# respond_to?('name') and/or ['name'] returning the name of the parameter
|
37
|
+
# respond_to?('required') and/or ['required'] returning boolean
|
38
|
+
# respond_to?('additional_properties') and/or ['additional_properties'] returning boolean
|
39
|
+
# respond_to?('validator') and/or ['validator'] returning 'nil' (so type is 'string'), or an object that:
|
40
|
+
# 1) describes a type. currently type is inferred as follows:
|
41
|
+
# if validator.is_a? Apipie::Validator::EnumValidator --> respond_to? 'values' (returns array). Type is enum or boolean
|
42
|
+
# else: use v.expected_type(). This is expected to be the swagger type, or:
|
43
|
+
# numeric ==> swagger type is 'number'
|
44
|
+
# hash ==> swagger type is 'object' and validator should respond_to? 'params_ordered'
|
45
|
+
# array ==> swagger type is array and validator (FUTURE) should indicate type of element
|
46
|
+
|
47
|
+
class PropDesc
|
48
|
+
|
49
|
+
def to_s
|
50
|
+
"PropDesc -- name: #{@name} type: #{@expected_type} required: #{@required} options: #{@options} subprop count: #{@sub_properties.length} additional properties: #{@additional_properties}"
|
51
|
+
end
|
52
|
+
|
53
|
+
#
|
54
|
+
# a ResponseDescriptionAdapter::PropDesc::Validator pretends to be an Apipie::Validator
|
55
|
+
#
|
56
|
+
class Validator
|
57
|
+
attr_reader :expected_type
|
58
|
+
|
59
|
+
def [](key)
|
60
|
+
return self.send(key) if self.respond_to?(key.to_s)
|
61
|
+
end
|
62
|
+
|
63
|
+
def initialize(expected_type, enum_values=nil, sub_properties=nil)
|
64
|
+
@expected_type = expected_type
|
65
|
+
@enum_values = enum_values
|
66
|
+
@is_enum = !!enum_values
|
67
|
+
@sub_properties = sub_properties
|
68
|
+
end
|
69
|
+
|
70
|
+
def is_enum?
|
71
|
+
!!@is_enum
|
72
|
+
end
|
73
|
+
|
74
|
+
def values
|
75
|
+
@enum_values
|
76
|
+
end
|
77
|
+
|
78
|
+
def params_ordered
|
79
|
+
raise "Only validators with expected_type 'object' can have sub-properties" unless @expected_type == 'object'
|
80
|
+
@sub_properties
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
#======================================================================
|
85
|
+
|
86
|
+
|
87
|
+
def initialize(name, expected_type, options={}, sub_properties=[])
|
88
|
+
@name = name
|
89
|
+
@required = true
|
90
|
+
@required = false if options[:required] == false
|
91
|
+
@expected_type = expected_type
|
92
|
+
@additional_properties = false
|
93
|
+
|
94
|
+
options[:desc] ||= options[:description]
|
95
|
+
@description = options[:desc]
|
96
|
+
@options = options
|
97
|
+
@is_array = options[:is_array] || false
|
98
|
+
@sub_properties = []
|
99
|
+
for prop in sub_properties do
|
100
|
+
add_sub_property(prop)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def [](key)
|
105
|
+
return self.send(key) if self.respond_to?(key.to_s)
|
106
|
+
end
|
107
|
+
|
108
|
+
def add_sub_property(prop_desc)
|
109
|
+
raise "Only properties with expected_type 'object' can have sub-properties" unless @expected_type == 'object'
|
110
|
+
if prop_desc.is_a? PropDesc
|
111
|
+
@sub_properties << prop_desc
|
112
|
+
elsif prop_desc.is_a? Modifier
|
113
|
+
prop_desc.apply(self)
|
114
|
+
else
|
115
|
+
raise "Unrecognized prop_desc type (#{prop_desc.class})"
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def to_json(lang)
|
120
|
+
{
|
121
|
+
name: name,
|
122
|
+
required: required,
|
123
|
+
validator: validator,
|
124
|
+
description: description,
|
125
|
+
additional_properties: additional_properties,
|
126
|
+
is_array: is_array?,
|
127
|
+
options: options
|
128
|
+
}
|
129
|
+
end
|
130
|
+
attr_reader :name, :required, :expected_type, :options, :description
|
131
|
+
attr_accessor :additional_properties
|
132
|
+
|
133
|
+
alias_method :desc, :description
|
134
|
+
|
135
|
+
def is_array?
|
136
|
+
@is_array
|
137
|
+
end
|
138
|
+
|
139
|
+
def validator
|
140
|
+
Validator.new(@expected_type, options[:values], @sub_properties)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
#======================================================================
|
146
|
+
|
147
|
+
class ResponseDescriptionAdapter
|
148
|
+
|
149
|
+
def self.from_self_describing_class(cls)
|
150
|
+
adapter = ResponseDescriptionAdapter.new(cls.to_s)
|
151
|
+
props = cls.describe_own_properties
|
152
|
+
adapter.add_property_descriptions(props)
|
153
|
+
adapter
|
154
|
+
end
|
155
|
+
|
156
|
+
def initialize(typename)
|
157
|
+
@property_descs = []
|
158
|
+
@additional_properties = false
|
159
|
+
@typename = typename
|
160
|
+
end
|
161
|
+
|
162
|
+
attr_accessor :additional_properties, :typename
|
163
|
+
|
164
|
+
def allow_additional_properties
|
165
|
+
additional_properties
|
166
|
+
end
|
167
|
+
|
168
|
+
def to_json
|
169
|
+
params_ordered.to_json
|
170
|
+
end
|
171
|
+
|
172
|
+
def add(prop_desc)
|
173
|
+
if prop_desc.is_a? PropDesc
|
174
|
+
@property_descs << prop_desc
|
175
|
+
elsif prop_desc.is_a? Modifier
|
176
|
+
prop_desc.apply(self)
|
177
|
+
else
|
178
|
+
raise "Unrecognized prop_desc type (#{prop_desc.class})"
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
def add_property_descriptions(prop_descs)
|
183
|
+
for prop_desc in prop_descs
|
184
|
+
add(prop_desc)
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
def property(name, expected_type, options)
|
189
|
+
@property_descs << PropDesc.new(name, expected_type, options)
|
190
|
+
end
|
191
|
+
|
192
|
+
def params_ordered
|
193
|
+
@property_descs
|
194
|
+
end
|
195
|
+
|
196
|
+
def is_array?
|
197
|
+
false
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|