Skip to content

Commit

Permalink
Fix generating error of response example when Content-Disposition hea…
Browse files Browse the repository at this point in the history
…der is inline
  • Loading branch information
kyoshidajp committed Apr 24, 2021
1 parent 37f3d44 commit 4e8ac00
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
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

0 comments on commit 4e8ac00

Please sign in to comment.