mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-22 22:06:43 +00:00
Support parameters provided at the 'path' level
This commit is contained in:
parent
de09df59e1
commit
4d675056c1
@ -37,8 +37,14 @@ module Rswag
|
|||||||
|
|
||||||
def parameter(attributes)
|
def parameter(attributes)
|
||||||
attributes[:required] = true if attributes[:in].to_sym == :path
|
attributes[:required] = true if attributes[:in].to_sym == :path
|
||||||
metadata[:operation][:parameters] ||= []
|
|
||||||
metadata[:operation][:parameters] << attributes
|
if metadata.has_key?(:operation)
|
||||||
|
metadata[:operation][:parameters] ||= []
|
||||||
|
metadata[:operation][:parameters] << attributes
|
||||||
|
else
|
||||||
|
metadata[:path_item][:parameters] ||= []
|
||||||
|
metadata[:path_item][:parameters] << attributes
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def response(code, description, &block)
|
def response(code, description, &block)
|
||||||
|
|||||||
@ -44,7 +44,13 @@ module Rswag
|
|||||||
private
|
private
|
||||||
|
|
||||||
def parameters_in(location)
|
def parameters_in(location)
|
||||||
(@api_metadata[:operation][:parameters] || [])
|
path_item_params = @api_metadata[:path_item][:parameters] || []
|
||||||
|
operation_params = @api_metadata[:operation][:parameters] || []
|
||||||
|
applicable_params = operation_params
|
||||||
|
.concat(path_item_params)
|
||||||
|
.uniq { |p| p[:name] } # operation params should override path_item params
|
||||||
|
|
||||||
|
applicable_params
|
||||||
.map { |p| p['$ref'] ? resolve_parameter(p['$ref']) : p } # resolve any references
|
.map { |p| p['$ref'] ? resolve_parameter(p['$ref']) : p } # resolve any references
|
||||||
.concat(resolve_api_key_parameters)
|
.concat(resolve_api_key_parameters)
|
||||||
.select { |p| p[:in] == location }
|
.select { |p| p[:in] == location }
|
||||||
|
|||||||
@ -49,18 +49,18 @@ module Rswag
|
|||||||
def metadata_to_swagger(metadata)
|
def metadata_to_swagger(metadata)
|
||||||
response_code = metadata[:response][:code]
|
response_code = metadata[:response][:code]
|
||||||
response = metadata[:response].reject { |k,v| k == :code }
|
response = metadata[:response].reject { |k,v| k == :code }
|
||||||
|
|
||||||
verb = metadata[:operation][:verb]
|
verb = metadata[:operation][:verb]
|
||||||
operation = metadata[:operation]
|
operation = metadata[:operation]
|
||||||
.reject { |k,v| k == :verb }
|
.reject { |k,v| k == :verb }
|
||||||
.merge(responses: { response_code => response })
|
.merge(responses: { response_code => response })
|
||||||
|
|
||||||
{
|
path_template = metadata[:path_item][:template]
|
||||||
paths: {
|
path_item = metadata[:path_item]
|
||||||
metadata[:path_item][:template] => {
|
.reject { |k,v| k == :template }
|
||||||
verb => operation
|
.merge(verb => operation)
|
||||||
}
|
|
||||||
}
|
{ paths: { path_template => path_item } }
|
||||||
}
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -87,10 +87,21 @@ module Rswag
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe '#parameter(attributes)' do
|
describe '#parameter(attributes)' do
|
||||||
let(:api_metadata) { { operation: {} } }
|
|
||||||
|
|
||||||
context 'always' do
|
context "when called at the 'path' level" do
|
||||||
before { subject.parameter(name: :blog, in: :body, schema: { type: 'object' }) }
|
before { subject.parameter(name: :blog, in: :body, schema: { type: 'object' }) }
|
||||||
|
let(:api_metadata) { { path_item: {} } } # i.e. operation not defined yet
|
||||||
|
|
||||||
|
it "adds to the 'path_item parameters' metadata" do
|
||||||
|
expect(api_metadata[:path_item][:parameters]).to match(
|
||||||
|
[ name: :blog, in: :body, schema: { type: 'object' } ]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when called at the 'operation' level" do
|
||||||
|
before { subject.parameter(name: :blog, in: :body, schema: { type: 'object' }) }
|
||||||
|
let(:api_metadata) { { path_item: {}, operation: {} } } # i.e. operation defined
|
||||||
|
|
||||||
it "adds to the 'operation parameters' metadata" do
|
it "adds to the 'operation parameters' metadata" do
|
||||||
expect(api_metadata[:operation][:parameters]).to match(
|
expect(api_metadata[:operation][:parameters]).to match(
|
||||||
@ -101,6 +112,7 @@ module Rswag
|
|||||||
|
|
||||||
context "'path' parameter" do
|
context "'path' parameter" do
|
||||||
before { subject.parameter(name: :id, in: :path) }
|
before { subject.parameter(name: :id, in: :path) }
|
||||||
|
let(:api_metadata) { { operation: {} } }
|
||||||
|
|
||||||
it "automatically sets the 'required' flag" do
|
it "automatically sets the 'required' flag" do
|
||||||
expect(api_metadata[:operation][:parameters]).to match(
|
expect(api_metadata[:operation][:parameters]).to match(
|
||||||
|
|||||||
@ -125,6 +125,17 @@ module Rswag
|
|||||||
expect(path).to eq('/foobar/blogs/1/comments/2')
|
expect(path).to eq('/foobar/blogs/1/comments/2')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "defined at the 'path' level" do
|
||||||
|
before do
|
||||||
|
api_metadata[:path_item][:parameters] = [ { name: :blog_id, in: :path } ]
|
||||||
|
api_metadata[:operation][:parameters] = [ { name: :id, in: :path } ]
|
||||||
|
end
|
||||||
|
|
||||||
|
it "builds path from parameters defined at path and operation levels" do
|
||||||
|
expect(path).to eq('/blogs/1/comments/2')
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#build_body(example)' do
|
describe '#build_body(example)' do
|
||||||
|
|||||||
@ -41,12 +41,13 @@ describe 'Blogs API', type: :request, swagger_doc: 'v1/swagger.json' do
|
|||||||
end
|
end
|
||||||
|
|
||||||
path '/blogs/{id}' do
|
path '/blogs/{id}' do
|
||||||
|
parameter name: :id, :in => :path, :type => :string
|
||||||
|
|
||||||
get 'Retrieves a blog' do
|
get 'Retrieves a blog' do
|
||||||
tags 'Blogs'
|
tags 'Blogs'
|
||||||
description 'Retrieves a specific blog by id'
|
description 'Retrieves a specific blog by id'
|
||||||
operationId 'getBlog'
|
operationId 'getBlog'
|
||||||
produces 'application/json'
|
produces 'application/json'
|
||||||
parameter name: :id, :in => :path, :type => :string
|
|
||||||
|
|
||||||
response '200', 'blog found' do
|
response '200', 'blog found' do
|
||||||
schema '$ref' => '#/definitions/blog'
|
schema '$ref' => '#/definitions/blog'
|
||||||
|
|||||||
@ -68,6 +68,14 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/blogs/{id}": {
|
"/blogs/{id}": {
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"name": "id",
|
||||||
|
"in": "path",
|
||||||
|
"type": "string",
|
||||||
|
"required": true
|
||||||
|
}
|
||||||
|
],
|
||||||
"get": {
|
"get": {
|
||||||
"summary": "Retrieves a blog",
|
"summary": "Retrieves a blog",
|
||||||
"tags": [
|
"tags": [
|
||||||
@ -78,14 +86,6 @@
|
|||||||
"produces": [
|
"produces": [
|
||||||
"application/json"
|
"application/json"
|
||||||
],
|
],
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"name": "id",
|
|
||||||
"in": "path",
|
|
||||||
"type": "string",
|
|
||||||
"required": true
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"responses": {
|
"responses": {
|
||||||
"200": {
|
"200": {
|
||||||
"description": "blog found",
|
"description": "blog found",
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user