apipie-rails-jq 1.4.3.pre.beta.pre.jq.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.github/workflows/build.yml +32 -0
- data/.github/workflows/rubocop-challenger.yml +26 -0
- data/.github/workflows/rubocop.yml +18 -0
- data/.gitignore +16 -0
- data/.rspec +2 -0
- data/.rubocop.yml +132 -0
- data/.rubocop_todo.yml +1967 -0
- data/.vscode/settings.json +3 -0
- data/APACHE-LICENSE-2.0 +202 -0
- data/CHANGELOG.md +693 -0
- data/Gemfile +19 -0
- data/MIT-LICENSE +20 -0
- data/NOTICE +4 -0
- data/PROPOSAL_FOR_RESPONSE_DESCRIPTIONS.md +244 -0
- data/README.md +2088 -0
- data/Rakefile +8 -0
- data/apipie-rails.gemspec +44 -0
- data/app/controllers/apipie/apipies_controller.rb +184 -0
- data/app/helpers/apipie_helper.rb +10 -0
- data/app/public/apipie/javascripts/apipie.js +6 -0
- data/app/public/apipie/javascripts/bundled/bootstrap-collapse.js +167 -0
- data/app/public/apipie/javascripts/bundled/bootstrap.js +2280 -0
- data/app/public/apipie/javascripts/bundled/jquery.js +2 -0
- data/app/public/apipie/javascripts/bundled/prettify.js +28 -0
- data/app/public/apipie/stylesheets/application.css +7 -0
- data/app/public/apipie/stylesheets/bundled/bootstrap-responsive.min.css +9 -0
- data/app/public/apipie/stylesheets/bundled/bootstrap.min.css +9 -0
- data/app/public/apipie/stylesheets/bundled/prettify.css +30 -0
- data/app/views/apipie/apipies/_deprecation.html.erb +16 -0
- data/app/views/apipie/apipies/_disqus.html.erb +13 -0
- data/app/views/apipie/apipies/_errors.html.erb +23 -0
- data/app/views/apipie/apipies/_headers.html.erb +26 -0
- data/app/views/apipie/apipies/_languages.erb +6 -0
- data/app/views/apipie/apipies/_metadata.erb +1 -0
- data/app/views/apipie/apipies/_method_detail.erb +63 -0
- data/app/views/apipie/apipies/_params.html.erb +49 -0
- data/app/views/apipie/apipies/_params_plain.html.erb +21 -0
- data/app/views/apipie/apipies/apipie_404.html.erb +17 -0
- data/app/views/apipie/apipies/apipie_checksum.json.erb +1 -0
- data/app/views/apipie/apipies/getting_started.html.erb +6 -0
- data/app/views/apipie/apipies/index.html.erb +56 -0
- data/app/views/apipie/apipies/method.html.erb +41 -0
- data/app/views/apipie/apipies/plain.html.erb +77 -0
- data/app/views/apipie/apipies/resource.html.erb +80 -0
- data/app/views/apipie/apipies/static.html.erb +103 -0
- data/app/views/layouts/apipie/apipie.html.erb +27 -0
- data/config/locales/de.yml +28 -0
- data/config/locales/en.yml +41 -0
- data/config/locales/es.yml +28 -0
- data/config/locales/fr.yml +31 -0
- data/config/locales/it.yml +41 -0
- data/config/locales/ja.yml +31 -0
- data/config/locales/ko.yml +32 -0
- data/config/locales/pl.yml +28 -0
- data/config/locales/pt-BR.yml +28 -0
- data/config/locales/ru.yml +28 -0
- data/config/locales/tr.yml +28 -0
- data/config/locales/zh-CN.yml +28 -0
- data/config/locales/zh-TW.yml +28 -0
- data/gemfiles/Gemfile.tools +9 -0
- data/images/screenshot-1.png +0 -0
- data/images/screenshot-2.png +0 -0
- data/lib/apipie/apipie_module.rb +83 -0
- data/lib/apipie/application.rb +499 -0
- data/lib/apipie/configuration.rb +196 -0
- data/lib/apipie/core_ext/route.rb +9 -0
- data/lib/apipie/dsl_definition.rb +630 -0
- data/lib/apipie/error_description.rb +46 -0
- data/lib/apipie/errors.rb +86 -0
- data/lib/apipie/extractor/collector.rb +116 -0
- data/lib/apipie/extractor/recorder.rb +193 -0
- data/lib/apipie/extractor/writer.rb +454 -0
- data/lib/apipie/extractor.rb +181 -0
- data/lib/apipie/generator/config.rb +12 -0
- data/lib/apipie/generator/generator.rb +2 -0
- data/lib/apipie/generator/swagger/computed_interface_id.rb +23 -0
- data/lib/apipie/generator/swagger/config.rb +80 -0
- data/lib/apipie/generator/swagger/context.rb +38 -0
- data/lib/apipie/generator/swagger/method_description/api_decorator.rb +20 -0
- data/lib/apipie/generator/swagger/method_description/api_schema_service.rb +89 -0
- data/lib/apipie/generator/swagger/method_description/decorator.rb +22 -0
- data/lib/apipie/generator/swagger/method_description/parameters_service.rb +139 -0
- data/lib/apipie/generator/swagger/method_description/response_schema_service.rb +46 -0
- data/lib/apipie/generator/swagger/method_description/response_service.rb +71 -0
- data/lib/apipie/generator/swagger/method_description.rb +2 -0
- data/lib/apipie/generator/swagger/operation_id.rb +51 -0
- data/lib/apipie/generator/swagger/param_description/builder.rb +114 -0
- data/lib/apipie/generator/swagger/param_description/composite.rb +119 -0
- data/lib/apipie/generator/swagger/param_description/description.rb +15 -0
- data/lib/apipie/generator/swagger/param_description/in.rb +37 -0
- data/lib/apipie/generator/swagger/param_description/name.rb +18 -0
- data/lib/apipie/generator/swagger/param_description/path_params_composite.rb +61 -0
- data/lib/apipie/generator/swagger/param_description/referenced_composite.rb +36 -0
- data/lib/apipie/generator/swagger/param_description/type.rb +132 -0
- data/lib/apipie/generator/swagger/param_description.rb +18 -0
- data/lib/apipie/generator/swagger/path_decorator.rb +36 -0
- data/lib/apipie/generator/swagger/referenced_definitions.rb +17 -0
- data/lib/apipie/generator/swagger/resource_description_collection.rb +30 -0
- data/lib/apipie/generator/swagger/resource_description_composite.rb +56 -0
- data/lib/apipie/generator/swagger/schema.rb +63 -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 +51 -0
- data/lib/apipie/generator/swagger/warning.rb +74 -0
- data/lib/apipie/generator/swagger/warning_writer.rb +54 -0
- data/lib/apipie/helpers.rb +73 -0
- data/lib/apipie/markup.rb +52 -0
- 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 +230 -0
- data/lib/apipie/middleware/checksum_in_headers.rb +35 -0
- data/lib/apipie/param_description/deprecation.rb +24 -0
- data/lib/apipie/param_description.rb +313 -0
- data/lib/apipie/railtie.rb +9 -0
- data/lib/apipie/resource_description.rb +152 -0
- data/lib/apipie/response_description.rb +157 -0
- data/lib/apipie/response_description_adapter.rb +202 -0
- data/lib/apipie/routes_formatter.rb +33 -0
- data/lib/apipie/routing.rb +16 -0
- data/lib/apipie/rspec/response_validation_helper.rb +194 -0
- data/lib/apipie/see_description.rb +39 -0
- data/lib/apipie/static_dispatcher.rb +75 -0
- data/lib/apipie/swagger_generator.rb +45 -0
- data/lib/apipie/tag_list_description.rb +11 -0
- data/lib/apipie/validator.rb +552 -0
- data/lib/apipie/version.rb +3 -0
- data/lib/apipie-rails.rb +60 -0
- data/lib/generators/apipie/install/README +6 -0
- data/lib/generators/apipie/install/install_generator.rb +25 -0
- data/lib/generators/apipie/install/templates/initializer.rb.erb +7 -0
- data/lib/generators/apipie/views_generator.rb +11 -0
- data/lib/tasks/apipie.rake +355 -0
- data/rel-eng/gem_release.ipynb +398 -0
- data/rel-eng/packages/.readme +3 -0
- data/rel-eng/packages/rubygem-apipie-rails +1 -0
- data/rel-eng/tito.props +5 -0
- data/spec/controllers/api/v1/architectures_controller_spec.rb +29 -0
- data/spec/controllers/api/v2/architectures_controller_spec.rb +19 -0
- data/spec/controllers/api/v2/empty_middle_controller_spec.rb +23 -0
- data/spec/controllers/api/v2/nested/resources_controller_spec.rb +27 -0
- data/spec/controllers/api/v2/sub/footguns_controller_spec.rb +19 -0
- data/spec/controllers/concerns_controller_spec.rb +42 -0
- data/spec/controllers/extended_controller_spec.rb +14 -0
- data/spec/controllers/included_param_group_controller_spec.rb +13 -0
- data/spec/controllers/pets_controller_spec.rb +98 -0
- data/spec/controllers/users_controller_spec.rb +794 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/controllers/api/base_controller.rb +4 -0
- data/spec/dummy/app/controllers/api/v1/architectures_controller.rb +43 -0
- data/spec/dummy/app/controllers/api/v1/base_controller.rb +11 -0
- data/spec/dummy/app/controllers/api/v2/architectures_controller.rb +31 -0
- data/spec/dummy/app/controllers/api/v2/base_controller.rb +17 -0
- data/spec/dummy/app/controllers/api/v2/empty_middle_controller.rb +14 -0
- data/spec/dummy/app/controllers/api/v2/nested/architectures_controller.rb +32 -0
- data/spec/dummy/app/controllers/api/v2/nested/resources_controller.rb +33 -0
- data/spec/dummy/app/controllers/api/v2/sub/footguns_controller.rb +30 -0
- data/spec/dummy/app/controllers/application_controller.rb +18 -0
- data/spec/dummy/app/controllers/concerns_controller.rb +8 -0
- 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/files_controller.rb +5 -0
- data/spec/dummy/app/controllers/included_param_group_controller.rb +19 -0
- data/spec/dummy/app/controllers/overridden_concerns_controller.rb +31 -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/sample_controller.rb +39 -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 +307 -0
- data/spec/dummy/app/controllers/users_controller.rb +310 -0
- data/spec/dummy/app/helpers/random_param_group.rb +8 -0
- data/spec/dummy/app/views/layouts/application.html.erb +21 -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 +47 -0
- data/spec/dummy/config/boot.rb +12 -0
- data/spec/dummy/config/database.yml +21 -0
- data/spec/dummy/config/environment.rb +8 -0
- data/spec/dummy/config/environments/development.rb +25 -0
- data/spec/dummy/config/environments/production.rb +49 -0
- data/spec/dummy/config/environments/test.rb +33 -0
- data/spec/dummy/config/initializers/apipie.rb +110 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +10 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +8 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +61 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/db/.gitkeep +0 -0
- data/spec/dummy/doc/apipie_examples.json +1 -0
- data/spec/dummy/doc/users/desc_from_file.md +1 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +26 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/public/stylesheets/.gitkeep +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/lib/apipie/apipies_controller_spec.rb +345 -0
- data/spec/lib/apipie/application_spec.rb +62 -0
- data/spec/lib/apipie/configuration_spec.rb +38 -0
- data/spec/lib/apipie/extractor/collector_spec.rb +57 -0
- data/spec/lib/apipie/extractor/recorder/middleware_spec.rb +44 -0
- data/spec/lib/apipie/extractor/recorder_spec.rb +77 -0
- data/spec/lib/apipie/extractor/writer_spec.rb +112 -0
- data/spec/lib/apipie/extractor_spec.rb +9 -0
- data/spec/lib/apipie/file_handler_spec.rb +25 -0
- data/spec/lib/apipie/generator/swagger/config_spec.rb +19 -0
- data/spec/lib/apipie/generator/swagger/context_spec.rb +56 -0
- data/spec/lib/apipie/generator/swagger/method_description/api_schema_service_spec.rb +119 -0
- data/spec/lib/apipie/generator/swagger/method_description/response_schema_service_spec.rb +105 -0
- data/spec/lib/apipie/generator/swagger/method_description/response_service_spec.rb +62 -0
- data/spec/lib/apipie/generator/swagger/operation_id_spec.rb +63 -0
- data/spec/lib/apipie/generator/swagger/param_description/builder_spec.rb +245 -0
- data/spec/lib/apipie/generator/swagger/param_description/composite_spec.rb +95 -0
- data/spec/lib/apipie/generator/swagger/param_description/description_spec.rb +79 -0
- data/spec/lib/apipie/generator/swagger/param_description/in_spec.rb +86 -0
- data/spec/lib/apipie/generator/swagger/param_description/name_spec.rb +81 -0
- data/spec/lib/apipie/generator/swagger/param_description/type_spec.rb +210 -0
- data/spec/lib/apipie/generator/swagger/param_description_spec.rb +28 -0
- data/spec/lib/apipie/generator/swagger/path_decorator_spec.rb +57 -0
- data/spec/lib/apipie/generator/swagger/referenced_definitions_spec.rb +35 -0
- data/spec/lib/apipie/generator/swagger/resource_description_composite_spec.rb +37 -0
- data/spec/lib/apipie/generator/swagger/resource_descriptions_collection_spec.rb +57 -0
- data/spec/lib/apipie/generator/swagger/schema_spec.rb +89 -0
- data/spec/lib/apipie/generator/swagger/type_extractor_spec.rb +38 -0
- data/spec/lib/apipie/generator/swagger/warning_spec.rb +51 -0
- data/spec/lib/apipie/generator/swagger/warning_writer_spec.rb +71 -0
- data/spec/lib/apipie/method_description/apis_service_spec.rb +60 -0
- data/spec/lib/apipie/method_description_spec.rb +133 -0
- data/spec/lib/apipie/no_documented_method_spec.rb +17 -0
- data/spec/lib/apipie/param_description/deprecation_spec.rb +31 -0
- data/spec/lib/apipie/param_description_spec.rb +671 -0
- data/spec/lib/apipie/param_group_spec.rb +61 -0
- data/spec/lib/apipie/resource_description_spec.rb +91 -0
- data/spec/lib/apipie/response_description/response_object_spec.rb +22 -0
- data/spec/lib/apipie/response_description_spec.rb +56 -0
- data/spec/lib/apipie/response_does_not_match_swagger_schema_spec.rb +35 -0
- data/spec/lib/apipie/swagger_generator_spec.rb +94 -0
- data/spec/lib/apipie/validator_spec.rb +149 -0
- data/spec/lib/rake_spec.rb +69 -0
- data/spec/lib/swagger/openapi_2_0_schema.json +1614 -0
- data/spec/lib/swagger/rake_swagger_spec.rb +159 -0
- data/spec/lib/swagger/swagger_dsl_spec.rb +664 -0
- data/spec/lib/validators/array_validator_spec.rb +85 -0
- data/spec/spec_helper.rb +92 -0
- data/spec/support/custom_bool_validator.rb +17 -0
- data/spec/support/rake.rb +21 -0
- data/spec/test_engine/memes_controller_spec.rb +10 -0
- metadata +499 -0
@@ -0,0 +1,552 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
module Apipie
|
4
|
+
|
5
|
+
module Validator
|
6
|
+
|
7
|
+
# to create new validator, inherit from Apipie::Validator::BaseValidator
|
8
|
+
# and implement class method build and instance method validate
|
9
|
+
class BaseValidator
|
10
|
+
|
11
|
+
attr_accessor :param_description
|
12
|
+
|
13
|
+
def initialize(param_description)
|
14
|
+
@param_description = param_description
|
15
|
+
end
|
16
|
+
|
17
|
+
def inspected_fields
|
18
|
+
[:param_description]
|
19
|
+
end
|
20
|
+
|
21
|
+
def inspect
|
22
|
+
string = "#<#{self.class.name}:#{self.object_id} "
|
23
|
+
fields = inspected_fields.map {|field| "#{field}: #{self.send(field)}"}
|
24
|
+
string << fields.join(", ") << ">"
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.inherited(subclass)
|
28
|
+
@validators ||= []
|
29
|
+
@validators.insert 0, subclass
|
30
|
+
end
|
31
|
+
|
32
|
+
# find the right validator for given options
|
33
|
+
def self.find(param_description, argument, options, block)
|
34
|
+
@validators.each do |validator_type|
|
35
|
+
validator = validator_type.build(param_description, argument, options, block)
|
36
|
+
return validator if validator
|
37
|
+
end
|
38
|
+
return nil
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.raise_if_missing_params
|
42
|
+
missing_params = []
|
43
|
+
yield missing_params
|
44
|
+
if missing_params.size > 1
|
45
|
+
raise ParamMultipleMissing.new(missing_params)
|
46
|
+
elsif missing_params.size == 1
|
47
|
+
raise ParamMissing.new(missing_params.first)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# check if value is valid
|
52
|
+
def valid?(value)
|
53
|
+
if self.validate(value)
|
54
|
+
@error_value = nil
|
55
|
+
true
|
56
|
+
else
|
57
|
+
@error_value = value
|
58
|
+
false
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def param_name
|
63
|
+
@param_description.name
|
64
|
+
end
|
65
|
+
|
66
|
+
# validator description
|
67
|
+
def description
|
68
|
+
"TODO: validator description"
|
69
|
+
end
|
70
|
+
|
71
|
+
def format_description_value(value)
|
72
|
+
"<code>#{CGI::escapeHTML(value.to_s)}</code>"
|
73
|
+
end
|
74
|
+
|
75
|
+
def error
|
76
|
+
ParamInvalid.new(param_name, @error_value, description)
|
77
|
+
end
|
78
|
+
|
79
|
+
def to_s
|
80
|
+
self.description
|
81
|
+
end
|
82
|
+
|
83
|
+
def to_json
|
84
|
+
self.description
|
85
|
+
end
|
86
|
+
|
87
|
+
# what type is expected, mostly string
|
88
|
+
# this information is used in cli client
|
89
|
+
# thor supported types :string, :hash, :array, :numeric, or :boolean
|
90
|
+
def expected_type
|
91
|
+
'string'
|
92
|
+
end
|
93
|
+
|
94
|
+
def ignore_allow_blank?
|
95
|
+
false
|
96
|
+
end
|
97
|
+
|
98
|
+
def merge_with(other_validator)
|
99
|
+
return self if self == other_validator
|
100
|
+
raise NotImplementedError, "Don't know how to merge #{self.inspect} with #{other_validator.inspect}"
|
101
|
+
end
|
102
|
+
|
103
|
+
def params_ordered
|
104
|
+
nil
|
105
|
+
end
|
106
|
+
|
107
|
+
def ==(other)
|
108
|
+
return false unless self.class == other.class
|
109
|
+
if param_description == other.param_description
|
110
|
+
true
|
111
|
+
else
|
112
|
+
false
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
# validate arguments type
|
118
|
+
class TypeValidator < BaseValidator
|
119
|
+
|
120
|
+
def initialize(param_description, argument)
|
121
|
+
super(param_description)
|
122
|
+
@type = argument
|
123
|
+
end
|
124
|
+
|
125
|
+
def validate(value)
|
126
|
+
return false if value.nil?
|
127
|
+
value.is_a? @type
|
128
|
+
end
|
129
|
+
|
130
|
+
def self.build(param_description, argument, options, block)
|
131
|
+
if argument.is_a?(Class) && (argument != Hash || block.nil?)
|
132
|
+
self.new(param_description, argument)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def description
|
137
|
+
"Must be a #{@type}"
|
138
|
+
end
|
139
|
+
|
140
|
+
def expected_type
|
141
|
+
if @type.ancestors.include? Hash
|
142
|
+
'hash'
|
143
|
+
elsif @type.ancestors.include? Array
|
144
|
+
'array'
|
145
|
+
elsif @type.ancestors.include? Numeric
|
146
|
+
'numeric'
|
147
|
+
elsif @type.ancestors.include? File
|
148
|
+
'file'
|
149
|
+
else
|
150
|
+
'string'
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
# validate arguments value with regular expression
|
156
|
+
class RegexpValidator < BaseValidator
|
157
|
+
|
158
|
+
def initialize(param_description, argument)
|
159
|
+
super(param_description)
|
160
|
+
@regexp = argument
|
161
|
+
end
|
162
|
+
|
163
|
+
def validate(value)
|
164
|
+
value =~ @regexp
|
165
|
+
end
|
166
|
+
|
167
|
+
def self.build(param_description, argument, options, proc)
|
168
|
+
self.new(param_description, argument) if argument.is_a? Regexp
|
169
|
+
end
|
170
|
+
|
171
|
+
def description
|
172
|
+
"Must match regular expression #{format_description_value("/#{@regexp.source}/")}."
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
# arguments value must be one of given in array
|
177
|
+
class EnumValidator < BaseValidator
|
178
|
+
def initialize(param_description, argument)
|
179
|
+
super(param_description)
|
180
|
+
@array = argument
|
181
|
+
end
|
182
|
+
|
183
|
+
def validate(value)
|
184
|
+
@array.include?(value)
|
185
|
+
end
|
186
|
+
|
187
|
+
def self.build(param_description, argument, options, proc)
|
188
|
+
self.new(param_description, argument) if argument.is_a?(Array)
|
189
|
+
end
|
190
|
+
|
191
|
+
def values
|
192
|
+
@array
|
193
|
+
end
|
194
|
+
|
195
|
+
def description
|
196
|
+
string = @array.map { |value| format_description_value(value) }.join(', ')
|
197
|
+
"Must be one of: #{string}."
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
# arguments value must be an array
|
202
|
+
class ArrayValidator < Apipie::Validator::BaseValidator
|
203
|
+
def initialize(param_description, argument, options = {})
|
204
|
+
super(param_description)
|
205
|
+
@type = argument
|
206
|
+
@items_type = options[:of]
|
207
|
+
@items_enum = options[:in]
|
208
|
+
end
|
209
|
+
|
210
|
+
def validate(values)
|
211
|
+
return false unless process_value(values).respond_to?(:each) && !process_value(values).is_a?(String)
|
212
|
+
process_value(values).all? { |v| validate_item(v)}
|
213
|
+
end
|
214
|
+
|
215
|
+
def process_value(values)
|
216
|
+
values || []
|
217
|
+
end
|
218
|
+
|
219
|
+
def description
|
220
|
+
"Must be an array of #{items}"
|
221
|
+
end
|
222
|
+
|
223
|
+
def expected_type
|
224
|
+
"array"
|
225
|
+
end
|
226
|
+
|
227
|
+
def self.build(param_description, argument, options, block)
|
228
|
+
if argument == Array && !block.is_a?(Proc)
|
229
|
+
self.new(param_description, argument, options)
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
private
|
234
|
+
|
235
|
+
def enum
|
236
|
+
if @items_enum.kind_of?(Proc)
|
237
|
+
@items_enum = Array(@items_enum.call)
|
238
|
+
end
|
239
|
+
@items_enum
|
240
|
+
end
|
241
|
+
|
242
|
+
def validate_item(value)
|
243
|
+
has_valid_type?(value) &&
|
244
|
+
is_valid_value?(value)
|
245
|
+
end
|
246
|
+
|
247
|
+
def has_valid_type?(value)
|
248
|
+
if @items_type
|
249
|
+
item_validator = BaseValidator.find('', @items_type, nil, nil)
|
250
|
+
|
251
|
+
if item_validator
|
252
|
+
item_validator.valid?(value)
|
253
|
+
else
|
254
|
+
value.kind_of?(@items_type)
|
255
|
+
end
|
256
|
+
else
|
257
|
+
true
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
def is_valid_value?(value)
|
262
|
+
if enum
|
263
|
+
enum.include?(value)
|
264
|
+
else
|
265
|
+
true
|
266
|
+
end
|
267
|
+
end
|
268
|
+
|
269
|
+
def items
|
270
|
+
unless enum
|
271
|
+
@items_type || "any type"
|
272
|
+
else
|
273
|
+
enum.inspect
|
274
|
+
end
|
275
|
+
end
|
276
|
+
end
|
277
|
+
|
278
|
+
class ArrayClassValidator < BaseValidator
|
279
|
+
|
280
|
+
def initialize(param_description, argument)
|
281
|
+
super(param_description)
|
282
|
+
@array = argument
|
283
|
+
end
|
284
|
+
|
285
|
+
def validate(value)
|
286
|
+
@array.include?(value.class)
|
287
|
+
end
|
288
|
+
|
289
|
+
def self.build(param_description, argument, options, block)
|
290
|
+
if argument.is_a?(Array) && argument.first.class == Class && !block.is_a?(Proc)
|
291
|
+
self.new(param_description, argument)
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
def description
|
296
|
+
string = @array.map { |value| format_description_value(value) }.join(', ')
|
297
|
+
"Must be one of: #{string}."
|
298
|
+
end
|
299
|
+
end
|
300
|
+
|
301
|
+
class ProcValidator < BaseValidator
|
302
|
+
|
303
|
+
def initialize(param_description, argument)
|
304
|
+
super(param_description)
|
305
|
+
@proc = argument
|
306
|
+
end
|
307
|
+
|
308
|
+
def validate(value)
|
309
|
+
(@help = @proc.call(value)) === true
|
310
|
+
end
|
311
|
+
|
312
|
+
def self.build(param_description, argument, options, proc)
|
313
|
+
self.new(param_description, argument) if argument.is_a?(Proc) && argument.arity == 1
|
314
|
+
end
|
315
|
+
|
316
|
+
def error
|
317
|
+
ParamInvalid.new(param_name, @error_value, @help)
|
318
|
+
end
|
319
|
+
|
320
|
+
def description
|
321
|
+
""
|
322
|
+
end
|
323
|
+
end
|
324
|
+
|
325
|
+
class HashValidator < BaseValidator
|
326
|
+
include Apipie::DSL::Base
|
327
|
+
include Apipie::DSL::Param
|
328
|
+
|
329
|
+
def self.build(param_description, argument, options, block)
|
330
|
+
self.new(param_description, block, options[:param_group]) if block.is_a?(Proc) && block.arity <= 0 && argument == Hash
|
331
|
+
end
|
332
|
+
|
333
|
+
def initialize(param_description, argument, param_group)
|
334
|
+
super(param_description)
|
335
|
+
@proc = argument
|
336
|
+
@param_group = param_group
|
337
|
+
self.instance_exec(&@proc)
|
338
|
+
# specifying action_aware on Hash influences the child params,
|
339
|
+
# not the hash param itself: assuming it's required when
|
340
|
+
# updating as well
|
341
|
+
if param_description.options[:action_aware] && param_description.options[:required]
|
342
|
+
param_description.required = true
|
343
|
+
end
|
344
|
+
prepare_hash_params
|
345
|
+
end
|
346
|
+
|
347
|
+
def params_ordered
|
348
|
+
@params_ordered ||= _apipie_dsl_data[:params].map do |args|
|
349
|
+
options = args.find { |arg| arg.is_a? Hash }
|
350
|
+
options[:parent] = self.param_description
|
351
|
+
options[:param_group] = @param_group
|
352
|
+
Apipie::ParamDescription.from_dsl_data(param_description.method_description, args)
|
353
|
+
end
|
354
|
+
end
|
355
|
+
|
356
|
+
def validate(value)
|
357
|
+
return false if !value.is_a? Hash
|
358
|
+
|
359
|
+
BaseValidator.raise_if_missing_params do |missing|
|
360
|
+
@hash_params&.each do |k, p|
|
361
|
+
if Apipie.configuration.validate_presence?
|
362
|
+
missing << p if p.required && !value.key?(k)
|
363
|
+
end
|
364
|
+
if Apipie.configuration.validate_value?
|
365
|
+
p.validate(value[k]) if value.key?(k)
|
366
|
+
end
|
367
|
+
end
|
368
|
+
end
|
369
|
+
|
370
|
+
return true
|
371
|
+
end
|
372
|
+
|
373
|
+
def process_value(value)
|
374
|
+
if @hash_params && value
|
375
|
+
return @hash_params.each_with_object({}) do |(key, param), api_params|
|
376
|
+
if value.key?(key)
|
377
|
+
api_params[param.as] = param.process_value(value[key])
|
378
|
+
end
|
379
|
+
end
|
380
|
+
end
|
381
|
+
end
|
382
|
+
|
383
|
+
def description
|
384
|
+
"Must be a Hash"
|
385
|
+
end
|
386
|
+
|
387
|
+
def expected_type
|
388
|
+
'hash'
|
389
|
+
end
|
390
|
+
|
391
|
+
# where the group definition should be looked up when no scope
|
392
|
+
# given. This is expected to return a controller.
|
393
|
+
def _default_param_group_scope
|
394
|
+
@param_group && @param_group[:scope]
|
395
|
+
end
|
396
|
+
|
397
|
+
def merge_with(other_validator)
|
398
|
+
if other_validator.is_a? HashValidator
|
399
|
+
@params_ordered = ParamDescription.unify(self.params_ordered + other_validator.params_ordered)
|
400
|
+
prepare_hash_params
|
401
|
+
else
|
402
|
+
super
|
403
|
+
end
|
404
|
+
end
|
405
|
+
|
406
|
+
def prepare_hash_params
|
407
|
+
@hash_params = params_ordered.reduce({}) do |h, param|
|
408
|
+
h.update(param.name.to_sym => param)
|
409
|
+
end
|
410
|
+
end
|
411
|
+
end
|
412
|
+
|
413
|
+
|
414
|
+
# special type of validator: we say that it's not specified
|
415
|
+
class UndefValidator < BaseValidator
|
416
|
+
|
417
|
+
def validate(value)
|
418
|
+
true
|
419
|
+
end
|
420
|
+
|
421
|
+
def self.build(param_description, argument, options, block)
|
422
|
+
if argument == :undef
|
423
|
+
self.new(param_description)
|
424
|
+
end
|
425
|
+
end
|
426
|
+
|
427
|
+
def description
|
428
|
+
nil
|
429
|
+
end
|
430
|
+
end
|
431
|
+
|
432
|
+
class DecimalValidator < BaseValidator
|
433
|
+
|
434
|
+
delegate :validate, to: :class
|
435
|
+
|
436
|
+
def self.build(param_description, argument, options, block)
|
437
|
+
if argument == :decimal
|
438
|
+
self.new(param_description)
|
439
|
+
end
|
440
|
+
end
|
441
|
+
|
442
|
+
def description
|
443
|
+
"Must be a decimal number."
|
444
|
+
end
|
445
|
+
|
446
|
+
def expected_type
|
447
|
+
'numeric'
|
448
|
+
end
|
449
|
+
|
450
|
+
def self.validate(value)
|
451
|
+
value.to_s =~ /\A^[-+]?[0-9]+([,.][0-9]+)?\Z$/
|
452
|
+
end
|
453
|
+
end
|
454
|
+
|
455
|
+
class NumberValidator < BaseValidator
|
456
|
+
|
457
|
+
delegate :validate, to: :class
|
458
|
+
|
459
|
+
def self.build(param_description, argument, options, block)
|
460
|
+
if argument == :number
|
461
|
+
self.new(param_description)
|
462
|
+
end
|
463
|
+
end
|
464
|
+
|
465
|
+
def description
|
466
|
+
"Must be a number."
|
467
|
+
end
|
468
|
+
|
469
|
+
def expected_type
|
470
|
+
'numeric'
|
471
|
+
end
|
472
|
+
|
473
|
+
def self.validate(value)
|
474
|
+
value.to_s =~ /\A(0|[1-9]\d*)\Z$/
|
475
|
+
end
|
476
|
+
end
|
477
|
+
|
478
|
+
class BooleanValidator < BaseValidator
|
479
|
+
|
480
|
+
def validate(value)
|
481
|
+
%w[true false 1 0].include?(value.to_s)
|
482
|
+
end
|
483
|
+
|
484
|
+
def self.build(param_description, argument, options, block)
|
485
|
+
if argument == :bool || argument == :boolean || boolean_array?(argument)
|
486
|
+
self.new(param_description)
|
487
|
+
end
|
488
|
+
end
|
489
|
+
|
490
|
+
private_class_method def self.boolean_array?(argument)
|
491
|
+
argument.is_a?(Array) && (argument - [true, false]) == []
|
492
|
+
end
|
493
|
+
|
494
|
+
def description
|
495
|
+
string = %w(true false 1 0).map { |value| format_description_value(value) }.join(', ')
|
496
|
+
"Must be one of: #{string}."
|
497
|
+
end
|
498
|
+
|
499
|
+
def ignore_allow_blank?
|
500
|
+
true
|
501
|
+
end
|
502
|
+
|
503
|
+
def expected_type
|
504
|
+
'boolean'
|
505
|
+
end
|
506
|
+
end
|
507
|
+
|
508
|
+
class NestedValidator < BaseValidator
|
509
|
+
|
510
|
+
def initialize(param_description, argument, param_group)
|
511
|
+
super(param_description)
|
512
|
+
@validator = Apipie::Validator::HashValidator.new(param_description, argument, param_group)
|
513
|
+
@type = argument
|
514
|
+
end
|
515
|
+
|
516
|
+
def validate(value)
|
517
|
+
value ||= [] # Rails convert empty array to nil
|
518
|
+
return false if value.class != Array
|
519
|
+
value.each do |child|
|
520
|
+
return false unless @validator.validate(child)
|
521
|
+
end
|
522
|
+
true
|
523
|
+
end
|
524
|
+
|
525
|
+
def process_value(value)
|
526
|
+
value ||= [] # Rails convert empty array to nil
|
527
|
+
@values = []
|
528
|
+
value.each do |child|
|
529
|
+
@values << @validator.process_value(child)
|
530
|
+
end
|
531
|
+
@values
|
532
|
+
end
|
533
|
+
|
534
|
+
def self.build(param_description, argument, options, block)
|
535
|
+
# in Ruby 1.8.x the arity on block without args is -1
|
536
|
+
# while in Ruby 1.9+ it is 0
|
537
|
+
self.new(param_description, block, options[:param_group]) if block.is_a?(Proc) && block.arity <= 0 && argument == Array
|
538
|
+
end
|
539
|
+
|
540
|
+
def expected_type
|
541
|
+
'array'
|
542
|
+
end
|
543
|
+
|
544
|
+
def description
|
545
|
+
"Must be an Array of nested elements"
|
546
|
+
end
|
547
|
+
|
548
|
+
delegate :params_ordered, to: :@validator
|
549
|
+
end
|
550
|
+
|
551
|
+
end
|
552
|
+
end
|
data/lib/apipie-rails.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'i18n'
|
2
|
+
require 'json'
|
3
|
+
require 'active_support/hash_with_indifferent_access'
|
4
|
+
|
5
|
+
require 'apipie/core_ext/route'
|
6
|
+
|
7
|
+
require "apipie/routing"
|
8
|
+
require "apipie/markup"
|
9
|
+
require "apipie/apipie_module"
|
10
|
+
require "apipie/dsl_definition"
|
11
|
+
require "apipie/generator/swagger/config"
|
12
|
+
require "apipie/configuration"
|
13
|
+
require "apipie/method_description"
|
14
|
+
require "apipie/resource_description"
|
15
|
+
require "apipie/param_description"
|
16
|
+
require "apipie/param_description/deprecation"
|
17
|
+
require "apipie/method_description/api"
|
18
|
+
require "apipie/method_description/apis_service"
|
19
|
+
require "apipie/errors"
|
20
|
+
require "apipie/error_description"
|
21
|
+
require "apipie/response_description"
|
22
|
+
require "apipie/response_description_adapter"
|
23
|
+
require "apipie/see_description"
|
24
|
+
require "apipie/tag_list_description"
|
25
|
+
require "apipie/validator"
|
26
|
+
require "apipie/railtie"
|
27
|
+
require 'apipie/extractor'
|
28
|
+
require "apipie/version"
|
29
|
+
require "apipie/swagger_generator"
|
30
|
+
require "apipie/generator/generator"
|
31
|
+
require "apipie/generator/config"
|
32
|
+
require "apipie/generator/swagger/swagger"
|
33
|
+
require "apipie/generator/swagger/schema"
|
34
|
+
require "apipie/generator/swagger/operation_id"
|
35
|
+
require "apipie/generator/swagger/warning"
|
36
|
+
require "apipie/generator/swagger/warning_writer"
|
37
|
+
require "apipie/generator/swagger/type"
|
38
|
+
require "apipie/generator/swagger/type_extractor"
|
39
|
+
require "apipie/generator/swagger/context"
|
40
|
+
require "apipie/generator/swagger/computed_interface_id"
|
41
|
+
require "apipie/generator/swagger/path_decorator"
|
42
|
+
require "apipie/generator/swagger/referenced_definitions"
|
43
|
+
require "apipie/generator/swagger/resource_description_composite"
|
44
|
+
require "apipie/generator/swagger/resource_description_collection"
|
45
|
+
require "apipie/generator/swagger/method_description"
|
46
|
+
require "apipie/generator/swagger/method_description/parameters_service"
|
47
|
+
require "apipie/generator/swagger/method_description/response_service"
|
48
|
+
require "apipie/generator/swagger/method_description/response_schema_service"
|
49
|
+
require "apipie/generator/swagger/method_description/decorator"
|
50
|
+
require "apipie/generator/swagger/method_description/api_decorator"
|
51
|
+
require "apipie/generator/swagger/method_description/api_schema_service"
|
52
|
+
require "apipie/generator/swagger/param_description"
|
53
|
+
require "apipie/generator/swagger/param_description/builder"
|
54
|
+
require "apipie/generator/swagger/param_description/composite"
|
55
|
+
require "apipie/generator/swagger/param_description/description"
|
56
|
+
require "apipie/generator/swagger/param_description/in"
|
57
|
+
require "apipie/generator/swagger/param_description/name"
|
58
|
+
require "apipie/generator/swagger/param_description/path_params_composite"
|
59
|
+
require "apipie/generator/swagger/param_description/referenced_composite"
|
60
|
+
require "apipie/generator/swagger/param_description/type"
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Apipie
|
2
|
+
class InstallGenerator < ::Rails::Generators::Base
|
3
|
+
source_root File.expand_path('templates', __dir__)
|
4
|
+
|
5
|
+
class_option(:route,
|
6
|
+
:aliases => "-r",
|
7
|
+
:type => :string,
|
8
|
+
:desc => "What path should be the doc available on",
|
9
|
+
:default => "/apipie")
|
10
|
+
|
11
|
+
class_option(:api_path,
|
12
|
+
:alias => "-a",
|
13
|
+
:type => :string,
|
14
|
+
:desc => "What path are API requests on",
|
15
|
+
:default => "/api")
|
16
|
+
|
17
|
+
def create_initializer
|
18
|
+
template 'initializer.rb.erb', 'config/initializers/apipie.rb'
|
19
|
+
end
|
20
|
+
|
21
|
+
def add_route
|
22
|
+
route("apipie")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
Apipie.configure do |config|
|
2
|
+
config.app_name = "<%= Rails.application.class.name[/^\w+/] %>"
|
3
|
+
config.api_base_url = "<%= options.api_path %>"
|
4
|
+
config.doc_base_url = "<%= options.route %>"
|
5
|
+
# where is your API defined?
|
6
|
+
config.api_controllers_matcher = "#{Rails.root}/app/controllers/**/*.rb"
|
7
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Apipie
|
2
|
+
class ViewsGenerator < ::Rails::Generators::Base
|
3
|
+
source_root File.expand_path('../../../app/views', __dir__)
|
4
|
+
desc 'Copy Apipie views to your application'
|
5
|
+
|
6
|
+
def copy_views
|
7
|
+
directory 'apipie', 'app/views/apipie'
|
8
|
+
directory 'layouts/apipie', 'app/views/layouts/apipie'
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|