Skip to content

Commit 40669ac

Browse files
committed
Replace downcase with underscore for multi-word roots
1 parent 294740b commit 40669ac

File tree

5 files changed

+25
-8
lines changed

5 files changed

+25
-8
lines changed

.rubocop_todo.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ Gemspec/RequiredRubyVersion:
1313
Exclude:
1414
- 'grape-swagger.gemspec'
1515

16-
# Offense count: 1
17-
# Cop supports --auto-correct.
18-
Lint/UnneededCopEnableDirective:
19-
Exclude:
20-
- 'spec/lib/optional_object_spec.rb'
21-
2216
# Offense count: 30
2317
Metrics/AbcSize:
2418
Max: 59

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* [#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).
1212
* [#757](https://github.com/ruby-grape/grape-swagger/pull/757): Fix `array_use_braces` for nested body params - [@bikolya](https://github.com/bikolya).
1313
* [#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).
14+
* [#764](https://github.com/ruby-grape/grape-swagger/pull/764): Fix root element for multi-word entities - [@bikolya](https://github.com/bikolya).
1415

1516
### 0.33.0 (June 21, 2019)
1617

lib/grape-swagger/endpoint.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,8 @@ def build_reference(route, value, response_model, settings)
271271
end
272272

273273
def build_root(route, reference, response_model, settings)
274-
default_root = route.options[:is_array] ? response_model.downcase.pluralize : response_model.downcase
274+
default_root = response_model.underscore
275+
default_root = default_root.pluralize if route.options[:is_array]
275276
case route.settings.dig(:swagger, :root)
276277
when true
277278
{ type: 'object', properties: { default_root => reference } }

spec/lib/optional_object_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
let(:options) do
4040
{ host: proc { |request| request.host =~ /^example/ ? '/api-example' : '/api' } }
4141
end
42-
# rubocop:enable RegexpMatch
4342
specify do
4443
expect(subject.build(key, options, request)).to eql '/api-example'
4544
end

spec/swagger_v2/api_swagger_v2_response_with_root_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ class ResponseApiWithRoot < Grape::API
3030
{ 'declared_params' => declared(params) }
3131
end
3232

33+
route_setting :swagger, root: true
34+
desc 'This returns underscored root',
35+
http_codes: [{ code: 200, model: Entities::ApiError }]
36+
get '/response_with_root_underscore' do
37+
{ 'declared_params' => declared(params) }
38+
end
39+
3340
route_setting :swagger, root: true
3441
desc 'This returns something',
3542
is_array: true,
@@ -97,6 +104,21 @@ def app
97104
end
98105
end
99106

107+
describe 'GET /response_with_root_underscore' do
108+
subject do
109+
get '/swagger_doc/response_with_root_underscore'
110+
JSON.parse(last_response.body)
111+
end
112+
113+
it 'adds root to the response' do
114+
schema = subject.dig('paths', '/response_with_root_underscore', 'get', 'responses', '200', 'schema')
115+
expect(schema).to eq(
116+
'type' => 'object',
117+
'properties' => { 'api_error' => { '$ref' => '#/definitions/ApiError' } }
118+
)
119+
end
120+
end
121+
100122
describe 'GET /response_with_array_and_root' do
101123
subject do
102124
get '/swagger_doc/response_with_array_and_root'

0 commit comments

Comments
 (0)