apipie-rails 0.9.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +8 -14
  3. data/.rubocop_todo.yml +102 -213
  4. data/CHANGELOG.md +17 -6
  5. data/README.rst +12 -12
  6. data/app/controllers/apipie/apipies_controller.rb +4 -4
  7. data/lib/apipie/apipie_module.rb +4 -4
  8. data/lib/apipie/application.rb +70 -54
  9. data/lib/apipie/configuration.rb +19 -26
  10. data/lib/apipie/dsl_definition.rb +4 -5
  11. data/lib/apipie/extractor/collector.rb +2 -2
  12. data/lib/apipie/extractor.rb +1 -1
  13. data/lib/apipie/generator/config.rb +12 -0
  14. data/lib/apipie/generator/swagger/computed_interface_id.rb +23 -0
  15. data/lib/apipie/generator/swagger/config.rb +78 -0
  16. data/lib/apipie/generator/swagger/context.rb +12 -1
  17. data/lib/apipie/generator/swagger/method_description/api_decorator.rb +20 -0
  18. data/lib/apipie/generator/swagger/method_description/api_schema_service.rb +86 -0
  19. data/lib/apipie/generator/swagger/method_description/decorator.rb +22 -0
  20. data/lib/apipie/generator/swagger/method_description/parameters_service.rb +139 -0
  21. data/lib/apipie/generator/swagger/method_description/response_schema_service.rb +46 -0
  22. data/lib/apipie/generator/swagger/method_description/response_service.rb +58 -0
  23. data/lib/apipie/generator/swagger/method_description.rb +2 -0
  24. data/lib/apipie/generator/swagger/operation_id.rb +1 -1
  25. data/lib/apipie/generator/swagger/param_description/builder.rb +4 -4
  26. data/lib/apipie/generator/swagger/param_description/composite.rb +9 -1
  27. data/lib/apipie/generator/swagger/param_description/in.rb +1 -1
  28. data/lib/apipie/generator/swagger/param_description/path_params_composite.rb +61 -0
  29. data/lib/apipie/generator/swagger/param_description/referenced_composite.rb +36 -0
  30. data/lib/apipie/generator/swagger/param_description/type.rb +9 -2
  31. data/lib/apipie/generator/swagger/path_decorator.rb +36 -0
  32. data/lib/apipie/generator/swagger/referenced_definitions.rb +17 -0
  33. data/lib/apipie/generator/swagger/resource_description_collection.rb +30 -0
  34. data/lib/apipie/generator/swagger/resource_description_composite.rb +56 -0
  35. data/lib/apipie/generator/swagger/schema.rb +63 -0
  36. data/lib/apipie/generator/swagger/type_extractor.rb +0 -19
  37. data/lib/apipie/generator/swagger/warning.rb +3 -6
  38. data/lib/apipie/generator/swagger/warning_writer.rb +7 -1
  39. data/lib/apipie/method_description.rb +4 -2
  40. data/lib/apipie/param_description.rb +4 -2
  41. data/lib/apipie/resource_description.rb +1 -1
  42. data/lib/apipie/swagger_generator.rb +27 -551
  43. data/lib/apipie/validator.rb +7 -3
  44. data/lib/apipie/version.rb +1 -1
  45. data/lib/apipie-rails.rb +17 -0
  46. data/lib/tasks/apipie.rake +15 -10
  47. data/spec/controllers/api/v2/nested/resources_controller_spec.rb +2 -2
  48. data/spec/controllers/pets_controller_spec.rb +10 -16
  49. data/spec/controllers/users_controller_spec.rb +2 -2
  50. data/spec/dummy/app/controllers/pets_controller.rb +4 -4
  51. data/spec/dummy/app/controllers/pets_using_self_describing_classes_controller.rb +2 -2
  52. data/spec/dummy/app/controllers/twitter_example_controller.rb +2 -2
  53. data/spec/dummy/app/controllers/users_controller.rb +5 -5
  54. data/spec/lib/apipie/application_spec.rb +16 -7
  55. data/spec/lib/apipie/configuration_spec.rb +15 -0
  56. data/spec/lib/apipie/generator/swagger/config_spec.rb +19 -0
  57. data/spec/lib/apipie/generator/swagger/context_spec.rb +22 -2
  58. data/spec/lib/apipie/generator/swagger/method_description/api_schema_service_spec.rb +106 -0
  59. data/spec/lib/apipie/generator/swagger/method_description/response_schema_service_spec.rb +105 -0
  60. data/spec/lib/apipie/generator/swagger/param_description/builder_spec.rb +1 -1
  61. data/spec/lib/apipie/generator/swagger/param_description/composite_spec.rb +2 -2
  62. data/spec/lib/apipie/generator/swagger/param_description/type_spec.rb +7 -7
  63. data/spec/lib/apipie/generator/swagger/path_decorator_spec.rb +57 -0
  64. data/spec/lib/apipie/generator/swagger/referenced_definitions_spec.rb +35 -0
  65. data/spec/lib/apipie/generator/swagger/resource_description_composite_spec.rb +37 -0
  66. data/spec/lib/apipie/generator/swagger/resource_descriptions_collection_spec.rb +57 -0
  67. data/spec/lib/apipie/generator/swagger/schema_spec.rb +89 -0
  68. data/spec/lib/apipie/generator/swagger/type_extractor_spec.rb +0 -43
  69. data/spec/lib/apipie/generator/swagger/warning_spec.rb +1 -1
  70. data/spec/lib/apipie/generator/swagger/warning_writer_spec.rb +19 -7
  71. data/spec/lib/apipie/method_description_spec.rb +101 -66
  72. data/spec/lib/apipie/param_description_spec.rb +209 -49
  73. data/spec/lib/apipie/param_group_spec.rb +1 -0
  74. data/spec/lib/apipie/resource_description_spec.rb +1 -1
  75. data/spec/lib/apipie/swagger_generator_spec.rb +94 -0
  76. data/spec/lib/apipie/validator_spec.rb +47 -11
  77. data/spec/lib/swagger/rake_swagger_spec.rb +4 -4
  78. data/spec/lib/swagger/swagger_dsl_spec.rb +7 -7
  79. data/spec/lib/validators/array_validator_spec.rb +1 -1
  80. metadata +28 -2
