apipie-rails 0.5.19 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (181) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/build.yml +31 -0
  3. data/.github/workflows/rubocop-challenger.yml +28 -0
  4. data/.github/workflows/rubocop.yml +18 -0
  5. data/.gitignore +1 -0
  6. data/.rubocop.yml +128 -0
  7. data/.rubocop_todo.yml +2056 -0
  8. data/.vscode/settings.json +3 -0
  9. data/CHANGELOG.md +161 -0
  10. data/Gemfile +20 -0
  11. data/README.rst +117 -23
  12. data/Rakefile +0 -5
  13. data/apipie-rails.gemspec +18 -9
  14. data/app/controllers/apipie/apipies_controller.rb +14 -29
  15. data/app/helpers/apipie_helper.rb +1 -1
  16. data/app/public/apipie/javascripts/bundled/bootstrap-collapse.js +70 -41
  17. data/app/public/apipie/javascripts/bundled/bootstrap.js +1033 -479
  18. data/app/public/apipie/javascripts/bundled/jquery.js +5 -5
  19. data/app/public/apipie/stylesheets/bundled/bootstrap-responsive.min.css +9 -12
  20. data/app/public/apipie/stylesheets/bundled/bootstrap.min.css +9 -689
  21. data/app/views/apipie/apipies/_deprecation.html.erb +16 -0
  22. data/app/views/apipie/apipies/_params.html.erb +7 -1
  23. data/config/locales/en.yml +8 -0
  24. data/config/locales/ko.yml +31 -0
  25. data/gemfiles/Gemfile.tools +9 -0
  26. data/lib/apipie/apipie_module.rb +7 -7
  27. data/lib/apipie/application.rb +132 -97
  28. data/lib/apipie/configuration.rb +43 -33
  29. data/lib/apipie/dsl_definition.rb +44 -33
  30. data/lib/apipie/error_description.rb +3 -3
  31. data/lib/apipie/errors.rb +17 -17
  32. data/lib/apipie/extractor/collector.rb +5 -6
  33. data/lib/apipie/extractor/recorder.rb +35 -8
  34. data/lib/apipie/extractor/writer.rb +15 -15
  35. data/lib/apipie/extractor.rb +6 -9
  36. data/lib/apipie/generator/config.rb +12 -0
  37. data/lib/apipie/generator/generator.rb +2 -0
  38. data/lib/apipie/generator/swagger/computed_interface_id.rb +23 -0
  39. data/lib/apipie/generator/swagger/config.rb +80 -0
  40. data/lib/apipie/generator/swagger/context.rb +38 -0
  41. data/lib/apipie/generator/swagger/method_description/api_decorator.rb +20 -0
  42. data/lib/apipie/generator/swagger/method_description/api_schema_service.rb +89 -0
  43. data/lib/apipie/generator/swagger/method_description/decorator.rb +22 -0
  44. data/lib/apipie/generator/swagger/method_description/parameters_service.rb +139 -0
  45. data/lib/apipie/generator/swagger/method_description/response_schema_service.rb +46 -0
  46. data/lib/apipie/generator/swagger/method_description/response_service.rb +58 -0
  47. data/lib/apipie/generator/swagger/method_description.rb +2 -0
  48. data/lib/apipie/generator/swagger/operation_id.rb +51 -0
  49. data/lib/apipie/generator/swagger/param_description/builder.rb +114 -0
  50. data/lib/apipie/generator/swagger/param_description/composite.rb +119 -0
  51. data/lib/apipie/generator/swagger/param_description/description.rb +15 -0
  52. data/lib/apipie/generator/swagger/param_description/in.rb +37 -0
  53. data/lib/apipie/generator/swagger/param_description/name.rb +18 -0
  54. data/lib/apipie/generator/swagger/param_description/path_params_composite.rb +61 -0
  55. data/lib/apipie/generator/swagger/param_description/referenced_composite.rb +36 -0
  56. data/lib/apipie/generator/swagger/param_description/type.rb +128 -0
  57. data/lib/apipie/generator/swagger/param_description.rb +18 -0
  58. data/lib/apipie/generator/swagger/path_decorator.rb +36 -0
  59. data/lib/apipie/generator/swagger/referenced_definitions.rb +17 -0
  60. data/lib/apipie/generator/swagger/resource_description_collection.rb +30 -0
  61. data/lib/apipie/generator/swagger/resource_description_composite.rb +56 -0
  62. data/lib/apipie/generator/swagger/schema.rb +63 -0
  63. data/lib/apipie/generator/swagger/swagger.rb +2 -0
  64. data/lib/apipie/generator/swagger/type.rb +16 -0
  65. data/lib/apipie/generator/swagger/type_extractor.rb +51 -0
  66. data/lib/apipie/generator/swagger/warning.rb +74 -0
  67. data/lib/apipie/generator/swagger/warning_writer.rb +54 -0
  68. data/lib/apipie/helpers.rb +3 -3
  69. data/lib/apipie/markup.rb +9 -8
  70. data/lib/apipie/method_description/api.rb +12 -0
  71. data/lib/apipie/method_description/apis_service.rb +82 -0
  72. data/lib/apipie/method_description.rb +12 -56
  73. data/lib/apipie/param_description/deprecation.rb +24 -0
  74. data/lib/apipie/param_description.rb +57 -24
  75. data/lib/apipie/resource_description.rb +42 -14
  76. data/lib/apipie/response_description.rb +3 -3
  77. data/lib/apipie/response_description_adapter.rb +12 -10
  78. data/lib/apipie/routing.rb +1 -1
  79. data/lib/apipie/rspec/response_validation_helper.rb +3 -3
  80. data/lib/apipie/static_dispatcher.rb +10 -2
  81. data/lib/apipie/swagger_generator.rb +28 -691
  82. data/lib/apipie/validator.rb +41 -11
  83. data/lib/apipie/version.rb +1 -1
  84. data/lib/apipie-rails.rb +36 -5
  85. data/lib/generators/apipie/install/install_generator.rb +1 -1
  86. data/lib/generators/apipie/views_generator.rb +1 -1
  87. data/lib/tasks/apipie.rake +37 -32
  88. data/rel-eng/gem_release.ipynb +41 -9
  89. data/spec/controllers/api/v2/architectures_controller_spec.rb +10 -3
  90. data/spec/controllers/api/v2/empty_middle_controller_spec.rb +23 -0
  91. data/spec/controllers/api/v2/nested/resources_controller_spec.rb +18 -2
  92. data/spec/controllers/api/v2/sub/footguns_controller_spec.rb +19 -0
  93. data/spec/controllers/included_param_group_controller_spec.rb +13 -0
  94. data/spec/{lib/swagger/response_validation_spec.rb → controllers/pets_controller_spec.rb} +26 -32
  95. data/spec/controllers/users_controller_spec.rb +47 -6
  96. data/spec/dummy/Rakefile +1 -1
  97. data/spec/dummy/app/controllers/api/v2/architectures_controller.rb +2 -1
  98. data/spec/dummy/app/controllers/api/v2/base_controller.rb +6 -0
  99. data/spec/dummy/app/controllers/api/v2/empty_middle_controller.rb +14 -0
  100. data/spec/dummy/app/controllers/api/v2/nested/resources_controller.rb +2 -2
  101. data/spec/dummy/app/controllers/api/v2/sub/footguns_controller.rb +30 -0
  102. data/spec/dummy/app/controllers/concerns_controller.rb +1 -1
  103. data/spec/dummy/app/controllers/{concerns/extending_concern.rb → extending_concern.rb} +0 -2
  104. data/spec/dummy/app/controllers/included_param_group_controller.rb +19 -0
  105. data/spec/dummy/app/controllers/overridden_concerns_controller.rb +2 -2
  106. data/spec/dummy/app/controllers/pets_controller.rb +5 -5
  107. data/spec/dummy/app/controllers/pets_using_self_describing_classes_controller.rb +2 -2
  108. data/spec/dummy/app/controllers/{concerns/sample_controller.rb → sample_controller.rb} +0 -2
  109. data/spec/dummy/app/controllers/twitter_example_controller.rb +2 -2
  110. data/spec/dummy/app/controllers/users_controller.rb +17 -5
  111. data/spec/dummy/app/helpers/random_param_group.rb +8 -0
  112. data/spec/dummy/components/test_engine/test_engine.gemspec +1 -1
  113. data/spec/dummy/config/application.rb +2 -5
  114. data/spec/dummy/config/boot.rb +2 -2
  115. data/spec/dummy/config/environment.rb +1 -1
  116. data/spec/dummy/config/environments/development.rb +0 -3
  117. data/spec/dummy/config/environments/production.rb +0 -3
  118. data/spec/dummy/config/environments/test.rb +0 -5
  119. data/spec/dummy/config/initializers/apipie.rb +2 -2
  120. data/spec/dummy/config/routes.rb +8 -0
  121. data/spec/dummy/config.ru +1 -1
  122. data/spec/dummy/script/rails +2 -2
  123. data/spec/{controllers → lib/apipie}/apipies_controller_spec.rb +95 -23
  124. data/spec/lib/apipie/application_spec.rb +62 -0
  125. data/spec/lib/apipie/configuration_spec.rb +38 -0
  126. data/spec/lib/apipie/extractor/collector_spec.rb +57 -0
  127. data/spec/lib/apipie/extractor/recorder_spec.rb +77 -0
  128. data/spec/lib/{extractor → apipie/extractor}/writer_spec.rb +8 -6
  129. data/spec/lib/{extractor → apipie}/extractor_spec.rb +1 -1
  130. data/spec/lib/{file_handler_spec.rb → apipie/file_handler_spec.rb} +7 -0
  131. data/spec/lib/apipie/generator/swagger/config_spec.rb +19 -0
  132. data/spec/lib/apipie/generator/swagger/context_spec.rb +56 -0
  133. data/spec/lib/apipie/generator/swagger/method_description/api_schema_service_spec.rb +119 -0
  134. data/spec/lib/apipie/generator/swagger/method_description/response_schema_service_spec.rb +105 -0
  135. data/spec/lib/apipie/generator/swagger/operation_id_spec.rb +63 -0
  136. data/spec/lib/apipie/generator/swagger/param_description/builder_spec.rb +215 -0
  137. data/spec/lib/apipie/generator/swagger/param_description/composite_spec.rb +95 -0
  138. data/spec/lib/apipie/generator/swagger/param_description/description_spec.rb +79 -0
  139. data/spec/lib/apipie/generator/swagger/param_description/in_spec.rb +86 -0
  140. data/spec/lib/apipie/generator/swagger/param_description/name_spec.rb +81 -0
  141. data/spec/lib/apipie/generator/swagger/param_description/type_spec.rb +183 -0
  142. data/spec/lib/apipie/generator/swagger/param_description_spec.rb +28 -0
  143. data/spec/lib/apipie/generator/swagger/path_decorator_spec.rb +57 -0
  144. data/spec/lib/apipie/generator/swagger/referenced_definitions_spec.rb +35 -0
  145. data/spec/lib/apipie/generator/swagger/resource_description_composite_spec.rb +37 -0
  146. data/spec/lib/apipie/generator/swagger/resource_descriptions_collection_spec.rb +57 -0
  147. data/spec/lib/apipie/generator/swagger/schema_spec.rb +89 -0
  148. data/spec/lib/apipie/generator/swagger/type_extractor_spec.rb +38 -0
  149. data/spec/lib/apipie/generator/swagger/warning_spec.rb +51 -0
  150. data/spec/lib/apipie/generator/swagger/warning_writer_spec.rb +71 -0
  151. data/spec/lib/apipie/method_description/apis_service_spec.rb +60 -0
  152. data/spec/lib/apipie/method_description_spec.rb +133 -0
  153. data/spec/lib/apipie/no_documented_method_spec.rb +17 -0
  154. data/spec/lib/apipie/param_description/deprecation_spec.rb +31 -0
  155. data/spec/lib/{param_description_spec.rb → apipie/param_description_spec.rb} +332 -6
  156. data/spec/lib/{param_group_spec.rb → apipie/param_group_spec.rb} +6 -5
  157. data/spec/lib/apipie/resource_description_spec.rb +91 -0
  158. data/spec/lib/apipie/response_does_not_match_swagger_schema_spec.rb +35 -0
  159. data/spec/lib/apipie/swagger_generator_spec.rb +94 -0
  160. data/spec/lib/{validator_spec.rb → apipie/validator_spec.rb} +48 -12
  161. data/spec/lib/rake_spec.rb +3 -5
  162. data/spec/lib/swagger/openapi_2_0_schema.json +8 -1
  163. data/spec/lib/swagger/rake_swagger_spec.rb +30 -10
  164. data/spec/lib/swagger/swagger_dsl_spec.rb +18 -12
  165. data/spec/lib/validators/array_validator_spec.rb +1 -1
  166. data/spec/spec_helper.rb +10 -32
  167. data/spec/support/custom_bool_validator.rb +17 -0
  168. data/spec/{controllers → test_engine}/memes_controller_spec.rb +1 -1
  169. metadata +173 -125
  170. data/.travis.yml +0 -41
  171. data/Gemfile +0 -1
  172. data/Gemfile.rails41 +0 -7
  173. data/Gemfile.rails42 +0 -14
  174. data/Gemfile.rails50 +0 -9
  175. data/Gemfile.rails51 +0 -9
  176. data/Gemfile.rails60 +0 -10
  177. data/Gemfile.rails61 +0 -10
  178. data/spec/lib/application_spec.rb +0 -49
  179. data/spec/lib/method_description_spec.rb +0 -98
  180. data/spec/lib/resource_description_spec.rb +0 -48
  181. /data/spec/lib/{extractor → apipie/extractor/recorder}/middleware_spec.rb +0 -0
