Use full entity name as a default (fixes #779) (#786)

* 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
8abddd5f2c/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.

* Update CHANGELOG

* Fix for old Ruby versions

* Skip Representable:: prefix just like Entity::

* Add upgrade entry and move changelog entry from fixes to features scope
This commit is contained in:
Valentine Kiselev 2020-04-20 11:58:37 +03:00 committed by GitHub
parent 077a27441f
commit 8f978dc8d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 17 additions and 14 deletions

View File

@ -5,7 +5,7 @@
* Your contribution here.
* [#785](https://github.com/ruby-grape/grape-swagger/pull/785): Add extensions for params - [@MaximeRDY](https://github.com/MaximeRDY).
* [#782](https://github.com/ruby-grape/grape-swagger/pull/782): Allow passing class name as string for rake task initializer - [@misdoro](https://github.com/misdoro).
* [#786](https://github.com/ruby-grape/grape-swagger/pull/786): Use full entity name as a default - [@mrexox](https://github.com/mrexox)
#### Fixes

View File

@ -1,5 +1,9 @@
## Upgrading Grape-swagger
### Upgrading to >= 1.1.0
Full class name is used for referencing entity by default (e.g. `A::B::C` instead of just `C`). `Entity` and `Entities` suffixes and prefixes are omitted (e.g. if entity name is `Entities::SomeScope::MyFavourite::Entity` only `SomeScope::MyFavourite` will be used).
### Upgrading to >= 0.26.1
The format can now be specified,

View File

@ -51,11 +51,11 @@ module GrapeSwagger
if model.methods(false).include?(:entity_name)
model.entity_name
elsif model.to_s.end_with?('::Entity', '::Entities')
model.to_s.split('::')[-2]
elsif model.respond_to?(:name)
model.name.demodulize.camelize
model.to_s.split('::')[0..-2].join('::')
elsif model.to_s.start_with?('Entity::', 'Entities::', 'Representable::')
model.to_s.split('::')[1..-1].join('::')
else
model.to_s.split('::').last
model.to_s
end
end

View File

@ -345,7 +345,6 @@ module Grape
raise GrapeSwagger::Errors::SwaggerSpec,
"Empty model #{model_name}, swagger 2.0 doesn't support empty definitions."
end
@definitions[model_name] = GrapeSwagger::DocMethods::BuildModelDefinition.build(model, properties, required)
model_name

View File

@ -35,5 +35,5 @@ describe '#427 nested entity given as string' do
JSON.parse(last_response.body)['definitions']
end
specify { expect(subject.keys).to include 'RoleEntity', 'WithoutRole' }
specify { expect(subject.keys).to include 'RoleEntity', 'Permission::WithoutRole' }
end

View File

@ -82,11 +82,11 @@ describe 'definition names' do
JSON.parse(last_response.body)['definitions']
end
specify { expect(subject).to include 'Class1' }
specify { expect(subject).to include 'Class2' }
specify { expect(subject).to include 'TestDefinition::DummyEntities::WithVeryLongName::AnotherGroupingModule::Class1' }
specify { expect(subject).to include 'TestDefinition::DummyEntities::WithVeryLongName::AnotherGroupingModule::Class2' }
specify { expect(subject).to include 'FooKlass' }
specify { expect(subject).to include 'FourthEntity' }
specify { expect(subject).to include 'FithEntity' }
specify { expect(subject).to include 'TestDefinition::DummyEntities::WithVeryLongName::AnotherGroupingModule::Class4::FourthEntity' }
specify { expect(subject).to include 'TestDefinition::DummyEntities::WithVeryLongName::AnotherGroupingModule::Class5::FithEntity' }
specify { expect(subject).to include 'BarKlass' }
specify { expect(subject).to include 'SeventhEntity' }
specify { expect(subject).to include 'TestDefinition::DummyEntities::WithVeryLongName::AnotherGroupingModule::Class7::SeventhEntity' }
end

View File

@ -189,7 +189,7 @@ describe 'setting of param type, such as `query`, `path`, `formData`, `body`, `h
'type' => 'object',
'properties' => {
'data' => {
'$ref' => '#/definitions/ApiResponse',
'$ref' => '#/definitions/NestedModule::ApiResponse',
'description' => 'request data'
}
},
@ -207,7 +207,7 @@ describe 'setting of param type, such as `query`, `path`, `formData`, `body`, `h
end
specify do
expect(subject['definitions']['ApiResponse']).not_to be_nil
expect(subject['definitions']['NestedModule::ApiResponse']).not_to be_nil
end
specify do