@@ -75,7 +75,7 @@ describe Apipie::Generator::Swagger::ParamDescription::Type do
75
75
  it { is_expected.to eq(validator.to_s.downcase) }
76
76
 
77
77
  context 'when validator is enum' do
78
- let(:validator) { ['name', 'enum'] }
78
+ let(:validator) { %w[name enum] }
79
79
 
80
80
  it { is_expected.to eq('string') }
81
81
  end
@@ -108,9 +108,9 @@ describe Apipie::Generator::Swagger::ParamDescription::Type do
108
108
 
109
109
  it { is_expected.to eq({ type: 'string' }) }
110
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 }
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
114
 
115
115
  it 'returns the reference' do
116
116
  expect(subject).to eq({ '$ref' => reference })
@@ -130,8 +130,8 @@ describe Apipie::Generator::Swagger::ParamDescription::Type do
130
130
  it { is_expected.to be_blank }
131
131
 
132
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'] } }
133
+ let(:enum_values) { %w[enum-value-1 enum-value-2] }
134
+ let(:validator_options) { { in: %w[enum-value-1 enum-value-2] } }
135
135
 
136
136
  it { is_expected.to eq(enum_values) }
137
137
  end
@@ -144,7 +144,7 @@ describe Apipie::Generator::Swagger::ParamDescription::Type do
144
144
  subject { type_definition[:enum] }
145
145
 
146
146
  context 'and an array of in values is given' do
147
- let(:validator) { ['enum-value-1', 'enum-value-2'] }
147
+ let(:validator) { %w[enum-value-1 enum-value-2] }
148
148
 
149
149
  it { is_expected.to eq(validator) }
150
150
  end
@@ -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
@@ -26,15 +26,6 @@ describe Apipie::Generator::Swagger::TypeExtractor do
26
26
  it 'returns an enum type' do
