Skip to content

Replace downcase with underscore for multi-word roots #764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* [#758](https://github.com/ruby-grape/grape-swagger/pull/758): Handle cases where a route's prefix is a nested URL - [@SimonKaluza](https://github.com/simonkaluza).
* [#757](https://github.com/ruby-grape/grape-swagger/pull/757): Fix `array_use_braces` for nested body params - [@bikolya](https://github.com/bikolya).
* [#756](https://github.com/ruby-grape/grape-swagger/pull/756): Fix reference creation when custom type for documentation is provided - [@bikolya](https://github.com/bikolya).
* [#764](https://github.com/ruby-grape/grape-swagger/pull/764): Fix root element for multi-word entities - [@bikolya](https://github.com/bikolya).

### 0.33.0 (June 21, 2019)

Expand Down
3 changes: 2 additions & 1 deletion lib/grape-swagger/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ def build_reference(route, value, response_model, settings)
end

def build_root(route, reference, response_model, settings)
default_root = route.options[:is_array] ? response_model.downcase.pluralize : response_model.downcase
default_root = response_model.underscore
default_root = default_root.pluralize if route.options[:is_array]
case route.settings.dig(:swagger, :root)
when true
{ type: 'object', properties: { default_root => reference } }
Expand Down
1 change: 0 additions & 1 deletion spec/lib/optional_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
let(:options) do
{ host: proc { |request| request.host =~ /^example/ ? '/api-example' : '/api' } }
end

specify do
expect(subject.build(key, options, request)).to eql '/api-example'
end
Expand Down
22 changes: 22 additions & 0 deletions spec/swagger_v2/api_swagger_v2_response_with_root_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ class ResponseApiWithRoot < Grape::API
{ 'declared_params' => declared(params) }
end

route_setting :swagger, root: true
desc 'This returns underscored root',
http_codes: [{ code: 200, model: Entities::ApiError }]
get '/response_with_root_underscore' do
{ 'declared_params' => declared(params) }
end

route_setting :swagger, root: true
desc 'This returns something',
is_array: true,
Expand Down Expand Up @@ -97,6 +104,21 @@ def app
end
end

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

it 'adds root to the response' do
schema = subject.dig('paths', '/response_with_root_underscore', 'get', 'responses', '200', 'schema')
expect(schema).to eq(
'type' => 'object',
'properties' => { 'api_error' => { '$ref' => '#/definitions/ApiError' } }
)
end
end

describe 'GET /response_with_array_and_root' do
subject do
get '/swagger_doc/response_with_array_and_root'
Expand Down