Skip to content

Commit d4299c9

Browse files
committed
Use full entity name as a default (fixes #779)
I just wanted to resolve this: ``` - model.to_s.split('::').last + model.to_s ``` But I met a problem that it receives Class type in here https://github.com/ruby-grape/grape-swagger/blob/8abddd5f2ce5fe29238990b136625f34371b0be0/lib/grape-swagger/endpoint.rb#L354 But String type in the another place, where the type is generated. So, splitting and taking the last actually returns the same as `name` method of the class. That is very strange I think. Also, if we, for example, use such constructions, we end up with just `V1` and `V2` fron the `A::B` module as they are parsed first: ```ruby module A module B class V1 < Grape::Entity ... class V2 < Grape::Entity ... module A module C class V1 < Grape::Entity ... class V2 < Grape::Entity ... ``` This PR fixes this issue.
1 parent 077a274 commit d4299c9

File tree

5 files changed

+12
-13
lines changed

5 files changed

+12
-13
lines changed

lib/grape-swagger/doc_methods/data_type.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ def parse_entity_name(model)
5151
if model.methods(false).include?(:entity_name)
5252
model.entity_name
5353
elsif model.to_s.end_with?('::Entity', '::Entities')
54-
model.to_s.split('::')[-2]
55-
elsif model.respond_to?(:name)
56-
model.name.demodulize.camelize
54+
model.to_s.split('::')[0..-2].join('::')
55+
elsif model.to_s.start_with?('Entity::', 'Entities::')
56+
model.to_s.split('::')[1..].join('::')
5757
else
58-
model.to_s.split('::').last
58+
model.to_s
5959
end
6060
end
6161

lib/grape-swagger/endpoint.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,6 @@ def expose_params_from_model(model)
345345
raise GrapeSwagger::Errors::SwaggerSpec,
346346
"Empty model #{model_name}, swagger 2.0 doesn't support empty definitions."
347347
end
348-
349348
@definitions[model_name] = GrapeSwagger::DocMethods::BuildModelDefinition.build(model, properties, required)
350349

351350
model_name

spec/issues/427_entity_as_string_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ class RoleEntity < Grape::Entity
3535
JSON.parse(last_response.body)['definitions']
3636
end
3737

38-
specify { expect(subject.keys).to include 'RoleEntity', 'WithoutRole' }
38+
specify { expect(subject.keys).to include 'RoleEntity', 'Permission::WithoutRole' }
3939
end

spec/issues/430_entity_definitions_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ class NameApi < Grape::API
8282
JSON.parse(last_response.body)['definitions']
8383
end
8484

85-
specify { expect(subject).to include 'Class1' }
86-
specify { expect(subject).to include 'Class2' }
85+
specify { expect(subject).to include 'TestDefinition::DummyEntities::WithVeryLongName::AnotherGroupingModule::Class1' }
86+
specify { expect(subject).to include 'TestDefinition::DummyEntities::WithVeryLongName::AnotherGroupingModule::Class2' }
8787
specify { expect(subject).to include 'FooKlass' }
88-
specify { expect(subject).to include 'FourthEntity' }
89-
specify { expect(subject).to include 'FithEntity' }
88+
specify { expect(subject).to include 'TestDefinition::DummyEntities::WithVeryLongName::AnotherGroupingModule::Class4::FourthEntity' }
89+
specify { expect(subject).to include 'TestDefinition::DummyEntities::WithVeryLongName::AnotherGroupingModule::Class5::FithEntity' }
9090
specify { expect(subject).to include 'BarKlass' }
91-
specify { expect(subject).to include 'SeventhEntity' }
91+
specify { expect(subject).to include 'TestDefinition::DummyEntities::WithVeryLongName::AnotherGroupingModule::Class7::SeventhEntity' }
9292
end

spec/swagger_v2/api_swagger_v2_param_type_body_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def app
189189
'type' => 'object',
190190
'properties' => {
191191
'data' => {
192-
'$ref' => '#/definitions/ApiResponse',
192+
'$ref' => '#/definitions/NestedModule::ApiResponse',
193193
'description' => 'request data'
194194
}
195195
},
@@ -207,7 +207,7 @@ def app
207207
end
208208

209209
specify do
210-
expect(subject['definitions']['ApiResponse']).not_to be_nil
210+
expect(subject['definitions']['NestedModule::ApiResponse']).not_to be_nil
211211
end
212212

213213
specify do

0 commit comments

Comments
 (0)