Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error of response example when Content-Disposition header is inline #24

Merged
merged 2 commits into from
Apr 24, 2021
Merged
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
1 change: 1 addition & 0 deletions lib/rspec/openapi/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
:status, # @param [Integer] - 200
:response_body, # @param [Object] - {"status" => "ok"}
:response_content_type, # @param [String] - "application/json"
:response_content_disposition, # @param [String] - "inline"
keyword_init: true,
)
1 change: 1 addition & 0 deletions lib/rspec/openapi/record_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def build(context, example:)
status: response.status,
response_body: response_body,
response_content_type: response.content_type,
response_content_disposition: response.header["Content-Disposition"],
).freeze
end

Expand Down
23 changes: 18 additions & 5 deletions lib/rspec/openapi/schema_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ def build(record)
}

if record.response_body
disposition = normalize_content_disposition(record.response_content_disposition)
response[:content] = {
normalize_content_type(record.response_content_type) => {
schema: build_property(record.response_body),
example: (record.response_body if example_enabled?),
schema: build_property(record.response_body, disposition: disposition),
example: response_example(record, disposition: disposition),
}.compact,
}
end
Expand All @@ -34,6 +35,12 @@ def build(record)

private

def response_example(record, disposition:)
return nil if !example_enabled? || disposition

record.response_body
end

def example_enabled?
RSpec::OpenAPI.enable_example
end
Expand Down Expand Up @@ -78,8 +85,8 @@ def build_request_body(record)
}
end

def build_property(value)
property = build_type(value)
def build_property(value, disposition: nil)
property = build_type(value, disposition)

case value
when Array
Expand All @@ -94,7 +101,9 @@ def build_property(value)
property
end

def build_type(value)
def build_type(value, disposition)
return { type: 'string', format: 'binary' } if disposition

case value
when String
{ type: 'string' }
Expand Down Expand Up @@ -143,4 +152,8 @@ def normalize_path(path)
def normalize_content_type(content_type)
content_type&.sub(/;.+\z/, '')
end

def normalize_content_disposition(content_disposition)
content_disposition&.sub(/;.+\z/, '')
end
end
7 changes: 7 additions & 0 deletions spec/rails/app/controllers/images_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ImagesController < ApplicationController
def show
png = 'iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAAAAADhZOFXAAAADklEQVQIW2P4DwUMlDEA98A/wTjP
QBoAAAAASUVORK5CYII='.unpack('m').first
send_data png, type: 'image/png', disposition: 'inline'
end
end
1 change: 1 addition & 0 deletions spec/rails/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Rails.application.routes.draw do
defaults format: 'json' do
resources :tables, only: [:index, :show, :create, :update, :destroy]
resources :images, only: [:show]
end
end
20 changes: 20 additions & 0 deletions spec/rails/doc/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,23 @@ paths:
type: string
example:
no_content: 'true'
"/images/{id}":
get:
summary: show
tags:
- Image
parameters:
- name: id
in: path
required: true
schema:
type: integer
example: 1
responses:
'200':
description: returns a image payload
content:
image/png:
schema:
type: string
format: binary
9 changes: 9 additions & 0 deletions spec/requests/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,12 @@
end
end
end

RSpec.describe 'Images', type: :request do
describe '#payload' do
it 'returns a image payload' do
get '/images/1'
expect(response.status).to eq(200)
end
end
end