Skip to content

Commit 9f5d68c

Browse files
committed
Support non-string enum validators ...
... both for custom validators and self-describing properties. Before: ```ruby expect(pm_schema).to have_field(:num_tails, 'number', {:description => "Number of tails", :enum => [0, 1]}) # => expected param 'num_tails' to have type 'number' (got 'string') expect_param_def("get", "/users/by_department_number", "department", "type", "integer") # => expected: "integer" # => got: "string" ``` After: tests pass. This PR also adds support for subclassing of validators.
1 parent eeef4de commit 9f5d68c

File tree

8 files changed

+49
-5
lines changed

8 files changed

+49
-5
lines changed

lib/apipie/generator/swagger/param_description/type.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ def array_items_type(items_type)
9595

9696
def for_enum_type
9797
{
98-
type: 'string',
99-
enum: @param_description.validator.values
98+
type: validator.expected_type,
99+
enum: validator.values
100100
}
101101
end
102102

lib/apipie/validator.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ def inspect
2525
end
2626

2727
def self.inherited(subclass)
28+
BaseValidator.validators.unshift(subclass)
29+
end
30+
31+
def self.validators
2832
@validators ||= []
29-
@validators.insert 0, subclass
3033
end
3134

3235
# find the right validator for given options

spec/dummy/app/controllers/pets_using_self_describing_classes_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def self.describe_own_properties
3232
Apipie::prop(:weight, 'number', {:description => "Weight in pounds" }),
3333
Apipie::prop(:height, 'number', {:description => "Height in inches" }),
3434
Apipie::prop(:num_legs, 'number', {:description => "Number of legs", :required => false }),
35+
Apipie::prop(:num_tails, 'number', {:description => "Number of tails", :values => [0, 1] }),
3536
Apipie::additional_properties(false)
3637
])
3738
]

spec/dummy/app/controllers/users_controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,12 @@ def get_by_department
274274
render :plain => 'nothing to see here'
275275
end
276276

277+
api :GET, '/users/by_department_number', 'show users from a specific department by department number'
278+
param :department, [1, 2, 3], default_value: 1, type: 'integer'
279+
def get_by_department_number
280+
render :plain => 'nothing to see here'
281+
end
282+
277283
api :GET, '/users/in_departments', 'show users from specific departments'
278284
param :departments, Array, in: %w[finance operations sales marketing HR], default_value: ['sales']
279285
def get_in_departments

spec/lib/apipie/validator_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,17 @@
146146
expect(validator.description).to eq('Must be one of: <code>first</code>, <code>second &amp; third</code>.')
147147
end
148148
end
149+
150+
describe 'subclassing BaseValidator' do
151+
it 'adds the new validator to the list of validators' do
152+
subclass = Class.new(Apipie::Validator::BaseValidator)
153+
expect(Apipie::Validator::BaseValidator.validators).to include(subclass)
154+
end
155+
156+
it 'adds the new validator to the list of validators recursively' do
157+
subclass = Class.new(Apipie::Validator::BaseValidator)
158+
subsubclass = Class.new(subclass)
159+
expect(Apipie::Validator::BaseValidator.validators).to include(subsubclass)
160+
end
161+
end
149162
end

spec/lib/swagger/rake_swagger_spec.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ def expect_response_params_def(http_method, path, response_code, param_name, fie
108108
expect_param_def("get", "/users/by_department", "department", "enum",
109109
%w[finance operations sales marketing HR])
110110

111+
expect_param_def("get", "/users/by_department_number", "department", "type", "integer")
112+
expect_param_def("get", "/users/by_department_number", "department", "enum", [1, 2, 3])
113+
111114
expect_tags_def("get", "/twitter_example/{id}/followers", %w[twitter_example following index search])
112115
expect_response_params_def("get", "/pets/{id}/as_properties", 200, "pet_name", "example", "mypet")
113116
end
@@ -135,12 +138,15 @@ def expect_response_params_def(http_method, path, response_code, param_name, fie
135138
expect_param_def("get", "/users/by_department", "department", "enum",
136139
%w[finance operations sales marketing HR])
137140

141+
expect_param_def("get", "/users/by_department_number", "department", "type", "integer")
142+
expect_param_def("get", "/users/by_department_number", "department", "enum", [1, 2, 3])
143+
138144
expect_param_def("get", "/users/in_departments", "departments", "in", "query")
139145
expect_array_param_def("get", "/users/in_departments", "departments",
140146
%w[finance operations sales marketing HR])
141147

142148
expect_tags_def("get", "/twitter_example/{id}/followers", %w[twitter_example following index search])
143-
149+
expect_response_params_def("get", "/pets/{id}/as_properties", 200, "pet_name", "example", "mypet")
144150
end
145151

146152
it "generates a valid swagger file" do

spec/lib/swagger/swagger_dsl_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ def have_field?(field, expected_name, breadcrumb)
570570

571571
expect(returns_obj).to match_field_structure([:pet_name,
572572
:animal_type,
573-
{:pet_measurements => [:weight, :height, :num_legs]}
573+
{:pet_measurements => [:weight, :height, :num_legs, :num_tails]}
574574
])
575575
end
576576

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class CustomTypedEnumValidator < Apipie::Validator::EnumValidator
2+
attr_accessor :expected_type
3+
4+
def initialize(param_description, enum, type)
5+
super(param_description, enum)
6+
self.expected_type = type
7+
end
8+
9+
def self.build(param_description, argument, options, block)
10+
if argument.is_a?(Array) && options.to_h[:type]
11+
new(param_description, argument, options[:type])
12+
end
13+
end
14+
end

0 commit comments

Comments
 (0)