27
27
  expect(subject).to eq(Apipie::Generator::Swagger::TypeExtractor::TYPES[:enum])
28
28
  end
29
-
30
- context 'and has `true`, `false` as values' do
31
- let(:param_description_name) { :visible }
32
- let(:enum_values) { [true, false] }
33
-
34
- it 'returns a boolean type' do
35
- expect(subject).to eq(Apipie::Generator::Swagger::TypeExtractor::TYPES[:boolean])
36
- end
37
- end
38
29
  end
39
30
  end
40
31
  end
@@ -44,38 +35,4 @@ describe Apipie::Generator::Swagger::TypeExtractor do
44
35
 
45
36
  it_behaves_like 'extractable method'
46
37
  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
71
-
72
- let(:warnings) { { boolean: boolean_warning } }
73
-
74
- it 'outputs the warning' do
75
- expect { subject }.to output(boolean_warning.warning_message).to_stderr
76
- end
77
- end
78
- end
79
- end
80
- end
81
38
  end
@@ -16,7 +16,7 @@ describe Apipie::Generator::Swagger::Warning do
16
16
  describe '#warning_message' do
17
17
  subject { warning.warning_message }
18
18
 
19
- it { is_expected.to eq("WARNING (#{code}): [#{method_id}] -- #{info_message}") }
19
+ it { is_expected.to eq("WARNING (#{code}): [#{method_id}] -- #{info_message}\n") }
20
20
  end
21
21
 
22
22
  describe '#warn' do
@@ -5,14 +5,14 @@ describe Apipie::Generator::Swagger::WarningWriter do
5
5
 
6
6
  let(:warning) do
7
7
  Apipie::Generator::Swagger::Warning.for_code(
8
- Apipie::Generator::Swagger::Warning::INFERRING_BOOLEAN_CODE,
8
+ Apipie::Generator::Swagger::Warning::PARAM_IGNORED_IN_FORM_DATA_CODE,
9
9
  'SampleController#action',
10
10
  { parameter: 'some-param' }
11
11
  )
12
12
  end
13
13
 
14
14
  before do
15
- Apipie.configuration.swagger_suppress_warnings = false
15
+ Apipie.configuration.generator.swagger.suppress_warnings = false
16
16
  Singleton.__init__(described_class)
17
17
  end
18
18
 
@@ -23,16 +23,16 @@ describe Apipie::Generator::Swagger::WarningWriter do
23
23
  expect { subject }.to output(warning.warning_message).to_stderr
24
24
  end
25
25
 
26
- context 'when Apipie.configuration.swagger_suppress_warnings is true' do
27
- before { Apipie.configuration.swagger_suppress_warnings = true }
26
+ context 'when Apipie.configuration.generator.swagger.suppress_warnings is true' do
27
+ before { Apipie.configuration.generator.swagger.suppress_warnings = true }
28
28
 
29
29
  it { is_expected.to be_falsey }
30
30
  end
31
31
 
32
- context 'when Apipie.configuration.swagger_suppress_warnings includes warning code' do
32
+ context 'when Apipie.configuration.generator.swagger.suppress_warnings includes warning code' do
33
33
  before do
34
- Apipie.configuration.swagger_suppress_warnings =
35
- Array(Apipie::Generator::Swagger::Warning::INFERRING_BOOLEAN_CODE)
34
+ Apipie.configuration.generator.swagger.suppress_warnings =
35
+ Array(Apipie::Generator::Swagger::Warning::PARAM_IGNORED_IN_FORM_DATA_CODE)
36
36
  end
37
37
 
38
38
  it { is_expected.to be_falsey }
@@ -56,4 +56,16 @@ describe Apipie::Generator::Swagger::WarningWriter do
56
56
  it { is_expected.to be_truthy }
57
57
  end
58
58
  end
59
+
60
+ describe '#clear!' do
61
+ subject { writer.clear! }
62
+
63
+ context 'when writer has issued warnings' do
64
+ before { writer.warn(warning) }
65
+
66
+ it 'removes issued warnings' do
67
+ expect { subject }.to change(writer, :issued_warnings?).from(true).to(false)
68
+ end
69
+ end
70
+ end
59
71
  end