@@ -0,0 +1,86 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apipie::Generator::Swagger::ParamDescription::In do
4
+ let(:validator_options) { {} }
5
+ let(:param_description_options) { {}.merge(validator_options) }
6
+ let(:with_null) { false }
7
+ let(:http_method) { :GET }
8
+ let(:path) { '/api' }
9
+ let(:validator) { String }
10
+ let(:default_in_value) { 'kati' }
11
+ let(:in_schema) { true }
12
+
13
+ let(:base_dsl_data) do
14
+ {
15
+ :api => false,
16
+ :api_args => [],
17
+ :api_from_routes => nil,
18
+ :errors => [],
19
+ :tag_list => [],
20
+ :returns => {},
21
+ :params => [],
22
+ :headers => [],
23
+ :resource_id => nil,
24
+ :short_description => nil,
25
+ :description => nil,
26
+ :examples => [],
27
+ :see => [],
28
+ :formats => nil,
29
+ :api_versions => [],
30
+ :meta => nil,
31
+ :show => true,
32
+ :deprecated => false
33
+ }
34
+ end
35
+
36
+ let(:dsl_data) do
37
+ base_dsl_data.merge({
38
+ api_args: [
39
+ [
40
+ http_method,
41
+ path,
42
+ 'description',
43
+ { deprecated: true }
44
+ ]
45
+ ]
46
+ })
47
+ end
48
+
49
+ let(:resource_desc) do
50
+ Apipie::ResourceDescription.new(UsersController, 'dummy')
51
+ end
52
+
53
+ let(:method_desc) do
54
+ Apipie::MethodDescription.new(:show, resource_desc, dsl_data)
55
+ end
56
+
57
+ let(:param_description) do
58
+ Apipie::ParamDescription.new(
59
+ method_desc,
60
+ :param,
61
+ validator,
62
+ param_description_options
63
+ )
64
+ end
65
+
66
+ let(:in_definition) do
67
+ described_class.new(
68
+ param_description,
69
+ default_in_value: default_in_value,
70
+ http_method: http_method,
71
+ in_schema: in_schema
72
+ ).to_hash
73
+ end
74
+
75
+ describe 'in' do
76
+ subject { in_definition[:in] }
77
+
78
+ it { is_expected.to be_blank }
79
+
80
+ context 'when in_schema is false' do
81
+ let(:in_schema) { false }
82
+
83
+ it { is_expected.to eq(default_in_value) }
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,81 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apipie::Generator::Swagger::ParamDescription::Name do
4
+ let(:validator_options) { {} }
5
+ let(:param_description_options) { {}.merge(validator_options) }
6
+ let(:with_null) { false }
7
+ let(:http_method) { :GET }
8
+ let(:path) { '/api' }
9
+ let(:validator) { String }
10
+ let(:prefixed_by) {}
11
+
12
+ let(:base_dsl_data) do
13
+ {
14
+ :api => false,
15
+ :api_args => [],
16
+ :api_from_routes => nil,
17
+ :errors => [],
18
+ :tag_list => [],
19
+ :returns => {},
20
+ :params => [],
21
+ :headers => [],
22
+ :resource_id => nil,
23
+ :short_description => nil,
24
+ :description => nil,
25
+ :examples => [],
26
+ :see => [],
27
+ :formats => nil,
28
+ :api_versions => [],
29
+ :meta => nil,
30
+ :show => true,
31
+ :deprecated => false
32
+ }
33
+ end
34
+
35
+ let(:dsl_data) do
36
+ base_dsl_data.merge({
37
+ api_args: [
38
+ [
39
+ http_method,
40
+ path,
41
+ 'description',
42
+ { deprecated: true }
43
+ ]
44
+ ]
45
+ })
46
+ end
47
+
48
+ let(:resource_desc) do
49
+ Apipie::ResourceDescription.new(UsersController, 'dummy')
50
+ end
51
+
52
+ let(:method_desc) do
53
+ Apipie::MethodDescription.new(:show, resource_desc, dsl_data)
54
+ end
55
+
56
+ let(:param_description) do
57
+ Apipie::ParamDescription.new(
58
+ method_desc,
59
+ :param,
60
+ validator,
61
+ param_description_options
62
+ )
63
+ end
64
+
65
+ let(:name_definition) do
66
+ described_class.new(
67
+ param_description,
68
+ prefixed_by: prefixed_by
69
+ ).to_hash
70
+ end
71
+
72
+ subject { name_definition[:name] }
73
+
74
+ it { is_expected.to eq(param_description.name) }
75
+
76
+ context 'when prefixed by is given' do
77
+ let(:prefixed_by) { 'some-prefix' }
78
+
79
+ it { is_expected.to eq("#{prefixed_by}[#{param_description.name}]") }
80
+ end
81
+ end
@@ -0,0 +1,183 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apipie::Generator::Swagger::ParamDescription::Type do
4
+ let(:validator_options) { {} }
5
+ let(:param_description_options) { {}.merge(validator_options) }
6
+ let(:with_null) { false }
7
+ let(:http_method) { :GET }
8
+ let(:path) { '/api' }
9
+ let(:validator) { String }
10
+
11
+ let(:base_dsl_data) do
12
+ {
13
+ :api => false,
14
+ :api_args => [],
15
+ :api_from_routes => nil,
16
+ :errors => [],
17
+ :tag_list => [],
18
+ :returns => {},
19
+ :params => [],
20
+ :headers => [],
21
+ :resource_id => nil,
22
+ :short_description => nil,
23
+ :description => nil,
24
+ :examples => [],
25
+ :see => [],
26
+ :formats => nil,
27
+ :api_versions => [],
28
+ :meta => nil,
29
+ :show => true,
30
+ :deprecated => false
31
+ }
32
+ end
33
+
34
+ let(:dsl_data) do
35
+ base_dsl_data.merge({
36
+ api_args: [
37
+ [
38
+ http_method,
39
+ path,
40
+ 'description',
41
+ { deprecated: true }
42
+ ]
43
+ ]
44
+ })
45
+ end
46
+
47
+ let(:resource_desc) do
48
+ Apipie::ResourceDescription.new(UsersController, 'dummy')
49
+ end
50
+
51
+ let(:method_desc) do
52
+ Apipie::MethodDescription.new(:show, resource_desc, dsl_data)
53
+ end
54
+
55
+ let(:param_description_name) { 'some_param' }
56
+
57
+ let(:param_description) do
58
+ Apipie::ParamDescription.new(
59
+ method_desc,
60
+ param_description_name,
61
+ validator,
62
+ param_description_options
63
+ )
64
+ end
65
+
66
+ let(:type_definition) do
67
+ described_class.
68
+ new(param_description, with_null: with_null, controller_method: 'index').
69
+ to_hash
70
+ end
71
+
72
+ describe 'type' do
73
+ subject { type_definition[:type] }
74
+
75
+ it { is_expected.to eq(validator.to_s.downcase) }
76
+
77
+ context 'when validator is enum' do
78
+ let(:validator) { %w[name enum] }
79
+
80
+ it { is_expected.to eq('string') }
81
+ end
82
+
83
+ context 'when validator is a Hash' do
84
+ let(:validator) { Hash }
85
+
86
+ it { is_expected.to eq('object') }
87
+ end
88
+ end
89
+
90
+ describe 'items' do
91
+ let(:items) { type_definition[:items] }
92
+
93
+ subject { items }
94
+
95
+ context 'when has validator is Array' do
96
+ let(:validator) { Array }
97
+
98
+ it { is_expected.to eq({ type: 'string' }) }
99
+
100
+ context 'of Hash' do
101
+ let(:validator_options) { { of: Hash } }
102
+
103
+ let(:reference) do
104
+ Apipie::Generator::Swagger::OperationId.
105
+ new(http_method: http_method, path: path, param: param_description_name).
106
+ to_s
107
+ end
108
+
109
+ it { is_expected.to eq({ type: 'string' }) }
110
+
111
+ context 'and swagger.json_input_uses_refs is set to true' do
112
+ before { Apipie.configuration.generator.swagger.json_input_uses_refs = true }
113
+ after { Apipie.configuration.generator.swagger.json_input_uses_refs = false }
114
+
115
+ it 'returns the reference' do
116
+ expect(subject).to eq({ '$ref' => reference })
117
+ end
118
+ end
119
+ end
120
+
121
+ context 'of a Swagger type' do
122
+ let(:validator_options) { { of: Integer } }
123
+
124
+ it { is_expected.to eq({ type: 'integer' }) }
125
+ end
126
+
127
+ describe 'enum' do
128
+ subject { items[:enum] }
129
+
130
+ it { is_expected.to be_blank }
131
+
132
+ context 'when validator is Array' do
133
+ let(:validator) { Array }
134
+
135
+ it { is_expected.to be_blank }
136
+
137
+ context 'and an array of in values is given' do
138
+ let(:enum_values) { %w[enum-value-1 enum-value-2] }
139
+ let(:validator_options) { { in: %w[enum-value-1 enum-value-2] } }
140
+
141
+ it { is_expected.to eq(enum_values) }
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
147
+
148
+ describe 'enum' do
149
+ subject { type_definition[:enum] }
150
+
151
+ context 'and an array of in values is given' do
152
+ let(:validator) { %w[enum-value-1 enum-value-2] }
153
+
154
+ it { is_expected.to eq(validator) }
155
+ end
156
+ end
157
+
158
+ describe 'additionalProperties' do
159
+ subject { type_definition[:additionalProperties] }
160
+
161
+ it { is_expected.to be_blank }
162
+
163
+ context 'when validator is a Hash' do
164
+ let(:validator) { Hash }
165
+
166
+ it { is_expected.to be_truthy }
167
+ end
168
+ end
169
+
170
+ describe 'warnings' do
171
+ before { Singleton.__init__(Apipie::Generator::Swagger::WarningWriter) }
172
+
173
+ subject { type_definition }
174
+
175
+ context 'when validator is a Hash' do
176
+ let(:validator) { Hash }
177
+
178
+ it 'outputs a hash without internal typespec warning' do
179
+ expect { subject }.to output(/is a generic Hash without an internal type specification/).to_stderr
180
+ end
181
+ end
182
+ end
183
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apipie::Generator::Swagger::ParamDescription do
4
+ describe '.create_for_missing_param' do
5
+ let(:name) { 'ok' }
6
+
7
+ let(:method_description) do
8
+ Apipie.get_method_description(UsersController, :update)
9
+ end
10
+
11
+ subject do
12
+ described_class.create_for_missing_param(method_description, name)
13
+ end
14
+
15
+ it 'creates a required param description' do
16
+ expect(subject.required).to eq(true)
17
+ end
18
+
19
+ it 'has the correct name' do
20
+ expect(subject.name).to eq(name)
21
+ end
22
+
23
+ it 'has been created from path' do
24
+ expect(subject.options[:added_from_path]).to eq(true)
25
+ end
26
+ end
27
+ end
28
+
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apipie::Generator::Swagger::PathDecorator do
4
+ let(:decorated_path) { described_class.new(path) }
5
+
6
+ describe '#param_names' do
7
+ subject { decorated_path.param_names }
8
+
9
+ context 'when path does not contain any params' do
10
+ let(:path) { 'some/path/without_params' }
11
+
12
+ it { is_expected.to be_empty }
13
+ end
14
+
15
+ context 'when path has a param' do
16
+ let(:path) { '/:resource_id/custom' }
17
+
18
+ it { is_expected.to eq([:resource_id]) }
19
+ end
20
+ end
21
+
22
+ describe '#param?' do
23
+ subject { decorated_path.param?(param) }
24
+
25
+ context 'when path has params' do
26
+ let(:path) { '/:resource_id/custom' }
27
+
28
+ context 'when param to check is in the path' do
29
+ let(:param) { :resource_id }
30
+
31
+ it { is_expected.to be(true) }
32
+ end
33
+
34
+ context 'when param to check is not in the path' do
35
+ let(:param) { :some_param }
36
+
37
+ it { is_expected.to be(false) }
38
+ end
39
+ end
40
+ end
41
+
42
+ describe '#swagger_path' do
43
+ subject(:swagger_path) { decorated_path.swagger_path }
44
+
45
+ let(:path) { '/some/custom/path' }
46
+
47
+ it { is_expected.to eq('/some/custom/path') }
48
+
49
+ context 'when path does not start with slash' do
50
+ let(:path) { ':resource_id/custom' }
51
+
52
+ it 'adds the slash in the beginning' do
53
+ expect(swagger_path).to eq('/{resource_id}/custom')
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apipie::Generator::Swagger::ReferencedDefinitions do
4
+ let(:definitions) { described_class.instance }
5
+
6
+ before { Singleton.__init__(described_class) }
7
+
8
+ describe '.add!' do
9
+ subject(:add_call) { definitions.add!(param_type, schema) }
10
+
11
+ let(:param_type) { :some_type }
12
+ let(:schema) { { key: 'value' } }
13
+
14
+ it 'returns add the definition' do
15
+ expect { add_call }
16
+ .to change(definitions, :definitions)
17
+ .from({})
18
+ .to({ param_type => schema })
19
+ end
20
+ end
21
+
22
+ describe '.added?' do
23
+ subject { definitions.added?(param_type) }
24
+
25
+ let(:param_type) { :some_type }
26
+
27
+ it { is_expected.to be_blank }
28
+
29
+ context 'when definition exists' do
30
+ before { definitions.add!(param_type, {}) }
31
+
32
+ it { is_expected.to be(true) }
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apipie::Generator::Swagger::ResourceDescriptionComposite do
4
+ let(:dsl_data) { {} }
5
+ let(:resource_id) { 'pets' }
6
+ let(:resource_description) do
7
+ Apipie::ResourceDescription.new(PetsController, resource_id, dsl_data)
8
+ end
9
+
10
+ let(:resource_descriptions) { [resource_description] }
11
+ let(:composite) { described_class.new(resource_descriptions, language: :en) }
12
+
13
+ describe '#to-swagger' do
14
+ subject { composite.to_swagger }
15
+
16
+ it { is_expected.to include(:paths, :tags) }
17
+
18
+ describe 'tags' do
19
+ subject(:tag) { composite.to_swagger[:tags].first }
20
+
21
+ it { is_expected.to be_blank }
22
+
23
+ context 'when resource description has full description' do
24
+ let(:dsl_data) { { description: 'something' } }
25
+
26
+ it 'returns the name and description' do
27
+ expect(tag).to eq(
28
+ {
29
+ name: resource_id,
30
+ description: resource_description._full_description
31
+ }
32
+ )
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apipie::Generator::Swagger::ResourceDescriptionsCollection do
4
+ let(:collection) { described_class.new(resource_descriptions) }
5
+
6
+ let(:users_resource_description) do
7
+ Apipie.get_resource_description(UsersController, Apipie.configuration.default_version)
8
+ end
9
+
10
+ let(:pets_resource_description) do
11
+ Apipie.get_resource_description(PetsController, Apipie.configuration.default_version)
12
+ end
13
+
14
+ let(:resource_descriptions) do
15
+ {
16
+ 'development' => {
17
+ users_resource_description._id => users_resource_description,
18
+ pets_resource_description._id => pets_resource_description
19
+ },
20
+ 'production' => {
21
+ pets_resource_description._id => pets_resource_description
22
+ }
23
+ }
24
+ end
25
+
26
+ describe '#filter' do
27
+ subject(:filtered_resource_descriptions) do
28
+ collection.filter(
29
+ version: version,
30
+ resource_id: resource_id,
31
+ method_name: method_name
32
+ )
33
+ end
34
+
35
+ let(:version) { 'development' }
36
+ let(:resource_id) { users_resource_description._id }
37
+ let(:method_name) { nil }
38
+
39
+ it 'filters resources for the given version and resource name' do
40
+ expect(filtered_resource_descriptions).to eq([users_resource_description])
41
+ end
42
+
43
+ context 'when method name is given' do
44
+ context 'when does not exist in the controller' do
45
+ let(:method_name) { :i_do_not_exist }
46
+
47
+ it { is_expected.to be_empty }
48
+ end
49
+
50
+ context 'when exists in the controller' do
51
+ let(:method_name) { :show }
52
+
53
+ it { is_expected.to eq([users_resource_description]) }
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,89 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apipie::Generator::Swagger::Schema do
4
+ let(:version) { :ok }
5
+ let(:language) { :ok }
6
+ let(:clear_warnings) { :ok }
7
+ let(:dsl_data) { {} }
8
+ let(:resource_id) { 'pets' }
9
+ let(:resource_descriptions) { [resource_description] }
10
+
11
+ let(:resource_description) do
12
+ Apipie::ResourceDescription.new(PetsController, resource_id, dsl_data)
13
+ end
14
+
15
+ let(:schema_generator) do
16
+ described_class.new(
17
+ resource_descriptions,
18
+ version: version,
19
+ language: language,
20
+ clear_warnings: clear_warnings
21
+ )
22
+ end
23
+
24
+ describe '#generate' do
25
+ subject(:schema) { schema_generator.generate }
26
+
27
+ describe 'host' do
28
+ before { Apipie.configuration.generator.swagger.api_host = nil }
29
+
30
+ it 'is not returned' do
31
+ expect(schema.keys).not_to include(:host)
32
+ end
33
+
34
+ context 'when api_host is set' do
35
+ let(:host) { 'localhost:3000' }
36
+
37
+ before { Apipie.configuration.generator.swagger.api_host = host }
38
+
39
+ it 'returns the host' do
40
+ expect(schema[:host]).to eq(host)
41
+ end
42
+ end
43
+ end
44
+
45
+ describe 'consumes' do
46
+ subject { schema_generator.generate[:consumes] }
47
+
48
+ before { Apipie.configuration.generator.swagger.content_type_input = nil }
49
+
50
+ it { is_expected.to eq(['application/x-www-form-urlencoded', 'multipart/form-data']) }
51
+
52
+ context 'when swagger.content_type_input is set to json' do
53
+ before { Apipie.configuration.generator.swagger.content_type_input = :json }
54
+
55
+ it { is_expected.to eq(['application/json']) }
56
+ end
57
+ end
58
+
59
+ describe 'title' do
60
+ subject { schema_generator.generate[:info][:title] }
61
+
62
+ before { Apipie.configuration.generator.swagger.content_type_input = nil }
63
+
64
+ it { is_expected.to include(' (params in:formData)') }
65
+
66
+ context 'when swagger.content_type_input is set to json' do
67
+ before { Apipie.configuration.generator.swagger.content_type_input = :json }
68
+
69
+ it { is_expected.to include(' (params in:body)') }
70
+ end
71
+ end
72
+
73
+ describe 'x-computed-id' do
74
+ subject(:schema) { schema_generator.generate[:info] }
75
+
76
+ before { Apipie.configuration.generator.swagger.generate_x_computed_id_field = false }
77
+
78
+ it 'is not returned' do
79
+ expect(schema.keys).not_to include('x-computed-id')
80
+ end
81
+
82
+ context 'when swagger.content_type_input is set to json' do
83
+ before { Apipie.configuration.generator.swagger.generate_x_computed_id_field = true }
84
+
85
+ it { is_expected.to include('x-computed-id') }
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apipie::Generator::Swagger::TypeExtractor do
4
+ let(:extractor) { described_class.new(validator) }
5
+
6
+ shared_examples 'extractable method' do
7
+ let(:warnings) { {} }
8
+ let(:validator) {}
9
+
10
+ it { is_expected.to eq(Apipie::Generator::Swagger::TypeExtractor::TYPES[:string]) }
11
+
12
+ context "when enum validator is used" do
13
+ let(:enum_values) { ["Name"] }
14
+
15
+ context "of type Apipie::Validator::EnumValidator" do
16
+ let(:validator) { Apipie::Validator::EnumValidator.new(nil, enum_values) }
17
+
18
+ it { is_expected.to eq("enum") }
19
+ end
20
+
21
+ context "that responds to is_enum?" do
22
+ let(:validator) do
23
+ Apipie::ResponseDescriptionAdapter::PropDesc::Validator.new('some-type', enum_values)
24
+ end
25
+
26
+ it 'returns an enum type' do
27
+ expect(subject).to eq(Apipie::Generator::Swagger::TypeExtractor::TYPES[:enum])
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ describe '#extract' do
34
+ subject { extractor.extract }
35
+
36
+ it_behaves_like 'extractable method'
37
+ end
38
+ end