mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-23 06:16:42 +00:00
Merge remote-tracking branch 'olegkyz/bugfix/examples-content' into jamie
This commit is contained in:
commit
7e2c52b242
@ -608,7 +608,11 @@ To enable examples generation from responses add callback above run_test! like:
|
|||||||
|
|
||||||
```
|
```
|
||||||
after do |example|
|
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
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@ -19,7 +19,11 @@ RSpec.describe '<%= controller_path %>', type: :request do
|
|||||||
<% end -%>
|
<% end -%>
|
||||||
|
|
||||||
after do |example|
|
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
|
end
|
||||||
run_test!
|
run_test!
|
||||||
end
|
end
|
||||||
|
|||||||
@ -72,7 +72,10 @@ module Rswag
|
|||||||
def examples(example = nil)
|
def examples(example = nil)
|
||||||
return super() if 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
|
end
|
||||||
|
|
||||||
def run_test!(&block)
|
def run_test!(&block)
|
||||||
|
|||||||
@ -129,13 +129,13 @@ module Rswag
|
|||||||
end
|
end
|
||||||
|
|
||||||
def upgrade_content!(mime_list, target_node)
|
def upgrade_content!(mime_list, target_node)
|
||||||
target_node.merge!(content: {})
|
target_node[:content] ||= {}
|
||||||
schema = target_node[:schema]
|
schema = target_node[:schema]
|
||||||
return if mime_list.empty? || schema.nil?
|
return if mime_list.empty? || schema.nil?
|
||||||
|
|
||||||
mime_list.each do |mime_type|
|
mime_list.each do |mime_type|
|
||||||
# TODO upgrade to have content-type specific schema
|
# 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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -137,9 +137,10 @@ module Rswag
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe '#examples(example)' do
|
describe '#examples(example)' do
|
||||||
|
let(:mime) { 'application/json' }
|
||||||
let(:json_example) do
|
let(:json_example) do
|
||||||
{
|
{
|
||||||
'application/json' => {
|
mime => {
|
||||||
foo: 'bar'
|
foo: 'bar'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -151,7 +152,11 @@ module Rswag
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "adds to the 'response examples' metadata" do
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -235,16 +235,14 @@
|
|||||||
"type": "string"
|
"type": "string"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"examples": {
|
"content": {
|
||||||
"application/json": {
|
"application/json": {
|
||||||
|
"example": {
|
||||||
"id": 1,
|
"id": 1,
|
||||||
"title": "Hello world!",
|
"title": "Hello world!",
|
||||||
"content": "Hello world and hello universe. Thank you all very much!!!",
|
"content": "Hello world and hello universe. Thank you all very much!!!",
|
||||||
"thumbnail": "thumbnail.png"
|
"thumbnail": "thumbnail.png"
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"content": {
|
|
||||||
"application/json": {
|
|
||||||
"schema": {
|
"schema": {
|
||||||
"$ref": "#/definitions/blog"
|
"$ref": "#/definitions/blog"
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user