@@ -1,98 +1,133 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Apipie::MethodDescription do
4
-
5
4
  let(:dsl_data) { ActionController::Base.send(:_apipie_dsl_data_init) }
5
+ let(:deprecated_resource_description) { false }
6
+ let(:method_description_method) { :some_method }
6
7
 
7
- describe "metadata" do
8
+ let(:resource_description_dsl) do
9
+ ActionController::Base.send(:_apipie_dsl_data_init).merge(
10
+ {
11
+ deprecated: deprecated_resource_description
12
+ }
13
+ )
14
+ end
8
15
 
9
- before(:each) do
10
- @resource = Apipie::ResourceDescription.new(ApplicationController, "dummy")
11
- end
16
+ let(:resource_description) do
17
+ Apipie::ResourceDescription.new(
18
+ ApplicationController,
19
+ 'dummy',
20
+ resource_description_dsl
21
+ )
22
+ end
12
23
 
13
- it "should return nil when no metadata is provided" do
14
- method = Apipie::MethodDescription.new(:a, @resource, dsl_data)
15
- expect(method.to_json[:metadata]).to eq(nil)
16
- end
24
+ let(:method_description) do
25
+ Apipie::MethodDescription.new(
26
+ method_description_method,
27
+ resource_description,
28
+ dsl_data
29
+ )
30
+ end
17
31
 
18
- it "should return the metadata" do
19
- meta = {
20
- :lenght => 32,
21
- :weight => '830g'
22
- }
23
- method = Apipie::MethodDescription.new(:a, @resource, dsl_data.update(:meta => meta))
24
- expect(method.to_json[:metadata]).to eq(meta)
25
- end
32
+ describe '#to_json' do
33
+ describe 'metadata' do
34
+ subject { method_description.to_json[:metadata] }
26
35
 
27
- end
36
+ it { is_expected.to be_nil }
28
37
 
29
- describe "deprecated flag" do
30
- before(:each) do
31
- @resource = Apipie::ResourceDescription.new(ApplicationController, "dummy")
32
- end
38
+ context 'when meta are given' do
39
+ let(:meta) { { length: 32, weight: '830g' } }
40
+ let(:dsl_data) { super().merge(meta: meta) }
33
41
 
34
- it "should return the deprecated flag when provided" do
35
- dsl_data[:api_args] = [[:GET, "/foo/bar", "description", {:deprecated => true}]]
36
- method = Apipie::MethodDescription.new(:a, @resource, dsl_data)
37
- expect(method.method_apis_to_json.first[:deprecated]).to eq(true)
42
+ it { is_expected.to eq(meta) }
43
+ end
38
44
  end
39
45
 
40
- it "should return the deprecated flag if resource is deprecated" do
41
- @resource.instance_variable_set("@_deprecated", true)
42
- dsl_data[:api_args] = [[:GET, "/foo/bar", "description", {}]]
43
- method = Apipie::MethodDescription.new(:a, @resource, dsl_data)
44
- expect(method.method_apis_to_json.first[:deprecated]).to eq(true)
46
+ describe 'params' do
47
+ subject(:json_params) { method_description.to_json[:params].map { |h| h[:name] } }
48
+
49
+ let(:dsl_data) do
50
+ super().merge(
51
+ {
52
+ params: [
53
+ [:a, String, nil, {}, nil],
54
+ [:b, String, nil, {}, nil],
55
+ [:c, String, nil, {}, nil]
56
+ ]
57
+ }
58
+ )
59
+ end
60
+
61
+ it 'should be ordered' do
62
+ expect(json_params).to eq(%w[a b c])
63
+ end
64
+
65
+ context 'when param is only for response' do
66
+ let(:dsl_data) do
67
+ super().merge(
68
+ {
69
+ params: [
70
+ [:a, String, nil, { only_in: :request }, nil],
71
+ [:b, String, nil, { only_in: :response }, nil],
72
+ [:c, String, nil, {}, nil]
73
+ ]
74
+ }
75
+ )
76
+ end
77
+
78
+ it 'should ignore response-only parameters' do
79
+ expect(json_params).to eq(%w[a c])
80
+ end
81
+ end
45
82
  end
