Skip to content

Commit e40cba8

Browse files
elasti-roniNecas
authored andcommitted
supporting specification of array properties with non-string types
1 parent 16b7cee commit e40cba8

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

lib/apipie/swagger_generator.rb

+12-1
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ def save_field(entry, openapi_key, v, apipie_key=openapi_key, translate=false)
456456
end
457457

458458
if swagger_def[:type] == "array"
459-
swagger_def[:items] = {type: "string"} # TODO: add support for arrays of non-string items
459+
swagger_def[:items] = {type: "string"}
460460
end
461461

462462
if swagger_def[:type] == "enum"
@@ -469,6 +469,17 @@ def save_field(entry, openapi_key, v, apipie_key=openapi_key, translate=false)
469469
warn_hash_without_internal_typespec(param_desc.name)
470470
end
471471

472+
if param_desc.is_array?
473+
new_swagger_def = {
474+
items: swagger_def,
475+
type: 'array'
476+
}
477+
swagger_def = new_swagger_def
478+
if allow_nulls
479+
swagger_def[:type] = [swagger_def[:type], "null"]
480+
end
481+
end
482+
472483
if allow_nulls
473484
swagger_def[:type] = [swagger_def[:type], "null"]
474485
end

spec/dummy/app/controllers/pets_controller.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ def get_vote_by_owner_name
140140
param_group :pet_history
141141
end
142142
end
143+
returns :code => 204 do
144+
property :int_array, :array_of => Integer
145+
property :enum_array, :array_of => ['v1','v2','v3']
146+
end
143147
returns :code => :unprocessable_entity, :desc => "Fleas were discovered on the pet" do
144148
param_group :pet
145149
property :num_fleas, Integer, :desc => "Number of fleas on this pet"
@@ -341,7 +345,6 @@ def sub_object_allowed_extra_property
341345
render :json => result
342346
end
343347

344-
345348
#=======================================================================
346349
# Methods for testing array field responses
347350
#=======================================================================

spec/lib/swagger/swagger_dsl_spec.rb

+18
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,24 @@ def swagger_param_by_name(param_name, path, method='get')
320320
expect(pai_schema).to have_field(:avg_meals_per_day, 'number')
321321
end
322322

323+
it "should return code 204 with array of integer" do
324+
returns_obj = subject.returns.detect{|e| e.code == 204 }
325+
326+
puts returns_obj.to_json
327+
expect(returns_obj.code).to eq(204)
328+
expect(returns_obj.is_array?).to eq(false)
329+
330+
expect(returns_obj).to match_field_structure([:int_array, :enum_array])
331+
end
332+
it 'should have the 204 response described in the swagger' do
333+
response = swagger_response_for('/pets/{id}/extra_info', 204)
334+
335+
schema = response[:schema]
336+
expect(schema).to have_field(:int_array, 'array', {items: {type: 'number'}})
337+
expect(schema).to have_field(:enum_array, 'array', {items: {type: 'string', enum: ['v1','v2','v3']}})
338+
end
339+
340+
323341
it "should return code matching :unprocessable_entity (422) with spread out 'pet' and 'num_fleas'" do
324342
returns_obj = subject.returns.detect{|e| e.code == 422 }
325343

spec/spec_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ def fail(msg)
118118
return fail("expected param '#{name}' to have type '#{type}' (got '#{prop[:type]}')") if prop[:type] != type
119119
return fail("expected param '#{name}' to have description '#{opts[:description]}' (got '#{prop[:description]}')") if opts[:description] && prop[:description] != opts[:description]
120120
return fail("expected param '#{name}' to have enum '#{opts[:enum]}' (got #{prop[:enum]})") if opts[:enum] && prop[:enum] != opts[:enum]
121+
return fail("expected param '#{name}' to have items '#{opts[:items]}' (got #{prop[:items]})") if opts[:items] && prop[:items] != opts[:items]
121122
if !opts.include?(:required) || opts[:required] == true
122123
return fail("expected param '#{name}' to be required") unless actual[:required].include?(name)
123124
else

0 commit comments

Comments
 (0)