apipie-rails 0.9.1 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +75 -0
  3. data/.rubocop_todo.yml +780 -201
  4. data/CHANGELOG.md +8 -0
  5. data/app/controllers/apipie/apipies_controller.rb +5 -6
  6. data/lib/apipie/apipie_module.rb +2 -2
  7. data/lib/apipie/application.rb +6 -6
  8. data/lib/apipie/configuration.rb +11 -11
  9. data/lib/apipie/dsl_definition.rb +11 -10
  10. data/lib/apipie/error_description.rb +2 -2
  11. data/lib/apipie/extractor/writer.rb +11 -11
  12. data/lib/apipie/generator/swagger/context.rb +27 -0
  13. data/lib/apipie/generator/swagger/operation_id.rb +51 -0
  14. data/lib/apipie/generator/swagger/param_description/builder.rb +105 -0
  15. data/lib/apipie/generator/swagger/param_description/composite.rb +111 -0
  16. data/lib/apipie/generator/swagger/param_description/description.rb +15 -0
  17. data/lib/apipie/generator/swagger/param_description/in.rb +37 -0
  18. data/lib/apipie/generator/swagger/param_description/name.rb +18 -0
  19. data/lib/apipie/generator/swagger/param_description/type.rb +108 -0
  20. data/lib/apipie/generator/swagger/param_description.rb +18 -0
  21. data/lib/apipie/generator/swagger/type_extractor.rb +2 -2
  22. data/lib/apipie/method_description.rb +4 -5
  23. data/lib/apipie/param_description.rb +7 -7
  24. data/lib/apipie/resource_description.rb +3 -3
  25. data/lib/apipie/response_description.rb +2 -2
  26. data/lib/apipie/response_description_adapter.rb +7 -5
  27. data/lib/apipie/swagger_generator.rb +54 -202
  28. data/lib/apipie/validator.rb +3 -3
  29. data/lib/apipie/version.rb +1 -1
  30. data/lib/apipie-rails.rb +9 -0
  31. data/lib/tasks/apipie.rake +5 -6
  32. data/spec/controllers/users_controller_spec.rb +3 -2
  33. data/spec/lib/generator/swagger/context_spec.rb +35 -0
  34. data/spec/lib/generator/swagger/operation_id_spec.rb +63 -0
  35. data/spec/lib/generator/swagger/param_description/builder_spec.rb +163 -0
  36. data/spec/lib/generator/swagger/param_description/composite_spec.rb +95 -0
  37. data/spec/lib/generator/swagger/param_description/description_spec.rb +79 -0
  38. data/spec/lib/generator/swagger/param_description/in_spec.rb +86 -0
  39. data/spec/lib/generator/swagger/param_description/name_spec.rb +81 -0
  40. data/spec/lib/generator/swagger/param_description/type_spec.rb +178 -0
  41. data/spec/lib/generator/swagger/param_description_spec.rb +28 -0
  42. data/spec/lib/generator/swagger/type_extractor_spec.rb +38 -18
  43. data/spec/lib/param_group_spec.rb +5 -5
  44. metadata +20 -2
