Skip to content

Commit

Permalink
fix: Grape::Endpoint#merge_params now returns correct object (#943)
Browse files Browse the repository at this point in the history
  • Loading branch information
4ndv authored and dblock committed Oct 18, 2024
1 parent 7ec7bed commit fddae76
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#### Fixes

* [#943](https://github.com/ruby-grape/grape-swagger/pull/943): Fix route_param documentation and type - [@4ndv](https://github.com/4ndv)
* Your contribution here.


Expand Down
2 changes: 1 addition & 1 deletion lib/grape-swagger/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def merge_params(route)
route_params[key] = path.merge(params)
end

route.params.delete_if { |key| key.is_a?(String) && param_keys.include?(key.to_sym) }.to_a
route_params.delete_if { |key| key.is_a?(String) && param_keys.include?(key.to_sym) }.to_a
end

# Iterates over namespaces recursively
Expand Down
68 changes: 68 additions & 0 deletions spec/issues/942_route_params.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# frozen_string_literal: true

require 'spec_helper'

describe '#942 route param documentation' do
let(:documentation) { { format: 'uuid' } }

let(:app) do
docs = documentation

another_app = Class.new(Grape::API) do
get '/list' do
[]
end
end

Class.new(Grape::API) do
route_param :account_id, type: String, desc: 'id of account', documentation: docs do
mount another_app

get '/another-list' do
[]
end
end

add_swagger_documentation
end
end

subject do
get '/swagger_doc'
JSON.parse(last_response.body)
end

context 'when documenting route_param of mounted endpoint' do
let(:parameters) { subject['paths']['/{account_id}/list']['get']['parameters'] }

specify do
account_id_param = parameters.find { |param| param['name'] == 'account_id' }
expect(account_id_param['type']).to eq 'string'
expect(account_id_param['format']).to eq 'uuid'
expect(account_id_param['description']).to eq 'id of account'
end
end

context 'when documenting route_param of nested endpoint' do
let(:parameters) { subject['paths']['/{account_id}/another-list']['get']['parameters'] }

specify do
account_id_param = parameters.find { |param| param['name'] == 'account_id' }
expect(account_id_param['type']).to eq 'string'
expect(account_id_param['format']).to eq 'uuid'
expect(account_id_param['description']).to eq 'id of account'
end
end

context 'when documentation overrides description' do
let(:documentation) { { desc: 'another description' } }

let(:parameters) { subject['paths']['/{account_id}/list']['get']['parameters'] }

specify do
account_id_param = parameters.find { |param| param['name'] == 'account_id' }
expect(account_id_param['type']).to eq 'string'
expect(account_id_param['description']).to eq 'another description'
end
end
end

0 comments on commit fddae76

Please sign in to comment.