Merge remote-tracking branch 'olegkyz/bugfix/examples-content' into jamie

This commit is contained in:
Jamie Macey 2020-10-17 13:14:47 -07:00
commit 7e2c52b242
6 changed files with 47 additions and 33 deletions

View File

@ -608,7 +608,11 @@ To enable examples generation from responses add callback above run_test! like:
```
after do |example|
example.metadata[:response][:examples] = { 'application/json' => JSON.parse(response.body, symbolize_names: true) }
example.metadata[:response][:content] = {
'application/json' => {
example: JSON.parse(response.body, symbolize_names: true)
}
}
end
```

View File

@ -19,7 +19,11 @@ RSpec.describe '<%= controller_path %>', type: :request do
<% end -%>
after do |example|
example.metadata[:response][:examples] = { 'application/json' => JSON.parse(response.body, symbolize_names: true) }
example.metadata[:response][:content] = {
'application/json' => {
example: JSON.parse(response.body, symbolize_names: true)
}
}
end
run_test!
end

View File

@ -72,7 +72,10 @@ module Rswag
def examples(example = nil)
return super() if example.nil?
metadata[:response][:examples] = example
metadata[:response][:content] =
example.each_with_object({}) do |(mime, example_object), memo|
memo[mime] = { example: example_object }
end
end
def run_test!(&block)

View File

@ -129,13 +129,13 @@ module Rswag
end
def upgrade_content!(mime_list, target_node)
target_node.merge!(content: {})
target_node[:content] ||= {}
schema = target_node[:schema]
return if mime_list.empty? || schema.nil?
mime_list.each do |mime_type|
# TODO upgrade to have content-type specific schema
target_node[:content][mime_type] = { schema: schema }
(target_node[:content][mime_type] ||= {}).merge!(schema: schema)
end
end

View File

@ -137,9 +137,10 @@ module Rswag
end
describe '#examples(example)' do
let(:mime) { 'application/json' }
let(:json_example) do
{
'application/json' => {
mime => {
foo: 'bar'
}
}
@ -151,7 +152,11 @@ module Rswag
end
it "adds to the 'response examples' metadata" do
expect(api_metadata[:response][:examples]).to eq(json_example)
expect(api_metadata[:response][:content]).to match(
mime => {
example: json_example[mime]
}
)
end
end
end

View File

@ -235,16 +235,14 @@
"type": "string"
}
},
"examples": {
"application/json": {
"id": 1,
"title": "Hello world!",
"content": "Hello world and hello universe. Thank you all very much!!!",
"thumbnail": "thumbnail.png"
}
},
"content": {
"application/json": {
"example": {
"id": 1,
"title": "Hello world!",
"content": "Hello world and hello universe. Thank you all very much!!!",
"thumbnail": "thumbnail.png"
},
"schema": {
"$ref": "#/definitions/blog"
}