@@ -0,0 +1,178 @@
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) { ['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.swagger_json_input_uses_refs = true }
113
+ after { Apipie.configuration.swagger_json_input_uses_refs = false }
114
+
115
+ it 'returns the reference' do
116
+ expect(subject).to eq({ '$ref' => reference })
117
+ end
118
+
119
+ end
120
+ end
121
+
122
+ describe 'enum' do
123
+ subject { items[:enum] }
124
+
125
+ it { is_expected.to be_blank }
126
+
127
+ context 'when validator is Array' do
128
+ let(:validator) { Array }
129
+
130
+ it { is_expected.to be_blank }
131
+
132
+ context 'and an array of in values is given' do
133
+ let(:enum_values) { ['enum-value-1', 'enum-value-2'] }
134
+ let(:validator_options) { { in: ['enum-value-1', 'enum-value-2'] } }
135
+
136
+ it { is_expected.to eq(enum_values) }
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
142
+
143
+ describe 'enum' do
144
+ subject { type_definition[:enum] }
145
+
146
+ context 'and an array of in values is given' do
147
+ let(:validator) { ['enum-value-1', 'enum-value-2'] }
148
+
149
+ it { is_expected.to eq(validator) }
150
+ end
151
+ end
152
+
153
+ describe 'additionalProperties' do
154
+ subject { type_definition[:additionalProperties] }
155
+
156
+ it { is_expected.to be_blank }
157
+
158
+ context 'when validator is a Hash' do
159
+ let(:validator) { Hash }
160
+
161
+ it { is_expected.to be_truthy }
162
+ end
163
+ end
164
+
165
+ describe 'warnings' do
166
+ before { Singleton.__init__(Apipie::Generator::Swagger::WarningWriter) }
167
+
168
+ subject { type_definition }
169
+
170
+ context 'when validator is a Hash' do
171
+ let(:validator) { Hash }
172
+
173
+ it 'outputs a hash without internal typespec warning' do
174
+ expect { subject }.to output(/is a generic Hash without an internal type specification/).to_stderr
175
+ end
176
+ end
177
+ end
178
+ 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
+
@@ -1,15 +1,11 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Apipie::Generator::Swagger::TypeExtractor do
4
- let(:validator) {}
5
4
  let(:extractor) { described_class.new(validator) }
6
5
 
7
- describe '#extarct_with_warnings' do
6
+ shared_examples 'extractable method' do
8
7
  let(:warnings) { {} }
9
-
10
- before { Apipie.configuration.swagger_suppress_warnings = false }
11
-
12
- subject { extractor.extract_with_warnings(warnings) }
8
+ let(:validator) {}
13
9
 
14
10
  it { is_expected.to eq(Apipie::Generator::Swagger::TypeExtractor::TYPES[:string]) }
15
11
 
@@ -38,21 +34,45 @@ describe Apipie::Generator::Swagger::TypeExtractor do
38
34
  it 'returns a boolean type' do
39
35
  expect(subject).to eq(Apipie::Generator::Swagger::TypeExtractor::TYPES[:boolean])
40
36
  end
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ describe '#extract' do
43
+ subject { extractor.extract }
41
44
 
42
- context 'and a boolean warning is passed' do
43
- let(:boolean_warning) do
44
- Apipie::Generator::Swagger::Warning.for_code(
45
- Apipie::Generator::Swagger::Warning::INFERRING_BOOLEAN_CODE,
46
- 'SampleController#action',
47
- { parameter: 'some-param' }
48
- )
49
- end
45
+ it_behaves_like 'extractable method'
46
+ end
47
+
48
+ describe '#extarct_with_warnings' do
49
+ before { Apipie.configuration.swagger_suppress_warnings = false }
50
+
51
+ subject { extractor.extract_with_warnings(warnings) }
52
+
53
+ it_behaves_like 'extractable method'
54
+
55
+ context "when enum validator is used" do
56
+ context "and has `true`, `false` as values" do
57
+ let(:enum_values) { [true, false] }
58
+
59
+ let(:validator) do
60
+ Apipie::ResponseDescriptionAdapter::PropDesc::Validator.new('some-type', enum_values)
61
+ end
62
+
63
+ context 'and a boolean warning is passed' do
64
+ let(:boolean_warning) do
65
+ Apipie::Generator::Swagger::Warning.for_code(
66
+ Apipie::Generator::Swagger::Warning::INFERRING_BOOLEAN_CODE,
67
+ 'SampleController#action',
68
+ { parameter: 'some-param' }
69
+ )
70
+ end
50
71
 
51
- let(:warnings) { { boolean: boolean_warning } }
72
+ let(:warnings) { { boolean: boolean_warning } }
52
73
 
53
- it 'outputs the warning' do
54
- expect { subject }.to output(boolean_warning.warning_message).to_stderr
55
- end
74
+ it 'outputs the warning' do
75
+ expect { subject }.to output(boolean_warning.warning_message).to_stderr
56
76
  end
57
77
  end
58
78
  end
@@ -43,17 +43,17 @@ describe "param groups" do
43
43
  end
44
44
 
45
45
  it "should replace parameter name in a group when it comes from concern" do
46
- expect(Apipie["overridden_concern_resources#update"].params.has_key?(:user)).to eq(true)
46
+ expect(Apipie["overridden_concern_resources#update"].params.key?(:user)).to eq(true)
47
47
  end
48
48
 
49
49
  it "shouldn't replace parameter name in a group redefined in the controller" do
50
- expect(Apipie["overridden_concern_resources#create"].params.has_key?(:concern)).to eq(true)
51
- expect(Apipie["overridden_concern_resources#create"].params.has_key?(:user)).to eq(false)
50
+ expect(Apipie["overridden_concern_resources#create"].params.key?(:concern)).to eq(true)
51
+ expect(Apipie["overridden_concern_resources#create"].params.key?(:user)).to eq(false)
52
52
  end
53
53
 
54
54
  it "shouldn't replace name of a parameter defined in the controller" do
55
- expect(Apipie["overridden_concern_resources#custom"].params.has_key?(:concern)).to eq(true)
56
- expect(Apipie["overridden_concern_resources#custom"].params.has_key?(:user)).to eq(false)
55
+ expect(Apipie["overridden_concern_resources#custom"].params.key?(:concern)).to eq(true)
56
+ expect(Apipie["overridden_concern_resources#custom"].params.key?(:user)).to eq(false)
57
57
  end
58
58
 
59
59
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apipie-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Pokorny
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2023-01-16 00:00:00.000000000 Z
12
+ date: 2023-02-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: actionpack
@@ -235,6 +235,15 @@ files:
235
235
  - lib/apipie/extractor/recorder.rb
236
236
  - lib/apipie/extractor/writer.rb
237
237
  - lib/apipie/generator/generator.rb
238
+ - lib/apipie/generator/swagger/context.rb
239
+ - lib/apipie/generator/swagger/operation_id.rb
240
+ - lib/apipie/generator/swagger/param_description.rb
241
+ - lib/apipie/generator/swagger/param_description/builder.rb
242
+ - lib/apipie/generator/swagger/param_description/composite.rb
243
+ - lib/apipie/generator/swagger/param_description/description.rb
244
+ - lib/apipie/generator/swagger/param_description/in.rb
245
+ - lib/apipie/generator/swagger/param_description/name.rb
246
+ - lib/apipie/generator/swagger/param_description/type.rb
238
247
  - lib/apipie/generator/swagger/swagger.rb
239
248
  - lib/apipie/generator/swagger/type.rb
240
249
  - lib/apipie/generator/swagger/type_extractor.rb
@@ -340,6 +349,15 @@ files:
340
349
  - spec/lib/extractor/middleware_spec.rb
341
350
  - spec/lib/extractor/writer_spec.rb
342
351
  - spec/lib/file_handler_spec.rb
352
+ - spec/lib/generator/swagger/context_spec.rb
353
+ - spec/lib/generator/swagger/operation_id_spec.rb
354
+ - spec/lib/generator/swagger/param_description/builder_spec.rb
355
+ - spec/lib/generator/swagger/param_description/composite_spec.rb
356
+ - spec/lib/generator/swagger/param_description/description_spec.rb
357
+ - spec/lib/generator/swagger/param_description/in_spec.rb
358
+ - spec/lib/generator/swagger/param_description/name_spec.rb
359
+ - spec/lib/generator/swagger/param_description/type_spec.rb
360
+ - spec/lib/generator/swagger/param_description_spec.rb
343
361
  - spec/lib/generator/swagger/type_extractor_spec.rb
344
362
  - spec/lib/generator/swagger/warning_spec.rb
345
363
  - spec/lib/generator/swagger/warning_writer_spec.rb