diff --git a/lib/rspec/openapi/schema_builder.rb b/lib/rspec/openapi/schema_builder.rb index a659cba6..3af57eac 100644 --- a/lib/rspec/openapi/schema_builder.rb +++ b/lib/rspec/openapi/schema_builder.rb @@ -125,7 +125,11 @@ def build_property(value, disposition: nil) case value when Array - property[:items] = build_property(value.first) + if value.empty? + property[:items] = {} # unknown + else + property[:items] = build_property(value.first) + end when Hash property[:properties] = {}.tap do |properties| value.each do |key, v| diff --git a/spec/rails/app/controllers/images_controller.rb b/spec/rails/app/controllers/images_controller.rb index d1678254..0a5324b3 100644 --- a/spec/rails/app/controllers/images_controller.rb +++ b/spec/rails/app/controllers/images_controller.rb @@ -4,4 +4,14 @@ def show QBoAAAAASUVORK5CYII='.unpack('m').first send_data png, type: 'image/png', disposition: 'inline' end + + def index + list = [ + { + 'name': 'file.png', + 'tags': [], # Keep this empty to check empty array is accepted + } + ] + render json: list + end end diff --git a/spec/rails/config/routes.rb b/spec/rails/config/routes.rb index 074cbbc2..0ba74542 100644 --- a/spec/rails/config/routes.rb +++ b/spec/rails/config/routes.rb @@ -3,7 +3,7 @@ defaults format: 'json' do resources :tables, only: [:index, :show, :create, :update, :destroy] - resources :images, only: [:show] + resources :images, only: [:index, :show] get '/test_block' => ->(_env) { [200, { 'Content-Type' => 'text/plain' }, ['A TEST']] } end diff --git a/spec/rails/doc/openapi.json b/spec/rails/doc/openapi.json index 24be9e89..08741938 100644 --- a/spec/rails/doc/openapi.json +++ b/spec/rails/doc/openapi.json @@ -652,6 +652,47 @@ } } } + }, + "/images": { + "get": { + "summary": "index", + "tags": [ + "Image" + ], + "responses": { + "200": { + "description": "can return an object with an attribute of empty array", + "content": { + "application/json": { + "schema": { + "type": "array", + "items": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "tags": { + "type": "array", + "items": { + } + } + } + } + }, + "example": [ + { + "name": "file.png", + "tags": [ + + ] + } + ] + } + } + } + } + } } } } \ No newline at end of file diff --git a/spec/rails/doc/openapi.yaml b/spec/rails/doc/openapi.yaml index e5f99bd0..d5b9a8be 100644 --- a/spec/rails/doc/openapi.yaml +++ b/spec/rails/doc/openapi.yaml @@ -93,7 +93,7 @@ paths: database: id: 2 name: production - null_sample: + null_sample: storage_size: 12.3 created_at: '2020-07-17T00:00:00+00:00' updated_at: '2020-07-17T00:00:00+00:00' @@ -169,7 +169,7 @@ paths: database: id: 2 name: production - null_sample: + null_sample: storage_size: 12.3 created_at: '2020-07-17T00:00:00+00:00' updated_at: '2020-07-17T00:00:00+00:00' @@ -222,7 +222,7 @@ paths: database: id: 2 name: production - null_sample: + null_sample: storage_size: 12.3 created_at: '2020-07-17T00:00:00+00:00' updated_at: '2020-07-17T00:00:00+00:00' @@ -314,7 +314,7 @@ paths: database: id: 2 name: production - null_sample: + null_sample: storage_size: 12.3 created_at: '2020-07-17T00:00:00+00:00' updated_at: '2020-07-17T00:00:00+00:00' @@ -366,7 +366,7 @@ paths: database: id: 2 name: production - null_sample: + null_sample: storage_size: 12.3 created_at: '2020-07-17T00:00:00+00:00' updated_at: '2020-07-17T00:00:00+00:00' @@ -426,3 +426,26 @@ paths: schema: type: string example: AN ENGINE TEST + "/images": + get: + summary: index + tags: + - Image + responses: + '200': + description: can return an object with an attribute of empty array + content: + application/json: + schema: + type: array + items: + type: object + properties: + name: + type: string + tags: + type: array + items: {} + example: + - name: file.png + tags: [] diff --git a/spec/requests/rails_spec.rb b/spec/requests/rails_spec.rb index f4ff9303..8d148252 100644 --- a/spec/requests/rails_spec.rb +++ b/spec/requests/rails_spec.rb @@ -116,6 +116,13 @@ expect(response.status).to eq(200) end end + + describe '#index' do + it 'can return an object with an attribute of empty array' do + get '/images' + expect(response.status).to eq(200) + end + end end RSpec.describe 'Extra routes', type: :request do