Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/apipie/generator/swagger/param_description/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ def array_items_type(items_type)

def for_enum_type
{
type: 'string',
enum: @param_description.validator.values
type: validator.expected_type,
enum: validator.values
}
end

Expand Down
5 changes: 4 additions & 1 deletion lib/apipie/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ def inspect
end

def self.inherited(subclass)
BaseValidator.validators.unshift(subclass)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously when subclassing a concrete validator, e.g. EnumValidator, this method would init the @validators ivar on EnumValidator and insert the new subclass there.

By explicitly referencing BaseValidator, the new class is added to the correct list.

This change shouldn't affect backwards compatibility. Worst case if anyone subclassed existing validators, they probably did BaseValidator.inherited(my_validator) or so, in which case this fix would cause my_validator to appear in the list twice.

end

def self.validators
@validators ||= []
@validators.insert 0, subclass
end

# find the right validator for given options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def self.describe_own_properties
Apipie::prop(:weight, 'number', {:description => "Weight in pounds" }),
Apipie::prop(:height, 'number', {:description => "Height in inches" }),
Apipie::prop(:num_legs, 'number', {:description => "Number of legs", :required => false }),
Apipie::prop(:num_tails, 'number', {:description => "Number of tails", :values => [0, 1] }),
Apipie::additional_properties(false)
])
]
Expand Down
6 changes: 6 additions & 0 deletions spec/dummy/app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,12 @@ def get_by_department
render :plain => 'nothing to see here'
end

api :GET, '/users/by_department_number', 'show users from a specific department by department number'
param :department, [1, 2, 3], default_value: 1, type: 'integer'
def get_by_department_number
render :plain => 'nothing to see here'
end

api :GET, '/users/in_departments', 'show users from specific departments'
param :departments, Array, in: %w[finance operations sales marketing HR], default_value: ['sales']
def get_in_departments
Expand Down
18 changes: 18 additions & 0 deletions spec/lib/apipie/validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,22 @@
expect(validator.description).to eq('Must be one of: <code>first</code>, <code>second &amp; third</code>.')
end
end

describe 'subclassing BaseValidator' do
it 'adds the new validator to the list of validators' do
subclass = Class.new(Apipie::Validator::BaseValidator)
expect(Apipie::Validator::BaseValidator.validators).to include(subclass)
# clean up
Apipie::Validator::BaseValidator.validators.delete(subclass)
end

it 'adds the new validator to the list of validators recursively' do
subclass = Class.new(Apipie::Validator::BaseValidator)
subsubclass = Class.new(subclass)
expect(Apipie::Validator::BaseValidator.validators).to include(subsubclass)
# clean up
Apipie::Validator::BaseValidator.validators.delete(subclass)
Apipie::Validator::BaseValidator.validators.delete(subsubclass)
end
end
end
8 changes: 7 additions & 1 deletion spec/lib/swagger/rake_swagger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ def expect_response_params_def(http_method, path, response_code, param_name, fie
expect_param_def("get", "/users/by_department", "department", "enum",
%w[finance operations sales marketing HR])

expect_param_def("get", "/users/by_department_number", "department", "type", "integer")
expect_param_def("get", "/users/by_department_number", "department", "enum", [1, 2, 3])

expect_tags_def("get", "/twitter_example/{id}/followers", %w[twitter_example following index search])
expect_response_params_def("get", "/pets/{id}/as_properties", 200, "pet_name", "example", "mypet")
end
Expand Down Expand Up @@ -135,12 +138,15 @@ def expect_response_params_def(http_method, path, response_code, param_name, fie
expect_param_def("get", "/users/by_department", "department", "enum",
%w[finance operations sales marketing HR])

expect_param_def("get", "/users/by_department_number", "department", "type", "integer")
expect_param_def("get", "/users/by_department_number", "department", "enum", [1, 2, 3])

expect_param_def("get", "/users/in_departments", "departments", "in", "query")
expect_array_param_def("get", "/users/in_departments", "departments",
%w[finance operations sales marketing HR])

expect_tags_def("get", "/twitter_example/{id}/followers", %w[twitter_example following index search])

expect_response_params_def("get", "/pets/{id}/as_properties", 200, "pet_name", "example", "mypet")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this added line is not related to this PR. i've just copied it from the other changed example above because it looks as if the two examples were supposed to mirror each other.

end

it "generates a valid swagger file" do
Expand Down
3 changes: 2 additions & 1 deletion spec/lib/swagger/swagger_dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ def have_field?(field, expected_name, breadcrumb)

expect(returns_obj).to match_field_structure([:pet_name,
:animal_type,
{:pet_measurements => [:weight, :height, :num_legs]}
{:pet_measurements => [:weight, :height, :num_legs, :num_tails]}
])
end

Expand All @@ -587,6 +587,7 @@ def have_field?(field, expected_name, breadcrumb)
expect(pm_schema).to have_field(:weight, 'number', {:description => "Weight in pounds"})
expect(pm_schema).to have_field(:height, 'number', {:description => "Height in inches"})
expect(pm_schema).to have_field(:num_legs, 'number', {:description => "Number of legs", :required => false})
expect(pm_schema).to have_field(:num_tails, 'number', {:description => "Number of tails", :enum => [0, 1]})
end
end

Expand Down
14 changes: 14 additions & 0 deletions spec/support/custom_typed_enum_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CustomTypedEnumValidator < Apipie::Validator::EnumValidator
attr_accessor :expected_type

def initialize(param_description, enum, type)
super(param_description, enum)
self.expected_type = type
end

def self.build(param_description, argument, options, block)
if argument.is_a?(Array) && options.to_h[:type]
new(param_description, argument, options[:type])
end
end
end