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.
- checksums.yaml +4 -4
- data/.rubocop.yml +8 -14
- data/.rubocop_todo.yml +102 -213
- data/CHANGELOG.md +17 -6
- data/README.rst +12 -12
- data/app/controllers/apipie/apipies_controller.rb +4 -4
- data/lib/apipie/apipie_module.rb +4 -4
- data/lib/apipie/application.rb +70 -54
- data/lib/apipie/configuration.rb +19 -26
- data/lib/apipie/dsl_definition.rb +4 -5
- data/lib/apipie/extractor/collector.rb +2 -2
- data/lib/apipie/extractor.rb +1 -1
- data/lib/apipie/generator/config.rb +12 -0
- data/lib/apipie/generator/swagger/computed_interface_id.rb +23 -0
- data/lib/apipie/generator/swagger/config.rb +78 -0
- data/lib/apipie/generator/swagger/context.rb +12 -1
- data/lib/apipie/generator/swagger/method_description/api_decorator.rb +20 -0
- data/lib/apipie/generator/swagger/method_description/api_schema_service.rb +86 -0
- data/lib/apipie/generator/swagger/method_description/decorator.rb +22 -0
- data/lib/apipie/generator/swagger/method_description/parameters_service.rb +139 -0
- data/lib/apipie/generator/swagger/method_description/response_schema_service.rb +46 -0
- data/lib/apipie/generator/swagger/method_description/response_service.rb +58 -0
- data/lib/apipie/generator/swagger/method_description.rb +2 -0
- data/lib/apipie/generator/swagger/operation_id.rb +1 -1
- data/lib/apipie/generator/swagger/param_description/builder.rb +4 -4
- data/lib/apipie/generator/swagger/param_description/composite.rb +9 -1
- data/lib/apipie/generator/swagger/param_description/in.rb +1 -1
- data/lib/apipie/generator/swagger/param_description/path_params_composite.rb +61 -0
- data/lib/apipie/generator/swagger/param_description/referenced_composite.rb +36 -0
- data/lib/apipie/generator/swagger/param_description/type.rb +9 -2
- data/lib/apipie/generator/swagger/path_decorator.rb +36 -0
- data/lib/apipie/generator/swagger/referenced_definitions.rb +17 -0
- data/lib/apipie/generator/swagger/resource_description_collection.rb +30 -0
- data/lib/apipie/generator/swagger/resource_description_composite.rb +56 -0
- data/lib/apipie/generator/swagger/schema.rb +63 -0
- data/lib/apipie/generator/swagger/type_extractor.rb +0 -19
- data/lib/apipie/generator/swagger/warning.rb +3 -6
- data/lib/apipie/generator/swagger/warning_writer.rb +7 -1
- data/lib/apipie/method_description.rb +4 -2
- data/lib/apipie/param_description.rb +4 -2
- data/lib/apipie/resource_description.rb +1 -1
- data/lib/apipie/swagger_generator.rb +27 -551
- data/lib/apipie/validator.rb +7 -3
- data/lib/apipie/version.rb +1 -1
- data/lib/apipie-rails.rb +17 -0
- data/lib/tasks/apipie.rake +15 -10
- data/spec/controllers/api/v2/nested/resources_controller_spec.rb +2 -2
- data/spec/controllers/pets_controller_spec.rb +10 -16
- data/spec/controllers/users_controller_spec.rb +2 -2
- data/spec/dummy/app/controllers/pets_controller.rb +4 -4
- data/spec/dummy/app/controllers/pets_using_self_describing_classes_controller.rb +2 -2
- data/spec/dummy/app/controllers/twitter_example_controller.rb +2 -2
- data/spec/dummy/app/controllers/users_controller.rb +5 -5
- data/spec/lib/apipie/application_spec.rb +16 -7
- data/spec/lib/apipie/configuration_spec.rb +15 -0
- data/spec/lib/apipie/generator/swagger/config_spec.rb +19 -0
- data/spec/lib/apipie/generator/swagger/context_spec.rb +22 -2
- data/spec/lib/apipie/generator/swagger/method_description/api_schema_service_spec.rb +106 -0
- data/spec/lib/apipie/generator/swagger/method_description/response_schema_service_spec.rb +105 -0
- data/spec/lib/apipie/generator/swagger/param_description/builder_spec.rb +1 -1
- data/spec/lib/apipie/generator/swagger/param_description/composite_spec.rb +2 -2
- data/spec/lib/apipie/generator/swagger/param_description/type_spec.rb +7 -7
- data/spec/lib/apipie/generator/swagger/path_decorator_spec.rb +57 -0
- data/spec/lib/apipie/generator/swagger/referenced_definitions_spec.rb +35 -0
- data/spec/lib/apipie/generator/swagger/resource_description_composite_spec.rb +37 -0
- data/spec/lib/apipie/generator/swagger/resource_descriptions_collection_spec.rb +57 -0
- data/spec/lib/apipie/generator/swagger/schema_spec.rb +89 -0
- data/spec/lib/apipie/generator/swagger/type_extractor_spec.rb +0 -43
- data/spec/lib/apipie/generator/swagger/warning_spec.rb +1 -1
- data/spec/lib/apipie/generator/swagger/warning_writer_spec.rb +19 -7
- data/spec/lib/apipie/method_description_spec.rb +101 -66
- data/spec/lib/apipie/param_description_spec.rb +209 -49
- data/spec/lib/apipie/param_group_spec.rb +1 -0
- data/spec/lib/apipie/resource_description_spec.rb +1 -1
- data/spec/lib/apipie/swagger_generator_spec.rb +94 -0
- data/spec/lib/apipie/validator_spec.rb +47 -11
- data/spec/lib/swagger/rake_swagger_spec.rb +4 -4
- data/spec/lib/swagger/swagger_dsl_spec.rb +7 -7
- data/spec/lib/validators/array_validator_spec.rb +1 -1
- metadata +28 -2
data/lib/tasks/apipie.rake
CHANGED
@@ -195,10 +195,15 @@ namespace :apipie do
|
|
195
195
|
end
|
196
196
|
|
197
197
|
def generate_swagger_using_args(args, out)
|
198
|
-
args.with_defaults(
|
199
|
-
|
200
|
-
|
201
|
-
|
198
|
+
args.with_defaults(
|
199
|
+
version: Apipie.configuration.default_version,
|
200
|
+
swagger_content_type_input: Apipie.configuration.generator.swagger.content_type_input,
|
201
|
+
filename_suffix: nil
|
202
|
+
)
|
203
|
+
|
204
|
+
Apipie.configuration.generator.swagger.content_type_input =
|
205
|
+
args[:swagger_content_type_input].to_sym
|
206
|
+
|
202
207
|
count = 0
|
203
208
|
|
204
209
|
sfx = args[:filename_suffix] || "_#{args[:swagger_content_type_input]}"
|
@@ -254,11 +259,11 @@ namespace :apipie do
|
|
254
259
|
end
|
255
260
|
|
256
261
|
def generate_resource_pages(version, file_base, doc, include_json = false, lang = nil)
|
257
|
-
doc[:docs][:resources].each do |
|
258
|
-
resource_file_base = File.join(file_base,
|
262
|
+
doc[:docs][:resources].each do |resource_id, _|
|
263
|
+
resource_file_base = File.join(file_base, resource_id.to_s)
|
259
264
|
FileUtils.mkdir_p(File.dirname(resource_file_base)) unless File.exist?(File.dirname(resource_file_base))
|
260
265
|
|
261
|
-
doc = Apipie.to_json(version,
|
266
|
+
doc = Apipie.to_json(version, resource_id, nil, lang)
|
262
267
|
doc[:docs][:link_extension] = (lang ? ".#{lang}.html" : ".html")
|
263
268
|
render_page("#{resource_file_base}#{lang_ext(lang)}.html", "resource", {:doc => doc[:docs],
|
264
269
|
:resource => doc[:docs][:resources].first, :language => lang, :languages => Apipie.configuration.languages})
|
@@ -267,12 +272,12 @@ namespace :apipie do
|
|
267
272
|
end
|
268
273
|
|
269
274
|
def generate_method_pages(version, file_base, doc, include_json = false, lang = nil)
|
270
|
-
doc[:docs][:resources].each do |
|
275
|
+
doc[:docs][:resources].each do |resource_id, resource_params|
|
271
276
|
resource_params[:methods].each do |method|
|
272
|
-
method_file_base = File.join(file_base,
|
277
|
+
method_file_base = File.join(file_base, resource_id.to_s, method[:name].to_s)
|
273
278
|
FileUtils.mkdir_p(File.dirname(method_file_base)) unless File.exist?(File.dirname(method_file_base))
|
274
279
|
|
275
|
-
doc = Apipie.to_json(version,
|
280
|
+
doc = Apipie.to_json(version, resource_id, method[:name], lang)
|
276
281
|
doc[:docs][:link_extension] = (lang ? ".#{lang}.html" : ".html")
|
277
282
|
render_page("#{method_file_base}#{lang_ext(lang)}.html", "method", {:doc => doc[:docs],
|
278
283
|
:resource => doc[:docs][:resources].first,
|
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Api::V2::Nested::ResourcesController do
|
4
|
-
describe
|
5
|
-
subject { Apipie.
|
4
|
+
describe '.get_resource_id' do
|
5
|
+
subject { Apipie.get_resource_id(Api::V2::Nested::ResourcesController) }
|
6
6
|
|
7
7
|
it "should have resource_id set" do
|
8
8
|
expect(subject).to eq("resource")
|
@@ -5,10 +5,6 @@ require 'apipie/rspec/response_validation_helper'
|
|
5
5
|
require "json-schema"
|
6
6
|
|
7
7
|
RSpec.describe PetsController, :type => :controller do
|
8
|
-
before :each do
|
9
|
-
Apipie.configuration.swagger_allow_additional_properties_in_response = false
|
10
|
-
end
|
11
|
-
|
12
8
|
it "does not raise error when rendered output matches the described response" do
|
13
9
|
response = get :return_and_validate_expected_response, format: :json
|
14
10
|
expect(response).to match_declared_responses
|
@@ -39,7 +35,7 @@ RSpec.describe PetsController, :type => :controller do
|
|
39
35
|
expect(response).not_to match_declared_responses
|
40
36
|
end
|
41
37
|
|
42
|
-
it "raises error when a response has an extra property and '
|
38
|
+
it "raises error when a response has an extra property and 'swagger.allow_additional_properties_in_response' is false" do
|
43
39
|
response = get :return_and_validate_extra_property, format: :json
|
44
40
|
expect(response).not_to match_declared_responses
|
45
41
|
end
|
@@ -50,26 +46,28 @@ RSpec.describe PetsController, :type => :controller do
|
|
50
46
|
expect(response).not_to match_declared_responses
|
51
47
|
end
|
52
48
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
49
|
+
context "when allow_additional_properties_in_response is true'" do
|
50
|
+
before do
|
51
|
+
Apipie.configuration.generator.swagger.allow_additional_properties_in_response = true
|
52
|
+
end
|
53
|
+
|
54
|
+
it "does not raise error when a response has an extra property" do
|
55
|
+
response = get :return_and_validate_extra_property, format: :json
|
56
|
+
expect(response).to match_declared_responses
|
57
|
+
end
|
57
58
|
end
|
58
59
|
|
59
60
|
it "does not raise error when a response has an extra field and 'additional_properties' is specified in the response" do
|
60
|
-
Apipie.configuration.swagger_allow_additional_properties_in_response = false
|
61
61
|
response = get :return_and_validate_allowed_extra_property, format: :json
|
62
62
|
expect(response).to match_declared_responses
|
63
63
|
end
|
64
64
|
|
65
65
|
it "raises error when a response sub-object has an extra field and 'additional_properties' is not specified on it, but specified on the top level of the response" do
|
66
|
-
Apipie.configuration.swagger_allow_additional_properties_in_response = false
|
67
66
|
response = get :sub_object_invalid_extra_property, format: :json
|
68
67
|
expect(response).not_to match_declared_responses
|
69
68
|
end
|
70
69
|
|
71
70
|
it "does not raise error when a response sub-object has an extra field and 'additional_properties' is specified on it" do
|
72
|
-
Apipie.configuration.swagger_allow_additional_properties_in_response = false
|
73
71
|
response = get :sub_object_allowed_extra_property, format: :json
|
74
72
|
expect(response).to match_declared_responses
|
75
73
|
end
|
@@ -86,7 +84,6 @@ RSpec.describe PetsController, :type => :controller do
|
|
86
84
|
|
87
85
|
end
|
88
86
|
|
89
|
-
|
90
87
|
describe "with array field" do
|
91
88
|
it "no error for valid response" do
|
92
89
|
response = get :returns_response_with_valid_array, format: :json
|
@@ -98,7 +95,4 @@ RSpec.describe PetsController, :type => :controller do
|
|
98
95
|
expect(response).not_to match_declared_responses
|
99
96
|
end
|
100
97
|
end
|
101
|
-
|
102
|
-
|
103
|
-
|
104
98
|
end
|
@@ -412,7 +412,7 @@ describe UsersController do
|
|
412
412
|
expect(b.resource._id).to eq('users')
|
413
413
|
|
414
414
|
expect(b.apis.count).to eq(1)
|
415
|
-
expect(b.formats).to eq([
|
415
|
+
expect(b.formats).to eq(%w[json jsonp])
|
416
416
|
api = b.apis.first
|
417
417
|
expect(api.short_description).to eq("Show user profile")
|
418
418
|
expect(api.path).to eq("/users/:id")
|
@@ -708,7 +708,7 @@ EOS2
|
|
708
708
|
param = a.params[:array_param]
|
709
709
|
expect(param.desc).to eq("\n<p>array validator</p>\n")
|
710
710
|
expect(param.validator.class).to be(Apipie::Validator::EnumValidator)
|
711
|
-
expect(param.validator.instance_variable_get("@array")).to eq([
|
711
|
+
expect(param.validator.instance_variable_get("@array")).to eq(%w[100 one two 1 2])
|
712
712
|
|
713
713
|
param = a.params[:proc_param]
|
714
714
|
expect(param.desc).to eq("\n<p>proc validator</p>\n")
|
@@ -24,7 +24,7 @@ class PetsController < ApplicationController
|
|
24
24
|
api :GET, "/pets/:id/as_properties", "Get a pet record"
|
25
25
|
returns :code => 200 do
|
26
26
|
property :pet_name, String, :desc => "Name of pet", :required => false
|
27
|
-
property :animal_type, [
|
27
|
+
property :animal_type, %w[dog cat iguana kangaroo], :desc => "Type of pet" # required by default, because this is a 'property'
|
28
28
|
end
|
29
29
|
returns :code => 404 do
|
30
30
|
property :another_error_message, String, :desc => "Overriding the response description from the Pets resource"
|
@@ -40,7 +40,7 @@ class PetsController < ApplicationController
|
|
40
40
|
#-----------------------------------------------------------
|
41
41
|
def_param_group :pet do
|
42
42
|
property :pet_name, String, :desc => "Name of pet", :required => false
|
43
|
-
property :animal_type, [
|
43
|
+
property :animal_type, %w[dog cat iguana kangaroo], :desc => "Type of pet" # required by default, because this is a 'property'
|
44
44
|
end
|
45
45
|
|
46
46
|
api :GET, "/pets/:id/as_param_group_of_properties", "Get a pet record"
|
@@ -70,7 +70,7 @@ class PetsController < ApplicationController
|
|
70
70
|
param :pet_id, Integer, :desc => "id of pet", :required => true
|
71
71
|
param :pet_name, String, :desc => "Name of pet", :required => false, :only_in => :response
|
72
72
|
param :partial_match_allowed, [true, false], :desc => "Partial match allowed?", :required => false, :only_in => :request
|
73
|
-
property :animal_type, [
|
73
|
+
property :animal_type, %w[dog cat iguana kangaroo], :desc => "Type of pet" # this is implicitly :only_in => :response
|
74
74
|
end
|
75
75
|
|
76
76
|
api :GET, "/pets/pet_by_id", "Get a pet record with the pet id in the body of the request"
|
@@ -142,7 +142,7 @@ class PetsController < ApplicationController
|
|
142
142
|
end
|
143
143
|
returns :code => 204 do
|
144
144
|
property :int_array, :array_of => Integer
|
145
|
-
property :enum_array, :array_of => [
|
145
|
+
property :enum_array, :array_of => %w[v1 v2 v3]
|
146
146
|
end
|
147
147
|
returns :code => :unprocessable_entity, :desc => "Fleas were discovered on the pet" do
|
148
148
|
param_group :pet
|
@@ -14,7 +14,7 @@ class Pet
|
|
14
14
|
def self.describe_own_properties
|
15
15
|
[
|
16
16
|
Apipie::prop(:pet_name, 'string', {:description => 'Name of pet', :required => false}),
|
17
|
-
Apipie::prop(:animal_type, 'string', {:description => 'Type of pet', :values => [
|
17
|
+
Apipie::prop(:animal_type, 'string', {:description => 'Type of pet', :values => %w[dog cat iguana kangaroo]}),
|
18
18
|
Apipie::additional_properties(false)
|
19
19
|
]
|
20
20
|
end
|
@@ -27,7 +27,7 @@ class PetWithMeasurements
|
|
27
27
|
def self.describe_own_properties
|
28
28
|
[
|
29
29
|
Apipie::prop(:pet_name, 'string', {:description => 'Name of pet', :required => false}),
|
30
|
-
Apipie::prop('animal_type', 'string', {:description => 'Type of pet', :values => [
|
30
|
+
Apipie::prop('animal_type', 'string', {:description => 'Type of pet', :values => %w[dog cat iguana kangaroo]}),
|
31
31
|
Apipie::prop(:pet_measurements, 'object', {}, [
|
32
32
|
Apipie::prop(:weight, 'number', {:description => "Weight in pounds" }),
|
33
33
|
Apipie::prop(:height, 'number', {:description => "Height in inches" }),
|
@@ -31,7 +31,7 @@ class TwitterExampleController < ApplicationController
|
|
31
31
|
|
32
32
|
api :GET, '/twitter_example/profile_image/:screen_name', 'Access the profile image in various sizes for the user with the indicated screen_name.'
|
33
33
|
param :screen_name, String, :required => true, :desc => 'The screen name of the user for whom to return results for. Helpful for disambiguating when a valid screen name is also a user ID.'
|
34
|
-
param :size, [
|
34
|
+
param :size, %w[bigger normal mini original], :desc => <<-EOS
|
35
35
|
Specifies the size of image to fetch. Not specifying a size will give the default, normal size of 48px by 48px. Valid options include:
|
36
36
|
|
37
37
|
* bigger - 73px by 73px
|
@@ -166,7 +166,7 @@ class TwitterExampleController < ApplicationController
|
|
166
166
|
param :user_id, Integer, :desc => 'The ID of the user for whom to return results for. Helpful for disambiguating when a valid user ID is also a valid screen name.'
|
167
167
|
param :screen_name, String, :desc => 'The screen name of the user for whom to return results for. Helpful for disambiguating when a valid screen name is also a user ID.'
|
168
168
|
param :include_entities, String
|
169
|
-
param :skip_status, [
|
169
|
+
param :skip_status, %w[t true 1],
|
170
170
|
:description => 'When set to either true, t or 1 statuses will not be included in the returned user objects.'
|
171
171
|
|
172
172
|
description "Look at examples."
|
@@ -170,14 +170,14 @@ class UsersController < ApplicationController
|
|
170
170
|
eos
|
171
171
|
api :GET, "/users/:id", "Show user profile"
|
172
172
|
show false
|
173
|
-
formats [
|
173
|
+
formats %w[json jsonp]
|
174
174
|
error 401, "Unauthorized"
|
175
175
|
error :code => 404, :description => "Not Found"
|
176
176
|
param :id, Integer, :desc => "user id", :required => true
|
177
177
|
param :session, String, :desc => "user is logged in", :required => true, :missing_message => lambda { "session_parameter_is_required" }
|
178
178
|
param :regexp_param, /^[0-9]* years/, :desc => "regexp param"
|
179
179
|
param :regexp2, /\b[A-Z0-9._%+-=]+@[A-Z0-9.-]+.[A-Z]{2,}\b/i, :desc => "email regexp"
|
180
|
-
param :array_param, [
|
180
|
+
param :array_param, %w[100 one two 1 2], :desc => "array validator"
|
181
181
|
param :boolean_param, [true, false], :desc => "array validator with boolean"
|
182
182
|
param :proc_param, lambda { |val|
|
183
183
|
val == "param value" ? true : "The only good value is 'param value'."
|
@@ -211,7 +211,7 @@ class UsersController < ApplicationController
|
|
211
211
|
def_param_group :user do
|
212
212
|
param :user, Hash, :desc => "User info", :required => true, :action_aware => true do
|
213
213
|
param_group :credentials
|
214
|
-
param :membership, [
|
214
|
+
param :membership, %w[standard premium], :desc => "User membership", :allow_nil => false
|
215
215
|
end
|
216
216
|
end
|
217
217
|
|
@@ -269,13 +269,13 @@ class UsersController < ApplicationController
|
|
269
269
|
end
|
270
270
|
|
271
271
|
api :GET, '/users/by_department', 'show users from a specific department'
|
272
|
-
param :department, [
|
272
|
+
param :department, %w[finance operations sales marketing HR], required: false, default_value: "sales"
|
273
273
|
def get_by_department
|
274
274
|
render :plain => 'nothing to see here'
|
275
275
|
end
|
276
276
|
|
277
277
|
api :GET, '/users/in_departments', 'show users from specific departments'
|
278
|
-
param :departments, Array, in: [
|
278
|
+
param :departments, Array, in: %w[finance operations sales marketing HR], default_value: ['sales']
|
279
279
|
def get_in_departments
|
280
280
|
render :plain => 'nothing to see here'
|
281
281
|
end
|
@@ -16,14 +16,11 @@ describe Apipie::Application do
|
|
16
16
|
|
17
17
|
end
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
Apipie.get_resource_name(Api::V2::Nested::ArchitecturesController)
|
22
|
-
end
|
23
|
-
|
19
|
+
shared_examples 'resource id' do
|
20
|
+
let(:resource_class) { Api::V2::Nested::ArchitecturesController }
|
24
21
|
let(:base_url) { '/some-api' }
|
25
22
|
|
26
|
-
before { allow(
|
23
|
+
before { allow(described_class).to receive(:get_base_url).and_return(base_url) }
|
27
24
|
|
28
25
|
context "with namespaced_resources enabled" do
|
29
26
|
before { Apipie.configuration.namespaced_resources = true }
|
@@ -37,7 +34,7 @@ describe Apipie::Application do
|
|
37
34
|
let(:base_url) { nil }
|
38
35
|
|
39
36
|
it "should not raise an error" do
|
40
|
-
expect {
|
37
|
+
expect { method_call }.not_to raise_error
|
41
38
|
end
|
42
39
|
end
|
43
40
|
end
|
@@ -50,4 +47,16 @@ describe Apipie::Application do
|
|
50
47
|
end
|
51
48
|
end
|
52
49
|
end
|
50
|
+
|
51
|
+
describe '.get_resource_id' do
|
52
|
+
subject(:method_call) { Apipie.get_resource_id(resource_class) }
|
53
|
+
|
54
|
+
it_behaves_like 'resource id'
|
55
|
+
end
|
56
|
+
|
57
|
+
describe '.get_resource_name' do
|
58
|
+
subject(:method_call) { Apipie.get_resource_name(resource_class) }
|
59
|
+
|
60
|
+
it_behaves_like 'resource id'
|
61
|
+
end
|
53
62
|
end
|
@@ -20,4 +20,19 @@ describe 'Apipie::Configuration' do
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
23
|
+
|
24
|
+
describe 'generator configuration' do
|
25
|
+
let(:generator_config) { configuration.generator }
|
26
|
+
|
27
|
+
describe '#swagger' do
|
28
|
+
subject(:setter) { generator_config.swagger.include_warning_tags = true }
|
29
|
+
|
30
|
+
it 'assigns the correct value' do
|
31
|
+
expect { setter }
|
32
|
+
.to change(configuration.generator.swagger, :include_warning_tags?)
|
33
|
+
.from(false)
|
34
|
+
.to(true)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
23
38
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Apipie::Generator::Swagger::Config do
|
4
|
+
let(:config) { described_class.clone.instance }
|
5
|
+
|
6
|
+
before { Singleton.__init__(described_class) }
|
7
|
+
|
8
|
+
describe 'deprecation warnings' do
|
9
|
+
context 'when a deprecated method is call' do
|
10
|
+
subject(:deprecated_method_call) { config.swagger_include_warning_tags? }
|
11
|
+
|
12
|
+
it 'returns a deprecations warning' do
|
13
|
+
expect { deprecated_method_call }
|
14
|
+
.to output(/DEPRECATION WARNING: config.swagger_include_warning_tags is deprecated/)
|
15
|
+
.to_stderr
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -4,17 +4,21 @@ describe Apipie::Generator::Swagger::Context do
|
|
4
4
|
let(:allow_null) { true }
|
5
5
|
let(:in_schema) { true }
|
6
6
|
let(:http_method) { 'get' }
|
7
|
+
let(:prefix) { nil }
|
7
8
|
|
8
|
-
|
9
|
+
let(:context_instance) do
|
9
10
|
described_class.new(
|
10
11
|
allow_null: allow_null,
|
11
12
|
http_method: http_method,
|
12
13
|
in_schema: in_schema,
|
13
|
-
controller_method: 'show'
|
14
|
+
controller_method: 'show',
|
15
|
+
prefix: prefix
|
14
16
|
)
|
15
17
|
end
|
16
18
|
|
17
19
|
describe '#in_schema?' do
|
20
|
+
subject { context_instance }
|
21
|
+
|
18
22
|
it { is_expected.to be_in_schema }
|
19
23
|
|
20
24
|
context 'when in_schema is false' do
|
@@ -25,6 +29,8 @@ describe Apipie::Generator::Swagger::Context do
|
|
25
29
|
end
|
26
30
|
|
27
31
|
describe '#allow_null?' do
|
32
|
+
subject { context_instance }
|
33
|
+
|
28
34
|
it { is_expected.to be_allow_null }
|
29
35
|
|
30
36
|
context 'when allow_null is false' do
|
@@ -33,4 +39,18 @@ describe Apipie::Generator::Swagger::Context do
|
|
33
39
|
it { is_expected.not_to be_allow_null }
|
34
40
|
end
|
35
41
|
end
|
42
|
+
|
43
|
+
describe '#add_to_prefix!' do
|
44
|
+
before { context_instance.add_to_prefix!('some-prefix') }
|
45
|
+
|
46
|
+
subject { context_instance.prefix }
|
47
|
+
|
48
|
+
it { is_expected.to eq('some-prefix') }
|
49
|
+
|
50
|
+
context 'when context has already a prefix' do
|
51
|
+
let(:prefix) { 'existing-prefix' }
|
52
|
+
|
53
|
+
it { is_expected.to eq('existing-prefix[some-prefix]') }
|
54
|
+
end
|
55
|
+
end
|
36
56
|
end
|
@@ -0,0 +1,106 @@
|
|
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.include_warning_tags is enabled' do
|
68
|
+
before { Apipie.configuration.generator.swagger.include_warning_tags = true }
|
69
|
+
|
70
|
+
context 'when warnings are issued' do
|
71
|
+
before do
|
72
|
+
Apipie::Generator::Swagger::Warning.for_code(
|
73
|
+
Apipie::Generator::Swagger::Warning::MISSING_METHOD_SUMMARY_CODE,
|
74
|
+
'some_method'
|
75
|
+
).warn_through_writer
|
76
|
+
end
|
77
|
+
|
78
|
+
it { is_expected.to include('warnings issued') }
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe 'consumes' do
|
84
|
+
subject { service.call[path]['get'][:consumes] }
|
85
|
+
|
86
|
+
it { is_expected.to eq(['application/x-www-form-urlencoded', 'multipart/form-data']) }
|
87
|
+
|
88
|
+
context 'when content type input is json' do
|
89
|
+
before { Apipie.configuration.generator.swagger.content_type_input = :json }
|
90
|
+
|
91
|
+
it { is_expected.to eq(['application/json']) }
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe 'description' do
|
96
|
+
subject { service.call[path]['get'][:description] }
|
97
|
+
|
98
|
+
it { is_expected.to be_blank }
|
99
|
+
|
100
|
+
context 'when description for method description exists' do
|
101
|
+
let(:method_description_description) { 'Some description' }
|
102
|
+
|
103
|
+
it { is_expected.to eq(method_description_description) }
|
104
|
+
end
|
105
|
+
end
|
106
|
+
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
|
23
|
+
property :an_optional_number, Integer, required: false
|
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'
|
60
|
+
},
|
61
|
+
an_optional_number: {
|
62
|
+
type: 'number'
|
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]
|
76
|
+
},
|
77
|
+
an_optional_number: {
|
78
|
+
type: %w[number null]
|
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
|
@@ -13,7 +13,7 @@ describe Apipie::Generator::Swagger::ParamDescription::Composite do
|
|
13
13
|
Apipie::Generator::Swagger::Context.new(
|
14
14
|
allow_null: true,
|
15
15
|
http_method: 'get',
|
16
|
-
controller_method:
|
16
|
+
controller_method: method_description
|
17
17
|
)
|
18
18
|
end
|
19
19
|
|
@@ -50,7 +50,7 @@ describe Apipie::Generator::Swagger::ParamDescription::Composite do
|
|
50
50
|
|
51
51
|
context 'when additional properties in response allowed' do
|
52
52
|
before do
|
53
|
-
Apipie.configuration.
|
53
|
+
Apipie.configuration.generator.swagger.allow_additional_properties_in_response = true
|
54
54
|
end
|
55
55
|
|
56
56
|
it { is_expected.to be_blank }
|