diff --git a/lib/apipie/generator/swagger/param_description/type.rb b/lib/apipie/generator/swagger/param_description/type.rb index 37c799fac..b05c244b0 100644 --- a/lib/apipie/generator/swagger/param_description/type.rb +++ b/lib/apipie/generator/swagger/param_description/type.rb @@ -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 diff --git a/lib/apipie/validator.rb b/lib/apipie/validator.rb index a56f227ce..eda325597 100644 --- a/lib/apipie/validator.rb +++ b/lib/apipie/validator.rb @@ -25,8 +25,11 @@ def inspect end def self.inherited(subclass) + BaseValidator.validators.unshift(subclass) + end + + def self.validators @validators ||= [] - @validators.insert 0, subclass end # find the right validator for given options diff --git a/spec/dummy/app/controllers/pets_using_self_describing_classes_controller.rb b/spec/dummy/app/controllers/pets_using_self_describing_classes_controller.rb index ca4a085d0..4b07b7f30 100644 --- a/spec/dummy/app/controllers/pets_using_self_describing_classes_controller.rb +++ b/spec/dummy/app/controllers/pets_using_self_describing_classes_controller.rb @@ -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) ]) ] diff --git a/spec/dummy/app/controllers/users_controller.rb b/spec/dummy/app/controllers/users_controller.rb index ffaded02a..208e5b58b 100644 --- a/spec/dummy/app/controllers/users_controller.rb +++ b/spec/dummy/app/controllers/users_controller.rb @@ -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 diff --git a/spec/lib/apipie/validator_spec.rb b/spec/lib/apipie/validator_spec.rb index 10eca9b1e..97a618553 100644 --- a/spec/lib/apipie/validator_spec.rb +++ b/spec/lib/apipie/validator_spec.rb @@ -146,4 +146,22 @@ expect(validator.description).to eq('Must be one of: first, second & third.') 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 diff --git a/spec/lib/swagger/rake_swagger_spec.rb b/spec/lib/swagger/rake_swagger_spec.rb index 6817956cf..192eec202 100644 --- a/spec/lib/swagger/rake_swagger_spec.rb +++ b/spec/lib/swagger/rake_swagger_spec.rb @@ -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 @@ -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") end it "generates a valid swagger file" do diff --git a/spec/lib/swagger/swagger_dsl_spec.rb b/spec/lib/swagger/swagger_dsl_spec.rb index 6e6fd45c7..fb742a6f5 100644 --- a/spec/lib/swagger/swagger_dsl_spec.rb +++ b/spec/lib/swagger/swagger_dsl_spec.rb @@ -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 @@ -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 diff --git a/spec/support/custom_typed_enum_validator.rb b/spec/support/custom_typed_enum_validator.rb new file mode 100644 index 000000000..e1164aad8 --- /dev/null +++ b/spec/support/custom_typed_enum_validator.rb @@ -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