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,119 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apipie::Generator::Swagger::MethodDescription::ApiSchemaService do
4
+ let(:path) { '/api' }
5
+ let(:http_method) { 'get' }
6
+ let(:resource_id) { 'users' }
7
+ let(:method_description_description) { nil }
8
+ let(:tags) { [] }
9
+
10
+ let(:dsl_data) do
11
+ ActionController::Base
12
+ .send(:_apipie_dsl_data_init)
13
+ .merge(
14
+ {
15
+ description: method_description_description,
16
+ api_args: [[http_method, path, 'Some api description', { deprecated: true }]],
17
+ tag_list: tags
18
+ }
19
+ )
20
+ end
21
+
22
+ let(:resource_desc) do
23
+ Apipie::ResourceDescription.new(UsersController, resource_id)
24
+ end
25
+
26
+ let(:method_description) do
27
+ Apipie::Generator::Swagger::MethodDescription::Decorator.new(
28
+ Apipie::MethodDescription.new(:show, resource_desc, dsl_data)
29
+ )
30
+ end
31
+
32
+ let(:service) { described_class.new(method_description) }
33
+
34
+ describe '#call' do
35
+ subject(:schema) { service.call }
36
+
37
+ it 'returns the path' do
38
+ expect(schema).to include(path)
39
+ end
40
+
41
+ it 'returns the http method' do
42
+ expect(schema[path]).to include(http_method)
43
+ end
44
+
45
+ it 'returns the correct attributes' do
46
+ expect(schema[path][http_method].keys).to include(
47
+ :tags,
48
+ :consumes,
49
+ :operationId,
50
+ :parameters,
51
+ :responses
52
+ )
53
+ end
54
+ end
55
+
56
+ describe 'tags' do
57
+ subject { service.call[path][http_method][:tags] }
58
+
59
+ it { is_expected.to eq([resource_id]) }
60
+
61
+ context 'when tags are available' do
62
+ let(:tags) { ['Tag 1', 'Tag 2'] }
63
+
64
+ it { is_expected.to include(*tags) }
65
+ end
66
+
67
+ context 'when Apipie.configuration.generator.swagger.skip_default_tags is enabled' do
68
+ before { Apipie.configuration.generator.swagger.skip_default_tags = true }
69
+ after { Apipie.configuration.generator.swagger.skip_default_tags = false }
70
+
71
+ it { is_expected.to be_empty }
72
+
73
+ context 'when tags are available' do
74
+ let(:tags) { ['Tag 1', 'Tag 2'] }
75
+
76
+ it { is_expected.to eq(tags) }
77
+ end
78
+ end
79
+
80
+ context 'when Apipie.configuration.generator.swagger.include_warning_tags is enabled' do
81
+ before { Apipie.configuration.generator.swagger.include_warning_tags = true }
82
+
83
+ context 'when warnings are issued' do
84
+ before do
85
+ Apipie::Generator::Swagger::Warning.for_code(
86
+ Apipie::Generator::Swagger::Warning::MISSING_METHOD_SUMMARY_CODE,
87
+ 'some_method'
88
+ ).warn_through_writer
89
+ end
90
+
91
+ it { is_expected.to include('warnings issued') }
92
+ end
93
+ end
94
+ end
95
+
96
+ describe 'consumes' do
97
+ subject { service.call[path]['get'][:consumes] }
98
+
99
+ it { is_expected.to eq(['application/x-www-form-urlencoded', 'multipart/form-data']) }
100
+
101
+ context 'when content type input is json' do
102
+ before { Apipie.configuration.generator.swagger.content_type_input = :json }
103
+
104
+ it { is_expected.to eq(['application/json']) }
105
+ end
106
+ end
107
+
108
+ describe 'description' do
109
+ subject { service.call[path]['get'][:description] }
110
+
111
+ it { is_expected.to be_blank }
112
+
113
+ context 'when description for method description exists' do
114
+ let(:method_description_description) { 'Some description' }
115
+
116
+ it { is_expected.to eq(method_description_description) }
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,105 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apipie::Generator::Swagger::MethodDescription::ResponseSchemaService do
4
+ let(:http_method) { nil }
5
+ let(:controller_method) { nil }
6
+ let(:allow_null) { false }
7
+
8
+ let(:resource_description) do
9
+ Apipie::ResourceDescription.new(PetsController, 'pets')
10
+ end
11
+
12
+ let(:method_description) do
13
+ Apipie::MethodDescription.new(
14
+ 'create',
15
+ resource_description,
16
+ ActionController::Base.send(:_apipie_dsl_data_init)
17
+ )
18
+ end
19
+
20
+ let(:response_description_dsl) do
21
+ proc do
22
+ property :a_number, Integer, example: 1
23
+ property :an_optional_number, Integer, required: false, example: 2
24
+ end
25
+ end
26
+
27
+ let(:options) { {} }
28
+
29
+ let(:response_description) do
30
+ Apipie::ResponseDescription.new(
31
+ method_description,
32
+ 204,
33
+ options,
34
+ PetsController,
35
+ response_description_dsl,
36
+ nil
37
+ )
38
+ end
39
+
40
+ let(:service) do
41
+ described_class.new(
42
+ response_description,
43
+ allow_null: allow_null,
44
+ http_method: http_method,
45
+ controller_method: controller_method
46
+ )
47
+ end
48
+
49
+ describe '#to_swagger' do
50
+ let(:swagger_response) { service.to_swagger }
51
+
52
+ describe 'properties' do
53
+ subject(:properties) { swagger_response[:properties] }
54
+
55
+ it 'returns correct properties' do
56
+ expect(properties).to eq(
57
+ {
58
+ a_number: {
59
+ type: 'number', required: true, example: 1
60
+ },
61
+ an_optional_number: {
62
+ type: 'number', example: 2
63
+ }
64
+ }
65
+ )
66
+ end
67
+
68
+ context 'when nulls are allowed' do
69
+ let(:allow_null) { true }
70
+
71
+ it 'returns correct properties' do
72
+ expect(properties).to eq(
73
+ {
74
+ a_number: {
75
+ type: %w[number null], required: true, example: 1
76
+ },
77
+ an_optional_number: {
78
+ type: %w[number null], example: 2
79
+ }
80
+ }
81
+ )
82
+ end
83
+ end
84
+ end
85
+
86
+ context 'when responses_use_refs is set to true' do
87
+ subject(:response) { swagger_response }
88
+
89
+ before { Apipie.configuration.generator.swagger.responses_use_refs = true }
90
+ after { Apipie.configuration.generator.swagger.responses_use_refs = false }
91
+
92
+ context 'when typename is given' do
93
+ let(:options) { { param_group: :some_param_group } }
94
+
95
+ it 'returns the reference' do
96
+ expect(response).to eq(
97
+ {
98
+ '$ref' => '#/definitions/some_param_group'
99
+ }
100
+ )
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apipie::Generator::Swagger::OperationId do
4
+ let(:path) { '/api' }
5
+ let(:http_method) { :GET }
6
+ let(:param) {}
7
+
8
+ let(:operation_id) do
9
+ described_class.new(path: path, http_method: http_method, param: param)
10
+ end
11
+
12
+ describe '#to_s' do
13
+ subject { operation_id.to_s}
14
+
15
+ it { is_expected.to eq('get_api') }
16
+
17
+ context 'when path has variable' do
18
+ let(:path) { '/api/users/:id' }
19
+
20
+ it { is_expected.to eq("#{http_method.downcase}_api_users_id") }
21
+ end
22
+
23
+ context 'when param is given' do
24
+ let(:param) { 'show' }
25
+
26
+ it { is_expected.to eq("#{http_method.downcase}_api_param_show") }
27
+ end
28
+ end
29
+
30
+ describe '.from' do
31
+ subject { described_class.from(describable).to_s }
32
+
33
+ context 'when an Apipie::MethodDescription::Api is given' do
34
+ let(:describable) do
35
+ Apipie::MethodDescription::Api.
36
+ new(http_method, path, '', { from_routes: '' })
37
+ end
38
+
39
+ it { is_expected.to eq("#{http_method.downcase}_api") }
40
+ end
41
+
42
+ context 'when an Apipie::MethodDescription is given' do
43
+ let(:dsl_data) do
44
+ ActionController::Base.
45
+ send(:_apipie_dsl_data_init).
46
+ merge({
47
+ api_args: [[http_method, path, "description", { :deprecated => true }]]
48
+ })
49
+ end
50
+
51
+ let(:resource_desc) do
52
+ Apipie::ResourceDescription.new(UsersController, "users")
53
+ end
54
+
55
+ let(:describable) do
56
+ Apipie::MethodDescription.new(:show, resource_desc, dsl_data)
57
+ end
58
+
59
+ it { is_expected.to eq("#{http_method.downcase}_api") }
60
+ end
61
+ end
62
+ end
63
+
@@ -0,0 +1,215 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apipie::Generator::Swagger::ParamDescription::Builder do
4
+ let(:validator) { String }
5
+ let(:validator_options) { {} }
6
+ let(:path) { '/api' }
7
+ let(:http_method) { :GET }
8
+ let(:in_schema) { true }
9
+ let(:builder) do
10
+ described_class.new(
11
+ param_description,
12
+ in_schema: in_schema,
13
+ controller_method: method_desc
14
+ )
15
+ end
16
+ let(:generated_block) { builder.to_swagger }
17
+ let(:base_param_description_options) { {} }
18
+
19
+ let(:param_description_options) do
20
+ base_param_description_options.merge(validator_options)
21
+ end
22
+
23
+ let(:base_dsl_data) do
24
+ {
25
+ :api => false,
26
+ :api_args => [],
27
+ :api_from_routes => nil,
28
+ :errors => [],
29
+ :tag_list => [],
30
+ :returns => {},
31
+ :params => [],
32
+ :headers => [],
33
+ :resource_id => nil,
34
+ :short_description => nil,
35
+ :description => nil,
36
+ :examples => [],
37
+ :see => [],
38
+ :formats => nil,
39
+ :api_versions => [],
40
+ :meta => nil,
41
+ :show => true,
42
+ :deprecated => false
43
+ }
44
+ end
45
+
46
+ let(:dsl_data) do
47
+ base_dsl_data.merge({
48
+ api_args: [
49
+ [
50
+ http_method,
51
+ path,
52
+ 'description',
53
+ { deprecated: true }
54
+ ]
55
+ ]
56
+ })
57
+ end
58
+
59
+ let(:resource_desc) do
60
+ Apipie::ResourceDescription.new(UsersController, 'dummy')
61
+ end
62
+
63
+ let(:method_desc) do
64
+ Apipie::MethodDescription.new(:show, resource_desc, dsl_data)
65
+ end
66
+
67
+ let(:param_description) do
68
+ Apipie::ParamDescription.new(
69
+ method_desc,
70
+ :param,
71
+ validator,
72
+ param_description_options
73
+ )
74
+ end
75
+
76
+ describe 'required' do
77
+ subject { generated_block[:required] }
78
+
79
+ it { is_expected.to be_blank }
80
+
81
+ context 'when is required' do
82
+ let(:base_param_description_options) { { required: true } }
83
+
84
+ it { is_expected.to eq(true) }
85
+ end
86
+
87
+ context 'when in_schema is false' do
88
+ let(:in_schema) { false }
89
+
90
+ it { is_expected.to be_blank }
91
+
92
+ context 'and is required' do
93
+ let(:base_param_description_options) { { required: true } }
94
+
95
+ it { is_expected.to eq(true) }
96
+ end
97
+ end
98
+ end
99
+
100
+ describe 'warning' do
101
+ before { Singleton.__init__(Apipie::Generator::Swagger::WarningWriter) }
102
+
103
+ subject { generated_block }
104
+
105
+ context 'when required is true' do
106
+ let(:base_param_description_options) { { required: true } }
107
+
108
+ it 'does not output an option without default warning' do
109
+ expect { subject }.not_to output(
110
+ /is optional but default value is not specified/
111
+ ).to_stderr
112
+ end
113
+ end
114
+
115
+ context 'when required is false' do
116
+ context 'when default_value is nil' do
117
+ let(:base_param_description_options) do
118
+ { required: false, default_value: nil }
119
+ end
120
+
121
+ it 'will not warn' do
122
+ expect { subject }.not_to output(
123
+ /is optional but default value is not specified/
124
+ ).to_stderr
125
+ end
126
+ end
127
+
128
+ context 'when default_value is 123' do
129
+ let(:base_param_description_options) do
130
+ { required: false, default_value: 123 }
131
+ end
132
+
133
+ it 'will not warn' do
134
+ expect { subject }.not_to output(
135
+ /is optional but default value is not specified/
136
+ ).to_stderr
137
+ end
138
+ end
139
+
140
+ context 'default_value not given' do
141
+ let(:base_param_description_options) { { required: false } }
142
+
143
+ it 'warns' do
144
+ expect { subject }.to output(
145
+ /is optional but default value is not specified/
146
+ ).to_stderr
147
+ end
148
+ end
149
+ end
150
+ end
151
+
152
+ describe '.with_type' do
153
+ subject { generated_block[:type] }
154
+
155
+ it { is_expected.to be_blank }
156
+
157
+ context 'when type is assigned' do
158
+ before { builder.with_type(with_null: false ) }
159
+
160
+ it { is_expected.to be_present }
161
+ end
162
+ end
163
+
164
+ describe '.with_description' do
165
+ subject { generated_block[:description] }
166
+
167
+ it { is_expected.to be_blank }
168
+
169
+ context 'when description is assigned' do
170
+ let(:param_description_options) { { desc: 'some-description' } }
171
+
172
+ before { builder.with_description(language: 'en') }
173
+
174
+ it { is_expected.to be_present }
175
+ end
176
+ end
177
+
178
+ describe '.with_name' do
179
+ subject { generated_block[:name] }
180
+
181
+ it { is_expected.to be_blank }
182
+
183
+ context 'when name is assigned' do
184
+ before { builder.with_name }
185
+
186
+ it { is_expected.to be_present }
187
+ end
188
+ end
189
+
190
+ describe '.with_in' do
191
+ subject { generated_block[:in] }
192
+
193
+ it { is_expected.to be_blank }
194
+
195
+ context 'when in is assigned' do
196
+ let(:in_schema) { false }
197
+
198
+ before { builder.with_in(http_method: 'get') }
199
+
200
+ it { is_expected.to be_present }
201
+ end
202
+ end
203
+
204
+ describe 'example' do
205
+ subject { generated_block[:example] }
206
+
207
+ it { is_expected.to be_blank }
208
+
209
+ context 'when example is assigned' do
210
+ let(:base_param_description_options) { { example: 'example' } }
211
+
212
+ it { is_expected.to eq('example') }
213
+ end
214
+ end
215
+ end
@@ -0,0 +1,95 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apipie::Generator::Swagger::ParamDescription::Composite do
4
+ let(:param_descriptions) {}
5
+
6
+ let(:dsl_data) { ActionController::Base.send(:_apipie_dsl_data_init) }
7
+
8
+ let(:resource_desc) do
9
+ Apipie::ResourceDescription.new(UsersController, "users")
10
+ end
11
+
12
+ let(:context) do
13
+ Apipie::Generator::Swagger::Context.new(
14
+ allow_null: true,
15
+ http_method: 'get',
16
+ controller_method: method_description
17
+ )
18
+ end
19
+
20
+ let(:method_description) do
21
+ Apipie::MethodDescription.new(:create, resource_desc, dsl_data)
22
+ end
23
+
24
+ let(:composite) { described_class.new(param_descriptions, context) }
25
+
26
+ let(:swagger) { composite.to_swagger }
27
+
28
+ let(:params_description_one) do
29
+ Apipie::ParamDescription.new(method_description, :some_param, String)
30
+ end
31
+
32
+ let(:params_description_two) do
33
+ Apipie::ParamDescription.new(method_description, :some_other_param, String)
34
+ end
35
+
36
+ let(:param_descriptions) { [params_description_one, params_description_two] }
37
+
38
+ context 'when no param descriptions are given' do
39
+ let(:param_descriptions) { [] }
40
+
41
+ subject { swagger }
42
+
43
+ it { is_expected.to be_blank }
44
+ end
45
+
46
+ describe 'additionalProperties' do
47
+ subject { swagger[:additionalProperties] }
48
+
49
+ it { is_expected.to be_falsey }
50
+
51
+ context 'when additional properties in response allowed' do
52
+ before do
53
+ Apipie.configuration.generator.swagger.allow_additional_properties_in_response = true
54
+ end
55
+
56
+ it { is_expected.to be_blank }
57
+ end
58
+ end
59
+
60
+ xdescribe 'nested additionalProperties' do
61
+ context 'when param description has nested params' do
62
+ let(:validator) do
63
+
64
+ end
65
+
66
+ let(:params_description_one) do
67
+ Apipie::ParamDescription.new(
68
+ method_description,
69
+ :some_param,
70
+ validator,
71
+ { required: true }
72
+ )
73
+ end
74
+ end
75
+ end
76
+
77
+ describe 'required' do
78
+ subject { swagger[:required] }
79
+
80
+ it { is_expected.to be_blank }
81
+
82
+ context 'when param description is required' do
83
+ let(:params_description_one) do
84
+ Apipie::ParamDescription.new(
85
+ method_description,
86
+ :some_param,
87
+ String,
88
+ { required: true }
89
+ )
90
+ end
91
+
92
+ it { is_expected.to be_truthy }
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apipie::Generator::Swagger::ParamDescription::Description do
4
+ let(:param_description_options) { {} }
5
+ let(:http_method) { :GET }
6
+ let(:path) { '/api' }
7
+ let(:validator) { String }
8
+ let(:language) {}
9
+
10
+ let(:base_dsl_data) do
11
+ {
12
+ :api => false,
13
+ :api_args => [],
14
+ :api_from_routes => nil,
15
+ :errors => [],
16
+ :tag_list => [],
17
+ :returns => {},
18
+ :params => [],
19
+ :headers => [],
20
+ :resource_id => nil,
21
+ :short_description => nil,
22
+ :description => nil,
23
+ :examples => [],
24
+ :see => [],
25
+ :formats => nil,
26
+ :api_versions => [],
27
+ :meta => nil,
28
+ :show => true,
29
+ :deprecated => false
30
+ }
31
+ end
32
+
33
+ let(:dsl_data) do
34
+ base_dsl_data.merge({
35
+ api_args: [
36
+ [
37
+ http_method,
38
+ path,
39
+ 'description',
40
+ { deprecated: true }
41
+ ]
42
+ ]
43
+ })
44
+ end
45
+
46
+ let(:resource_desc) do
47
+ Apipie::ResourceDescription.new(UsersController, 'dummy')
48
+ end
49
+
50
+ let(:method_desc) do
51
+ Apipie::MethodDescription.new(:show, resource_desc, dsl_data)
52
+ end
53
+
54
+ let(:param_description) do
55
+ Apipie::ParamDescription.new(
56
+ method_desc,
57
+ :param,
58
+ validator,
59
+ param_description_options
60
+ )
61
+ end
62
+
63
+ let(:description_definition) do
64
+ described_class.new(param_description, language: language).to_hash
65
+ end
66
+
67
+ describe 'description' do
68
+ subject { description_definition[:description] }
69
+
70
+ it { is_expected.to be_blank }
71
+
72
+ context 'when desc is given to options' do
73
+ let(:desc) { 'Some description' }
74
+ let(:param_description_options) { { desc: desc } }
75
+
76
+ it { is_expected.to eq(desc) }
77
+ end
78
+ end
79
+ end