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
@@ -1,3 +1,3 @@
1
1
  module Apipie
2
- VERSION = "0.9.1"
2
+ VERSION = "0.9.2"
3
3
  end
data/lib/apipie-rails.rb CHANGED
@@ -27,7 +27,16 @@ require "apipie/version"
27
27
  require "apipie/swagger_generator"
28
28
  require "apipie/generator/generator"
29
29
  require "apipie/generator/swagger/swagger"
30
+ require "apipie/generator/swagger/operation_id"
30
31
  require "apipie/generator/swagger/warning"
31
32
  require "apipie/generator/swagger/warning_writer"
32
33
  require "apipie/generator/swagger/type"
33
34
  require "apipie/generator/swagger/type_extractor"
35
+ require "apipie/generator/swagger/context"
36
+ require "apipie/generator/swagger/param_description"
37
+ require "apipie/generator/swagger/param_description/builder"
38
+ require "apipie/generator/swagger/param_description/composite"
39
+ require "apipie/generator/swagger/param_description/description"
40
+ require "apipie/generator/swagger/param_description/in"
41
+ require "apipie/generator/swagger/param_description/name"
42
+ require "apipie/generator/swagger/param_description/type"
@@ -150,12 +150,11 @@ namespace :apipie do
150
150
  doc[:docs][:link_extension] = (lang ? ".#{lang}.html" : ".html")
151
151
 
152
152
  generate_index_page(file_base_version, doc, true, true, lang) if generate_index
153
- if generate_resources
154
- Apipie.url_prefix = "../#{subdir_traversal_prefix}#{subdir}"
155
- generate_resource_pages(version, file_base_version, doc, true, lang)
156
- Apipie.url_prefix = "../../#{subdir_traversal_prefix}#{subdir}"
157
- generate_method_pages(version, file_base_version, doc, true, lang)
158
- end
153
+ next unless generate_resources
154
+ Apipie.url_prefix = "../#{subdir_traversal_prefix}#{subdir}"
155
+ generate_resource_pages(version, file_base_version, doc, true, lang)
156
+ Apipie.url_prefix = "../../#{subdir_traversal_prefix}#{subdir}"
157
+ generate_method_pages(version, file_base_version, doc, true, lang)
159
158
  end
160
159
  end
161
160
  end
@@ -5,9 +5,10 @@ def compare_hashes(h1, h2)
5
5
  expect(h1).to eq(h2)
6
6
  else
7
7
  h1.each do |key, val|
8
- if val.is_a? Hash
8
+ case val
9
+ when Hash
9
10
  compare_hashes val, h2[key]
10
- elsif val.is_a? Array
11
+ when Array
11
12
  val.each_with_index do |v, i|
12
13
  compare_hashes val[i], h2[key][i]
13
14
  end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apipie::Generator::Swagger::Context do
4
+ let(:allow_null) { true }
5
+ let(:in_schema) { true }
6
+ let(:http_method) { 'get' }
7
+
8
+ subject do
9
+ described_class.new(
10
+ allow_null: allow_null,
11
+ http_method: http_method,
12
+ in_schema: in_schema,
13
+ controller_method: 'show'
14
+ )
15
+ end
16
+
17
+ describe '#in_schema?' do
18
+ it { is_expected.to be_in_schema }
19
+ context 'when in_schema is false' do
20
+ let(:in_schema) { false }
21
+
22
+ it { is_expected.not_to be_in_schema }
23
+ end
24
+ end
25
+
26
+ describe '#allow_null?' do
27
+ it { is_expected.to be_allow_null }
28
+
29
+ context 'when allow_null is false' do
30
+ let(:allow_null) { false }
31
+
32
+ it { is_expected.not_to be_allow_null }
33
+ end
34
+ end
35
+ 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,163 @@
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: 'show'
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 in_schema is false' do
82
+ let(:in_schema) { false }
83
+
84
+ it { is_expected.to be_blank }
85
+
86
+ context 'and is required' do
87
+ let(:base_param_description_options) { { required: true } }
88
+
89
+ it { is_expected.to eq(true) }
90
+ end
91
+ end
92
+ end
93
+
94
+ describe 'warning' do
95
+ before { Singleton.__init__(Apipie::Generator::Swagger::WarningWriter) }
96
+
97
+ subject { generated_block }
98
+
99
+ context 'when is not required' do
100
+ let(:base_param_description_options) { { required: false } }
101
+
102
+ context 'and no default is given' do
103
+ before { param_description_options.delete(:default) }
104
+
105
+ it 'outputs an option without default warning' do
106
+ expect { subject }.to output(/is optional but default value is not specified/).to_stderr
107
+ end
108
+ end
109
+ end
110
+ end
111
+
112
+ describe '.with_type' do
113
+ subject { generated_block[:type] }
114
+
115
+ it { is_expected.to be_blank }
116
+
117
+ context 'when type is assigned' do
118
+ before { builder.with_type(with_null: false ) }
119
+
120
+ it { is_expected.to be_present }
121
+ end
122
+ end
123
+
124
+ describe '.with_description' do
125
+ subject { generated_block[:description] }
126
+
127
+ it { is_expected.to be_blank }
128
+
129
+ context 'when description is assigned' do
130
+ let(:param_description_options) { { desc: 'some-description' } }
131
+
132
+ before { builder.with_description(language: 'en') }
133
+
134
+ it { is_expected.to be_present }
135
+ end
136
+ end
137
+
138
+ describe '.with_name' do
139
+ subject { generated_block[:name] }
140
+
141
+ it { is_expected.to be_blank }
142
+
143
+ context 'when name is assigned' do
144
+ before { builder.with_name }
145
+
146
+ it { is_expected.to be_present }
147
+ end
148
+ end
149
+
150
+ describe '.with_in' do
151
+ subject { generated_block[:in] }
152
+
153
+ it { is_expected.to be_blank }
154
+
155
+ context 'when in is assigned' do
156
+ let(:in_schema) { false }
157
+
158
+ before { builder.with_in(http_method: 'get') }
159
+
160
+ it { is_expected.to be_present }
161
+ end
162
+ end
163
+ 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: 'show'
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.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
@@ -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