46
83
  end
47
84
 
48
- describe "params descriptions" do
85
+ describe '#method_apis_to_json' do
86
+ describe 'deprecated' do
87
+ subject { method_description.method_apis_to_json.first[:deprecated] }
49
88
 
50
- before(:each) do
51
- @resource = Apipie::ResourceDescription.new(ApplicationController, "dummy")
52
- dsl_data[:params] = [[:a, String, nil, {}, nil],
53
- [:b, String, nil, {}, nil],
54
- [:c, String, nil, {}, nil]]
55
- @method = Apipie::MethodDescription.new(:a, @resource, dsl_data)
56
- @resource.add_method_description @method
57
- end
89
+ let(:api_deprecated) { false }
90
+ let(:dsl_data) { super().merge(api_args: api_args) }
58
91
 
59
- it "should be ordered" do
60
- expect(@method.params.keys).to eq([:a, :b, :c])
61
- expect(@method.to_json[:params].map { |h| h[:name] }).to eq(['a', 'b', 'c'])
62
- end
92
+ let(:api_args) do
93
+ [[:GET, "/foo/bar", "description", { deprecated: api_deprecated }]]
94
+ end
63
95
 
64
- it "should be still ordered" do
65
- expect(@method.params.keys).to eq([:a, :b, :c])
66
- expect(@method.to_json[:params].map { |h| h[:name] }).to eq(['a', 'b', 'c'])
67
- end
96
+ it { is_expected.to eq(false) }
68
97
 
69
- end
98
+ context 'when api is deprecated' do
99
+ let(:api_deprecated) { true }
70
100
 
71
- describe "response-only properties" do
72
- before(:each) do
73
- @resource = Apipie::ResourceDescription.new(ApplicationController, "dummy")
74
- dsl_data[:params] = [[:a, String, nil, {:only_in => :request}, nil],
75
- [:b, String, nil, {:only_in => :response}, nil],
76
- [:c, String, nil, {}, nil]]
77
- @method = Apipie::MethodDescription.new(:a, @resource, dsl_data)
78
- @resource.add_method_description @method
79
- end
101
+ it { is_expected.to eq(true) }
102
+ end
103
+
104
+ context 'when resource description is deprecated' do
105
+ let(:deprecated_resource_description) { true }
80
106
 
81
- it "should ignore response-only parameters" do
82
- expect(@method.params.keys).to eq([:a, :c])
83
- expect(@method.to_json[:params].map { |h| h[:name] }).to eq(['a', 'c'])
107
+ it { is_expected.to eq(true) }
108
+ end
84
109
  end
85
110
  end
86
111
 
112
+ describe '#returns' do
113
+ subject(:method_desc) { method_description }
87
114
 
88
- describe "'returns' properties" do
89
- it "should raise an error if both :param_group and :array_of are specified in 'returns'" do
90
- @resource = Apipie::ResourceDescription.new(ApplicationController, "dummy")
91
- dsl_data[:returns] = { 200 => [{:param_group => 'pet', :array_of => 'pet'}, nil, nil] }
115
+ context 'when both :param_group and :array_of are specified' do
116
+ let(:returns) do
117
+ { 200 => [{ param_group: 'pet', array_of: 'pet' }, nil, nil] }
118
+ end
92
119
 
93
- expect { Apipie::MethodDescription.new(:a, @resource, dsl_data) }.to raise_error(Apipie::ReturnsMultipleDefinitionError)
120
+ let(:dsl_data) { super().merge({ returns: returns }) }
121
+
122
+ it 'raises an error' do
123
+ expect { method_desc }.to raise_error(Apipie::ReturnsMultipleDefinitionError)
124
+ end
94
125
  end
95
126
  end
96
127
 
128
+ describe '#method_name' do
129
+ subject { method_description.method_name }
97
130
 
131
+ it { is_expected.to eq(method_description_method.to_s) }
132
+ end
98
133
  end