apipie-rails 0.3.6 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/build.yml +67 -0
- data/.github/workflows/rubocop-challenger.yml +28 -0
- data/.gitignore +2 -0
- data/.rubocop.yml +37 -0
- data/.rubocop_todo.yml +1991 -0
- data/CHANGELOG.md +246 -2
- data/PROPOSAL_FOR_RESPONSE_DESCRIPTIONS.md +244 -0
- data/README.rst +646 -25
- data/Rakefile +0 -5
- data/apipie-rails.gemspec +14 -9
- data/app/controllers/apipie/apipies_controller.rb +51 -20
- data/app/public/apipie/javascripts/bundled/bootstrap-collapse.js +70 -41
- data/app/public/apipie/javascripts/bundled/bootstrap.js +1033 -479
- data/app/public/apipie/javascripts/bundled/jquery.js +5 -5
- data/app/public/apipie/stylesheets/bundled/bootstrap-responsive.min.css +9 -12
- data/app/public/apipie/stylesheets/bundled/bootstrap.min.css +9 -689
- 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/config/locales/ko.yml +31 -0
- data/config/locales/pt-BR.yml +1 -1
- data/gemfiles/Gemfile.rails50 +10 -0
- data/gemfiles/Gemfile.rails51 +10 -0
- data/gemfiles/Gemfile.rails52 +10 -0
- data/gemfiles/Gemfile.rails60 +17 -0
- data/gemfiles/Gemfile.rails61 +17 -0
- data/gemfiles/Gemfile.rails70 +17 -0
- data/lib/apipie/apipie_module.rb +22 -4
- data/lib/apipie/application.rb +54 -25
- data/lib/apipie/configuration.rb +26 -4
- data/lib/apipie/core_ext/route.rb +9 -0
- data/lib/apipie/dsl_definition.rb +168 -16
- 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 +14 -12
- data/lib/apipie/extractor/writer.rb +86 -58
- data/lib/apipie/extractor.rb +5 -5
- data/lib/apipie/generator/generator.rb +2 -0
- data/lib/apipie/generator/swagger/swagger.rb +2 -0
- data/lib/apipie/generator/swagger/type.rb +16 -0
- data/lib/apipie/generator/swagger/type_extractor.rb +70 -0
- data/lib/apipie/generator/swagger/warning.rb +77 -0
- data/lib/apipie/generator/swagger/warning_writer.rb +48 -0
- data/lib/apipie/markup.rb +14 -11
- data/lib/apipie/method_description/api.rb +12 -0
- data/lib/apipie/method_description/apis_service.rb +82 -0
- data/lib/apipie/method_description.rb +51 -49
- data/lib/apipie/param_description.rb +63 -5
- data/lib/apipie/resource_description.rb +11 -4
- 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 +5 -2
- data/lib/apipie/swagger_generator.rb +717 -0
- data/lib/apipie/tag_list_description.rb +11 -0
- data/lib/apipie/validator.rb +83 -9
- data/lib/apipie/version.rb +1 -1
- data/lib/apipie-rails.rb +15 -4
- data/lib/generators/apipie/install/install_generator.rb +1 -1
- data/lib/generators/apipie/views_generator.rb +1 -1
- data/lib/tasks/apipie.rake +115 -15
- data/rel-eng/gem_release.ipynb +398 -0
- data/spec/controllers/apipies_controller_spec.rb +79 -14
- data/spec/controllers/concerns_controller_spec.rb +2 -2
- data/spec/controllers/extended_controller_spec.rb +14 -0
- data/spec/controllers/included_param_group_controller_spec.rb +13 -0
- data/spec/controllers/memes_controller_spec.rb +10 -0
- data/spec/controllers/users_controller_spec.rb +139 -76
- data/spec/dummy/Rakefile +1 -1
- data/spec/dummy/app/controllers/application_controller.rb +5 -1
- data/spec/dummy/app/controllers/concerns_controller.rb +1 -1
- data/spec/dummy/app/controllers/extended_controller.rb +14 -0
- data/spec/dummy/app/controllers/extending_concern.rb +10 -0
- data/spec/dummy/app/controllers/included_param_group_controller.rb +19 -0
- data/spec/dummy/app/controllers/overridden_concerns_controller.rb +2 -2
- 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/{concerns/sample_controller.rb → sample_controller.rb} +5 -7
- 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 +26 -12
- data/spec/dummy/app/helpers/random_param_group.rb +8 -0
- 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 +6 -4
- data/spec/dummy/config/boot.rb +2 -2
- data/spec/dummy/config/environment.rb +1 -1
- data/spec/dummy/config/environments/development.rb +3 -3
- data/spec/dummy/config/environments/production.rb +3 -3
- data/spec/dummy/config/environments/test.rb +3 -5
- data/spec/dummy/config/initializers/apipie.rb +5 -3
- data/spec/dummy/config/routes.rb +25 -1
- data/spec/dummy/config.ru +1 -1
- data/spec/dummy/script/rails +2 -2
- data/spec/lib/application_spec.rb +1 -1
- data/spec/lib/extractor/writer_spec.rb +37 -7
- data/spec/lib/file_handler_spec.rb +25 -0
- data/spec/lib/generator/swagger/type_extractor_spec.rb +61 -0
- data/spec/lib/generator/swagger/warning_spec.rb +51 -0
- data/spec/lib/generator/swagger/warning_writer_spec.rb +59 -0
- data/spec/lib/method_description/apis_service_spec.rb +60 -0
- data/spec/lib/method_description_spec.rb +34 -0
- data/spec/lib/param_description_spec.rb +90 -4
- data/spec/lib/rake_spec.rb +2 -4
- data/spec/lib/swagger/openapi_2_0_schema.json +1607 -0
- data/spec/lib/swagger/rake_swagger_spec.rb +154 -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 +59 -1
- data/spec/lib/validators/array_validator_spec.rb +28 -8
- data/spec/spec_helper.rb +49 -3
- data/spec/support/custom_bool_validator.rb +17 -0
- metadata +104 -99
- data/.travis.yml +0 -12
- data/Gemfile +0 -7
- data/Gemfile.rails32 +0 -6
- data/Gemfile.rails40 +0 -5
- data/Gemfile.rails41 +0 -5
- data/Gemfile.rails42 +0 -5
- data/lib/apipie/client/generator.rb +0 -135
data/lib/apipie/extractor.rb
CHANGED
@@ -9,16 +9,16 @@ require 'apipie/extractor/collector'
|
|
9
9
|
class Apipie::Railtie
|
10
10
|
initializer 'apipie.extractor' do |app|
|
11
11
|
ActiveSupport.on_load :action_controller do
|
12
|
-
|
12
|
+
before_action do |controller|
|
13
13
|
if Apipie.configuration.record
|
14
14
|
Apipie::Extractor.call_recorder.analyse_controller(controller)
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
18
18
|
app.middleware.use ::Apipie::Extractor::Recorder::Middleware
|
19
|
-
|
20
|
-
|
21
|
-
|
19
|
+
|
20
|
+
ActionController::TestCase.send(:prepend, Apipie::Extractor::Recorder::FunctionalTestRecording)
|
21
|
+
ActionController::TestCase::Behavior.send(:prepend, Apipie::Extractor::Recorder::FunctionalTestRecording)
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
@@ -154,7 +154,7 @@ module Apipie
|
|
154
154
|
def update_api_descriptions
|
155
155
|
apis_from_docs = all_apis_from_docs
|
156
156
|
@apis_from_routes.each do |(controller, action), new_apis|
|
157
|
-
method_key = "#{Apipie.get_resource_name(controller.
|
157
|
+
method_key = "#{Apipie.get_resource_name(controller.safe_constantize || next)}##{action}"
|
158
158
|
old_apis = apis_from_docs[method_key] || []
|
159
159
|
new_apis.each do |new_api|
|
160
160
|
new_api[:path].sub!(/\(\.:format\)$/,"") if new_api[:path]
|
@@ -0,0 +1,70 @@
|
|
1
|
+
class Apipie::Generator::Swagger::TypeExtractor
|
2
|
+
TYPES = {
|
3
|
+
numeric: 'number',
|
4
|
+
hash: 'object',
|
5
|
+
array: 'array',
|
6
|
+
enum: 'enum',
|
7
|
+
|
8
|
+
# see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#data-types
|
9
|
+
integer: Apipie::Generator::Swagger::Type.new('integer', 'int32'),
|
10
|
+
long: Apipie::Generator::Swagger::Type.new('integer', 'int64'),
|
11
|
+
number: Apipie::Generator::Swagger::Type.new('number'),
|
12
|
+
float: Apipie::Generator::Swagger::Type.new('number', 'float'),
|
13
|
+
double: Apipie::Generator::Swagger::Type.new('number', 'double'),
|
14
|
+
string: Apipie::Generator::Swagger::Type.new('string'),
|
15
|
+
byte: Apipie::Generator::Swagger::Type.new('string', 'byte'),
|
16
|
+
binary: Apipie::Generator::Swagger::Type.new('string', 'binary'),
|
17
|
+
boolean: Apipie::Generator::Swagger::Type.new('boolean'),
|
18
|
+
date: Apipie::Generator::Swagger::Type.new('string', 'date'),
|
19
|
+
dateTime: Apipie::Generator::Swagger::Type.new('string', 'date-time'),
|
20
|
+
password: Apipie::Generator::Swagger::Type.new('string', 'password')
|
21
|
+
}
|
22
|
+
|
23
|
+
# @param [Apipie::Validator::BaseValidator, ResponseDescriptionAdapter::PropDesc::Validator, nil] validator
|
24
|
+
def initialize(validator)
|
25
|
+
@validator = validator
|
26
|
+
end
|
27
|
+
|
28
|
+
# @param [Hash<Symbol, Apipie::Generator::Swagger::Warning>] warnings
|
29
|
+
def extract_with_warnings(warnings = {})
|
30
|
+
if boolean? && warnings[:boolean].present?
|
31
|
+
Apipie::Generator::Swagger::WarningWriter.instance.warn(warnings[:boolean])
|
32
|
+
end
|
33
|
+
|
34
|
+
extract
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def extract
|
40
|
+
expected_type =
|
41
|
+
if string?
|
42
|
+
:string
|
43
|
+
elsif boolean?
|
44
|
+
:boolean
|
45
|
+
elsif enum?
|
46
|
+
:enum
|
47
|
+
else
|
48
|
+
@validator.expected_type.to_sym
|
49
|
+
end
|
50
|
+
|
51
|
+
TYPES[expected_type] || @validator.expected_type
|
52
|
+
end
|
53
|
+
|
54
|
+
def string?
|
55
|
+
@validator.blank?
|
56
|
+
end
|
57
|
+
|
58
|
+
def enum?
|
59
|
+
@validator.is_a?(Apipie::Validator::EnumValidator) ||
|
60
|
+
(@validator.respond_to?(:is_enum?) && @validator.is_enum?)
|
61
|
+
end
|
62
|
+
|
63
|
+
def boolean?
|
64
|
+
@_boolean ||= enum? && boolean_values?
|
65
|
+
end
|
66
|
+
|
67
|
+
def boolean_values?
|
68
|
+
@validator.values.to_set == Set.new([true, false])
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
class Apipie::Generator::Swagger::Warning
|
2
|
+
MISSING_METHOD_SUMMARY_CODE = 100
|
3
|
+
ADDED_MISSING_SLASH_CODE = 101
|
4
|
+
NO_RETURN_CODES_SPECIFIED_CODE = 102
|
5
|
+
HASH_WITHOUT_INTERNAL_TYPESPEC_CODE = 103
|
6
|
+
OPTIONAL_PARAM_IN_PATH_CODE = 104
|
7
|
+
OPTIONAL_WITHOUT_DEFAULT_VALUE_CODE = 105
|
8
|
+
PARAM_IGNORED_IN_FORM_DATA_CODE = 106
|
9
|
+
PATH_PARAM_NOT_DESCRIBED_CODE = 107
|
10
|
+
INFERRING_BOOLEAN_CODE = 108
|
11
|
+
|
12
|
+
CODES = {
|
13
|
+
missing_method_summary: MISSING_METHOD_SUMMARY_CODE,
|
14
|
+
added_missing_slash: ADDED_MISSING_SLASH_CODE,
|
15
|
+
no_return_codes_specified: NO_RETURN_CODES_SPECIFIED_CODE,
|
16
|
+
hash_without_internal_typespec: HASH_WITHOUT_INTERNAL_TYPESPEC_CODE,
|
17
|
+
optional_param_in_path: OPTIONAL_PARAM_IN_PATH_CODE,
|
18
|
+
optional_without_default_value: OPTIONAL_WITHOUT_DEFAULT_VALUE_CODE,
|
19
|
+
param_ignored_in_form_data: PARAM_IGNORED_IN_FORM_DATA_CODE,
|
20
|
+
path_param_not_described_code: PATH_PARAM_NOT_DESCRIBED_CODE,
|
21
|
+
inferring_boolean: INFERRING_BOOLEAN_CODE
|
22
|
+
}
|
23
|
+
|
24
|
+
MESSAGES = {
|
25
|
+
MISSING_METHOD_SUMMARY_CODE => "Missing short description for method",
|
26
|
+
ADDED_MISSING_SLASH_CODE => "Added missing / at beginning of path: %{path}",
|
27
|
+
HASH_WITHOUT_INTERNAL_TYPESPEC_CODE => "The parameter :%{parameter} is a generic Hash without an internal type specification",
|
28
|
+
NO_RETURN_CODES_SPECIFIED_CODE => "No return codes ('errors') specified",
|
29
|
+
OPTIONAL_PARAM_IN_PATH_CODE => "The parameter :%{parameter} is 'in-path'. Ignoring 'not required' in DSL",
|
30
|
+
OPTIONAL_WITHOUT_DEFAULT_VALUE_CODE => "The parameter :%{parameter} is optional but default value is not specified (use :default_value => ...)",
|
31
|
+
PARAM_IGNORED_IN_FORM_DATA_CODE => "Ignoring param :%{parameter} -- cannot include Hash without fields in a formData specification",
|
32
|
+
PATH_PARAM_NOT_DESCRIBED_CODE => "The parameter :%{name} appears in the path %{path} but is not described",
|
33
|
+
INFERRING_BOOLEAN_CODE => "The parameter [%{parameter}] is Enum with [true, false] values. Inferring 'boolean'"
|
34
|
+
}
|
35
|
+
|
36
|
+
attr_reader :code
|
37
|
+
|
38
|
+
def initialize(code, info_message, method_id)
|
39
|
+
@code = code
|
40
|
+
@info_message = info_message
|
41
|
+
@method_id = method_id
|
42
|
+
end
|
43
|
+
|
44
|
+
def id
|
45
|
+
"#{@method_id}#{@code}#{@info_message}"
|
46
|
+
end
|
47
|
+
|
48
|
+
def warning_message
|
49
|
+
"WARNING (#{@code}): [#{@method_id}] -- #{@info_message}"
|
50
|
+
end
|
51
|
+
|
52
|
+
def warn
|
53
|
+
Warning.warn(warning_message)
|
54
|
+
end
|
55
|
+
|
56
|
+
def warn_through_writer
|
57
|
+
Apipie::Generator::Swagger::WarningWriter.instance.warn(self)
|
58
|
+
end
|
59
|
+
|
60
|
+
# @param [Integer] code
|
61
|
+
# @param [Hash] message_attributes
|
62
|
+
#
|
63
|
+
# @return [Apipie::Generator::Swagger::Warning]
|
64
|
+
def self.for_code(code, method_id, message_attributes = {})
|
65
|
+
if !CODES.values.include?(code)
|
66
|
+
raise ArgumentError, 'Unknown warning code'
|
67
|
+
end
|
68
|
+
|
69
|
+
info_message = if message_attributes.present?
|
70
|
+
self::MESSAGES[code] % message_attributes
|
71
|
+
else
|
72
|
+
self::MESSAGES[code]
|
73
|
+
end
|
74
|
+
|
75
|
+
Apipie::Generator::Swagger::Warning.new(code, info_message, method_id)
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
class Apipie::Generator::Swagger::WarningWriter
|
2
|
+
include Singleton
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
@issued_warnings = []
|
6
|
+
end
|
7
|
+
|
8
|
+
# @param [Apipie::Generator::Swagger::Warning] warning
|
9
|
+
def warn(warning)
|
10
|
+
return if muted_warning?(warning)
|
11
|
+
|
12
|
+
warning.warn
|
13
|
+
|
14
|
+
@issued_warnings << warning.id
|
15
|
+
end
|
16
|
+
|
17
|
+
def issued_warnings?
|
18
|
+
@issued_warnings.count > 0
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
# @param [Apipie::Generator::Swagger::Warning] warning
|
24
|
+
#
|
25
|
+
# @return [TrueClass, FalseClass]
|
26
|
+
def muted_warning?(warning)
|
27
|
+
@issued_warnings.include?(warning.id) ||
|
28
|
+
suppressed_warning?(warning.code) ||
|
29
|
+
suppress_warnings?
|
30
|
+
end
|
31
|
+
|
32
|
+
# @param [Integer] warning_number
|
33
|
+
#
|
34
|
+
# @return [TrueClass, FalseClass]
|
35
|
+
def suppressed_warning?(warning_number)
|
36
|
+
suppress_warnings_config.is_a?(Array) && suppress_warnings_config.include?(warning_number)
|
37
|
+
end
|
38
|
+
|
39
|
+
# @return [TrueClass, FalseClass]
|
40
|
+
def suppress_warnings?
|
41
|
+
suppress_warnings_config == true
|
42
|
+
end
|
43
|
+
|
44
|
+
# @return [FalseClass, TrueClass, Array]
|
45
|
+
def suppress_warnings_config
|
46
|
+
Apipie.configuration.swagger_suppress_warnings
|
47
|
+
end
|
48
|
+
end
|
data/lib/apipie/markup.rb
CHANGED
@@ -4,20 +4,23 @@ module Apipie
|
|
4
4
|
|
5
5
|
class RDoc
|
6
6
|
|
7
|
-
def initialize
|
8
|
-
require 'rdoc'
|
9
|
-
require 'rdoc/markup/to_html'
|
10
|
-
if Gem::Version.new(::RDoc::VERSION) < Gem::Version.new('4.0.0')
|
11
|
-
@rdoc ||= ::RDoc::Markup::ToHtml.new()
|
12
|
-
else
|
13
|
-
@rdoc ||= ::RDoc::Markup::ToHtml.new(::RDoc::Options.new)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
7
|
def to_html(text)
|
18
|
-
|
8
|
+
rdoc.convert(text)
|
19
9
|
end
|
20
10
|
|
11
|
+
private
|
12
|
+
|
13
|
+
def rdoc
|
14
|
+
@rdoc ||= begin
|
15
|
+
require 'rdoc'
|
16
|
+
require 'rdoc/markup/to_html'
|
17
|
+
if Gem::Version.new(::RDoc::VERSION) < Gem::Version.new('4.0.0')
|
18
|
+
::RDoc::Markup::ToHtml.new()
|
19
|
+
else
|
20
|
+
::RDoc::Markup::ToHtml.new(::RDoc::Options.new)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
21
24
|
end
|
22
25
|
|
23
26
|
class Markdown
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class Apipie::MethodDescription::Api
|
2
|
+
attr_accessor :short_description, :path, :http_method, :from_routes,
|
3
|
+
:options, :returns
|
4
|
+
|
5
|
+
def initialize(method, path, desc, options)
|
6
|
+
@http_method = method.to_s
|
7
|
+
@path = path
|
8
|
+
@short_description = desc
|
9
|
+
@from_routes = options[:from_routes]
|
10
|
+
@options = options
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# Service that builds the appropriate Apipie::MethodDescription::Api
|
2
|
+
# required by Apipie::MethodDescription
|
3
|
+
#
|
4
|
+
class Apipie::MethodDescription::ApisService
|
5
|
+
# @param [Apipie::ResourceDescription] resource
|
6
|
+
# @param [Symbol] controller_action
|
7
|
+
# @param [Hash] dsl
|
8
|
+
def initialize(resource, controller_action, dsl)
|
9
|
+
@resource = resource
|
10
|
+
@controller_action = controller_action
|
11
|
+
@dsl = dsl
|
12
|
+
end
|
13
|
+
|
14
|
+
# @return [Array<Apipie::MethodDescription::Api>]
|
15
|
+
def call
|
16
|
+
api_args.map do |http_method, path, desc, opts|
|
17
|
+
Apipie::MethodDescription::Api.new(
|
18
|
+
http_method,
|
19
|
+
concern_subst(path),
|
20
|
+
concern_subst(desc),
|
21
|
+
opts
|
22
|
+
)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def concern_subst(path)
|
29
|
+
return if path.blank?
|
30
|
+
|
31
|
+
if from_concern?
|
32
|
+
@resource.controller._apipie_perform_concern_subst(path)
|
33
|
+
else
|
34
|
+
path
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# @return [Array<Array>]
|
39
|
+
def api_args
|
40
|
+
return @dsl[:api_args] if !api_from_routes?
|
41
|
+
|
42
|
+
api_args = @dsl[:api_args].dup
|
43
|
+
|
44
|
+
api_args_from_routes = routes.map do |route_info|
|
45
|
+
[
|
46
|
+
route_info[:verb],
|
47
|
+
route_info[:path],
|
48
|
+
route_info[:desc],
|
49
|
+
(route_info[:options] || {}).merge(from_routes: true)
|
50
|
+
]
|
51
|
+
end
|
52
|
+
|
53
|
+
api_args.concat(api_args_from_routes)
|
54
|
+
end
|
55
|
+
|
56
|
+
def api_from_routes?
|
57
|
+
@dsl[:api_from_routes].present?
|
58
|
+
end
|
59
|
+
|
60
|
+
def from_concern?
|
61
|
+
@dsl[:from_concern] == true
|
62
|
+
end
|
63
|
+
|
64
|
+
def description
|
65
|
+
@dsl[:api_from_routes][:desc]
|
66
|
+
end
|
67
|
+
|
68
|
+
def options
|
69
|
+
@dsl[:api_from_routes][:options]
|
70
|
+
end
|
71
|
+
|
72
|
+
# @return [Array<Hash>]
|
73
|
+
def routes
|
74
|
+
Apipie.routes_for_action(
|
75
|
+
@resource.controller,
|
76
|
+
@controller_action,
|
77
|
+
{ desc: description, options: options }
|
78
|
+
)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
|
@@ -1,31 +1,14 @@
|
|
1
|
-
require 'set'
|
2
1
|
module Apipie
|
3
2
|
|
4
3
|
class MethodDescription
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
attr_accessor :short_description, :path, :http_method, :from_routes, :options
|
9
|
-
|
10
|
-
def initialize(method, path, desc, options)
|
11
|
-
@http_method = method.to_s
|
12
|
-
@path = path
|
13
|
-
@short_description = desc
|
14
|
-
@from_routes = options[:from_routes]
|
15
|
-
@options = options
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
19
|
-
|
20
|
-
attr_reader :full_description, :method, :resource, :apis, :examples, :see, :formats, :metadata, :headers, :show
|
4
|
+
attr_reader :full_description, :method, :resource, :apis, :examples, :see, :formats, :headers, :show
|
5
|
+
attr_accessor :metadata
|
21
6
|
|
22
7
|
def initialize(method, resource, dsl_data)
|
23
8
|
@method = method.to_s
|
24
9
|
@resource = resource
|
25
10
|
@from_concern = dsl_data[:from_concern]
|
26
|
-
@apis =
|
27
|
-
MethodDescription::Api.new(mthd, concern_subst(path), concern_subst(desc), opts)
|
28
|
-
end
|
11
|
+
@apis = ApisService.new(resource, method, dsl_data).call
|
29
12
|
|
30
13
|
desc = dsl_data[:description] || ''
|
31
14
|
@full_description = Apipie.markup_to_html(desc)
|
@@ -34,6 +17,12 @@ module Apipie
|
|
34
17
|
Apipie::ErrorDescription.from_dsl_data(args)
|
35
18
|
end
|
36
19
|
|
20
|
+
@tag_list = dsl_data[:tag_list]
|
21
|
+
|
22
|
+
@returns = dsl_data[:returns].map do |code,args|
|
23
|
+
Apipie::ResponseDescription.from_dsl_data(self, code, args)
|
24
|
+
end
|
25
|
+
|
37
26
|
@see = dsl_data[:see].map do |args|
|
38
27
|
Apipie::SeeDescription.new(args)
|
39
28
|
end
|
@@ -46,7 +35,8 @@ module Apipie
|
|
46
35
|
|
47
36
|
@params_ordered = dsl_data[:params].map do |args|
|
48
37
|
Apipie::ParamDescription.from_dsl_data(self, args)
|
49
|
-
end
|
38
|
+
end.reject(&:response_only?)
|
39
|
+
|
50
40
|
@params_ordered = ParamDescription.unify(@params_ordered)
|
51
41
|
@headers = dsl_data[:headers]
|
52
42
|
|
@@ -65,6 +55,10 @@ module Apipie
|
|
65
55
|
params_ordered.reduce(ActiveSupport::OrderedHash.new) { |h,p| h[p.name] = p; h }
|
66
56
|
end
|
67
57
|
|
58
|
+
def params_ordered_self
|
59
|
+
@params_ordered
|
60
|
+
end
|
61
|
+
|
68
62
|
def params_ordered
|
69
63
|
all_params = []
|
70
64
|
parent = Apipie.get_resource_description(@resource.controller.superclass)
|
@@ -81,6 +75,34 @@ module Apipie
|
|
81
75
|
all_params.find_all(&:validator)
|
82
76
|
end
|
83
77
|
|
78
|
+
def returns_self
|
79
|
+
@returns
|
80
|
+
end
|
81
|
+
|
82
|
+
def tag_list
|
83
|
+
all_tag_list = []
|
84
|
+
parent = Apipie.get_resource_description(@resource.controller.superclass)
|
85
|
+
|
86
|
+
# get tags from parent resource description
|
87
|
+
parent_tags = [parent, @resource].compact.flat_map(&:_tag_list_arg)
|
88
|
+
Apipie::TagListDescription.new((parent_tags + @tag_list).uniq.compact)
|
89
|
+
end
|
90
|
+
|
91
|
+
def returns
|
92
|
+
all_returns = []
|
93
|
+
parent = Apipie.get_resource_description(@resource.controller.superclass)
|
94
|
+
|
95
|
+
# get response descriptions from parent resource description
|
96
|
+
[parent, @resource].compact.each do |resource|
|
97
|
+
resource_returns = resource._returns_args.map do |code, args|
|
98
|
+
Apipie::ResponseDescription.from_dsl_data(self, code, args)
|
99
|
+
end
|
100
|
+
merge_returns(all_returns, resource_returns)
|
101
|
+
end
|
102
|
+
|
103
|
+
merge_returns(all_returns, @returns)
|
104
|
+
end
|
105
|
+
|
84
106
|
def errors
|
85
107
|
return @merged_errors if @merged_errors
|
86
108
|
@merged_errors = []
|
@@ -125,7 +147,7 @@ module Apipie
|
|
125
147
|
:api_url => create_api_url(api),
|
126
148
|
:http_method => api.http_method.to_s,
|
127
149
|
:short_description => Apipie.app.translate(api.short_description, lang),
|
128
|
-
:deprecated => api.options[:deprecated]
|
150
|
+
:deprecated => resource._deprecated || api.options[:deprecated]
|
129
151
|
}
|
130
152
|
end
|
131
153
|
end
|
@@ -147,6 +169,7 @@ module Apipie
|
|
147
169
|
:full_description => Apipie.app.translate(@full_description, lang),
|
148
170
|
:errors => errors.map(&:to_json),
|
149
171
|
:params => params_ordered.map{ |param| param.to_json(lang) }.flatten,
|
172
|
+
:returns => @returns.map{ |return_item| return_item.to_json(lang) }.flatten,
|
150
173
|
:examples => @examples,
|
151
174
|
:metadata => @metadata,
|
152
175
|
:see => see.map(&:to_json),
|
@@ -162,29 +185,18 @@ module Apipie
|
|
162
185
|
|
163
186
|
private
|
164
187
|
|
165
|
-
def api_data(dsl_data)
|
166
|
-
ret = dsl_data[:api_args].dup
|
167
|
-
if dsl_data[:api_from_routes]
|
168
|
-
desc = dsl_data[:api_from_routes][:desc]
|
169
|
-
options = dsl_data[:api_from_routes][:options]
|
170
|
-
|
171
|
-
api_from_routes = Apipie.routes_for_action(resource.controller, method, {:desc => desc, :options => options}).map do |route_info|
|
172
|
-
[route_info[:verb],
|
173
|
-
route_info[:path],
|
174
|
-
route_info[:desc],
|
175
|
-
(route_info[:options] || {}).merge(:from_routes => true)]
|
176
|
-
end
|
177
|
-
ret.concat(api_from_routes)
|
178
|
-
end
|
179
|
-
ret
|
180
|
-
end
|
181
|
-
|
182
188
|
def merge_params(params, new_params)
|
183
189
|
new_param_names = Set.new(new_params.map(&:name))
|
184
190
|
params.delete_if { |p| new_param_names.include?(p.name) }
|
185
191
|
params.concat(new_params)
|
186
192
|
end
|
187
193
|
|
194
|
+
def merge_returns(returns, new_returns)
|
195
|
+
new_return_codes = Set.new(new_returns.map(&:code))
|
196
|
+
returns.delete_if { |p| new_return_codes.include?(p.code) }
|
197
|
+
returns.concat(new_returns)
|
198
|
+
end
|
199
|
+
|
188
200
|
def load_recorded_examples
|
189
201
|
(Apipie.recorded_examples[id] || []).
|
190
202
|
find_all { |ex| ex["show_in_doc"].to_i > 0 }.
|
@@ -212,16 +224,6 @@ module Apipie
|
|
212
224
|
example << "\n" << format_example_data(ex[:response_data]).to_s if ex[:response_data]
|
213
225
|
example
|
214
226
|
end
|
215
|
-
|
216
|
-
def concern_subst(string)
|
217
|
-
return if string.nil?
|
218
|
-
if from_concern?
|
219
|
-
resource.controller._apipie_perform_concern_subst(string)
|
220
|
-
else
|
221
|
-
string
|
222
|
-
end
|
223
|
-
end
|
224
|
-
|
225
227
|
end
|
226
228
|
|
227
229
|
end
|
@@ -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,31 +78,58 @@ 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
|
-
return true if
|
76
|
-
if
|
77
|
-
|
117
|
+
return true if allow_nil && value.nil?
|
118
|
+
return true if allow_blank && value.blank?
|
119
|
+
value = normalized_value(value)
|
120
|
+
if (!allow_nil && value.nil?) || (blank_forbidden? && value.blank?) || !validator.valid?(value)
|
121
|
+
error = validator.error
|
78
122
|
error = ParamError.new(error) unless error.is_a? StandardError
|
79
123
|
raise error
|
80
124
|
end
|
81
125
|
end
|
82
126
|
|
127
|
+
def blank_forbidden?
|
128
|
+
!Apipie.configuration.ignore_allow_blank_false && !allow_blank && !validator.ignore_allow_blank?
|
129
|
+
end
|
130
|
+
|
83
131
|
def process_value(value)
|
132
|
+
value = normalized_value(value)
|
84
133
|
if @validator.respond_to?(:process_value)
|
85
134
|
@validator.process_value(value)
|
86
135
|
else
|
@@ -111,6 +160,7 @@ module Apipie
|
|
111
160
|
:description => preformat_text(Apipie.app.translate(@options[:desc], lang)),
|
112
161
|
:required => required,
|
113
162
|
:allow_nil => allow_nil,
|
163
|
+
:allow_blank => allow_blank,
|
114
164
|
:validator => validator.to_s,
|
115
165
|
:expected_type => validator.expected_type,
|
116
166
|
:metadata => metadata,
|
@@ -131,7 +181,7 @@ module Apipie
|
|
131
181
|
self
|
132
182
|
end
|
133
183
|
|
134
|
-
# merge param
|
184
|
+
# merge param descriptions. Allows defining hash params on more places
|
135
185
|
# (e.g. in param_groups). For example:
|
136
186
|
#
|
137
187
|
# def_param_group :user do
|
@@ -151,6 +201,14 @@ module Apipie
|
|
151
201
|
end.sort_by { |param| ordering.index(param.name) }
|
152
202
|
end
|
153
203
|
|
204
|
+
def self.merge(target_params, source_params)
|
205
|
+
params_to_merge, params_to_add = source_params.partition do |source_param|
|
206
|
+
target_params.any? { |target_param| source_param.name == target_param.name }
|
207
|
+
end
|
208
|
+
unify(target_params + params_to_merge)
|
209
|
+
target_params.concat(params_to_add)
|
210
|
+
end
|
211
|
+
|
154
212
|
# action awareness is being inherited from ancestors (in terms of
|
155
213
|
# nested params)
|
156
214
|
def action_aware?
|