Skip to content

Commit

Permalink
Add 'summary' for further object compliance with OpenAPI v2 (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
aschuster3 authored and peter scholz committed May 23, 2016
1 parent 677714b commit dd4ba4a
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Metrics/AbcSize:
# Offense count: 3
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 201
Max: 206

# Offense count: 10
Metrics/CyclomaticComplexity:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#### Features

* [#434](https://github.com/ruby-grape/grape-swagger/pull/434): Add summary to the operation object generator to be more compliant with [OpenAPI v2](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#operation-object) - [@aschuster3](https://github.com/aschuster3).
* Your contribution here.

#### Fixes
Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,32 @@ end
```
#### Overriding the route summary
By default, the route summary is filled with the value supplied to `desc`.
```ruby
namespace 'order' do
desc 'This will be your summary'
get :order_id do
...
end
end
```
To override the summary, add `summary: '[string]'` after the description.
```ruby
namespace 'order' do
desc 'This will be your summary',
summary: 'Now this is your summary!'
get :order_id do
...
end
end
```
#### Expose nested namespace as standalone route
Use the `nested: false` property in the `swagger` option to make nested namespaces appear as standalone resources.
Expand Down
11 changes: 9 additions & 2 deletions lib/grape-swagger/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def path_item(routes, options)

def method_object(route, options, path)
method = {}
method[:summary] = summary_object(route)
method[:description] = description_object(route, options[:markdown])
method[:produces] = produces_object(route, options[:produces] || options[:format])
method[:consumes] = consumes_object(route, options[:format])
Expand All @@ -115,6 +116,14 @@ def method_object(route, options, path)
[route.request_method.downcase.to_sym, method]
end

def summary_object(route)
summary = route.options[:desc] if route.options.key?(:desc)
summary = route.description if route.description.present?
summary = route.options[:summary] if route.options.key?(:summary)

summary
end

def description_object(route, markdown)
description = route.options[:desc] if route.options.key?(:desc)
description = route.description if route.description.present?
Expand Down Expand Up @@ -237,9 +246,7 @@ def expose_params_from_model(model)

GrapeSwagger.model_parsers.each do |klass, ancestor|
next unless model.ancestors.map(&:to_s).include?(ancestor)

parser = klass.new(model, self)

break
end

Expand Down
4 changes: 4 additions & 0 deletions spec/issues/403_versions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def app
paths: {
:'/nothings' => {
get: {
summary: 'no versions given',
description: 'no versions given',
produces: ['application/json'],
responses: {
Expand Down Expand Up @@ -71,6 +72,7 @@ def app
paths: {
:'/v2/api_version' => {
get: {
summary: 'api versions given',
description: 'api versions given',
produces: ['application/json'],
responses: {
Expand Down Expand Up @@ -112,6 +114,7 @@ def app
paths: {
:'/doc_version' => {
get: {
summary: 'doc versions given',
description: 'doc versions given',
produces: ['application/json'],
responses: {
Expand Down Expand Up @@ -154,6 +157,7 @@ def app
paths: {
:'/v2/both_versions' => {
get: {
summary: 'both versions given',
description: 'both versions given',
produces: ['application/json'],
responses: {
Expand Down
8 changes: 8 additions & 0 deletions spec/support/model_parsers/entity_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ class RecursiveModel < Grape::Entity
'paths' => {
'/v3/other_thing/{elements}' => {
'get' => {
'summary' => 'nested route inside namespace',
'description' => 'nested route inside namespace',
'produces' => ['application/json'],
'parameters' => [{ 'in' => 'body', 'name' => 'elements', 'description' => 'Set of configuration', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => true }],
Expand All @@ -204,6 +205,7 @@ class RecursiveModel < Grape::Entity
},
'/thing' => {
'get' => {
'summary' => 'This gets Things.',
'description' => 'This gets Things.',
'produces' => ['application/json'],
'parameters' => [
Expand All @@ -217,6 +219,7 @@ class RecursiveModel < Grape::Entity
'operationId' => 'getThing'
},
'post' => {
'summary' => 'This creates Thing.',
'description' => 'This creates Thing.',
'produces' => ['application/json'],
'consumes' => ['application/json'],
Expand All @@ -231,6 +234,7 @@ class RecursiveModel < Grape::Entity
},
'/thing/{id}' => {
'get' => {
'summary' => 'This gets Thing.',
'description' => 'This gets Thing.',
'produces' => ['application/json'],
'parameters' => [{ 'in' => 'path', 'name' => 'id', 'type' => 'integer', 'format' => 'int32', 'required' => true }],
Expand All @@ -239,6 +243,7 @@ class RecursiveModel < Grape::Entity
'operationId' => 'getThingId'
},
'put' => {
'summary' => 'This updates Thing.',
'description' => 'This updates Thing.',
'produces' => ['application/json'],
'consumes' => ['application/json'],
Expand All @@ -252,6 +257,7 @@ class RecursiveModel < Grape::Entity
'operationId' => 'putThingId'
},
'delete' => {
'summary' => 'This deletes Thing.',
'description' => 'This deletes Thing.',
'produces' => ['application/json'],
'parameters' => [{ 'in' => 'path', 'name' => 'id', 'type' => 'integer', 'format' => 'int32', 'required' => true }],
Expand All @@ -262,6 +268,7 @@ class RecursiveModel < Grape::Entity
},
'/thing2' => {
'get' => {
'summary' => 'This gets Things.',
'description' => 'This gets Things.',
'produces' => ['application/json'],
'responses' => { '200' => { 'description' => 'get Horses', 'schema' => { '$ref' => '#/definitions/Something' } }, '401' => { 'description' => 'HorsesOutError', 'schema' => { '$ref' => '#/definitions/ApiError' } } },
Expand All @@ -271,6 +278,7 @@ class RecursiveModel < Grape::Entity
},
'/dummy/{id}' => {
'delete' => {
'summary' => 'dummy route.',
'description' => 'dummy route.',
'produces' => ['application/json'],
'parameters' => [{ 'in' => 'path', 'name' => 'id', 'type' => 'integer', 'format' => 'int32', 'required' => true }],
Expand Down
8 changes: 8 additions & 0 deletions spec/support/model_parsers/mock_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ class RecursiveModel < OpenStruct; end
'paths' => {
'/v3/other_thing/{elements}' => {
'get' => {
'summary' => 'nested route inside namespace',
'description' => 'nested route inside namespace',
'produces' => ['application/json'],
'parameters' => [{ 'in' => 'body', 'name' => 'elements', 'description' => 'Set of configuration', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => true }],
Expand All @@ -196,6 +197,7 @@ class RecursiveModel < OpenStruct; end
},
'/thing' => {
'get' => {
'summary' => 'This gets Things.',
'description' => 'This gets Things.',
'produces' => ['application/json'],
'parameters' => [
Expand All @@ -209,6 +211,7 @@ class RecursiveModel < OpenStruct; end
'operationId' => 'getThing'
},
'post' => {
'summary' => 'This creates Thing.',
'description' => 'This creates Thing.',
'produces' => ['application/json'],
'consumes' => ['application/json'],
Expand All @@ -223,6 +226,7 @@ class RecursiveModel < OpenStruct; end
},
'/thing/{id}' => {
'get' => {
'summary' => 'This gets Thing.',
'description' => 'This gets Thing.',
'produces' => ['application/json'],
'parameters' => [{ 'in' => 'path', 'name' => 'id', 'type' => 'integer', 'format' => 'int32', 'required' => true }],
Expand All @@ -231,6 +235,7 @@ class RecursiveModel < OpenStruct; end
'operationId' => 'getThingId'
},
'put' => {
'summary' => 'This updates Thing.',
'description' => 'This updates Thing.',
'produces' => ['application/json'],
'consumes' => ['application/json'],
Expand All @@ -244,6 +249,7 @@ class RecursiveModel < OpenStruct; end
'operationId' => 'putThingId'
},
'delete' => {
'summary' => 'This deletes Thing.',
'description' => 'This deletes Thing.',
'produces' => ['application/json'],
'parameters' => [{ 'in' => 'path', 'name' => 'id', 'type' => 'integer', 'format' => 'int32', 'required' => true }],
Expand All @@ -254,6 +260,7 @@ class RecursiveModel < OpenStruct; end
},
'/thing2' => {
'get' => {
'summary' => 'This gets Things.',
'description' => 'This gets Things.',
'produces' => ['application/json'],
'responses' => { '200' => { 'description' => 'get Horses', 'schema' => { '$ref' => '#/definitions/Something' } }, '401' => { 'description' => 'HorsesOutError', 'schema' => { '$ref' => '#/definitions/ApiError' } } },
Expand All @@ -263,6 +270,7 @@ class RecursiveModel < OpenStruct; end
},
'/dummy/{id}' => {
'delete' => {
'summary' => 'dummy route.',
'description' => 'dummy route.',
'produces' => ['application/json'],
'parameters' => [{ 'in' => 'path', 'name' => 'id', 'type' => 'integer', 'format' => 'int32', 'required' => true }],
Expand Down
8 changes: 8 additions & 0 deletions spec/support/model_parsers/representable_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ class RecursiveModel < Representable::Decorator
'paths' => {
'/v3/other_thing/{elements}' => {
'get' => {
'summary' => 'nested route inside namespace',
'description' => 'nested route inside namespace',
'produces' => ['application/json'],
'parameters' => [{ 'in' => 'body', 'name' => 'elements', 'description' => 'Set of configuration', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => true }],
Expand All @@ -273,6 +274,7 @@ class RecursiveModel < Representable::Decorator
},
'/thing' => {
'get' => {
'summary' => 'This gets Things.',
'description' => 'This gets Things.',
'produces' => ['application/json'],
'parameters' => [
Expand All @@ -286,6 +288,7 @@ class RecursiveModel < Representable::Decorator
'operationId' => 'getThing'
},
'post' => {
'summary' => 'This creates Thing.',
'description' => 'This creates Thing.',
'produces' => ['application/json'],
'consumes' => ['application/json'],
Expand All @@ -300,6 +303,7 @@ class RecursiveModel < Representable::Decorator
},
'/thing/{id}' => {
'get' => {
'summary' => 'This gets Thing.',
'description' => 'This gets Thing.',
'produces' => ['application/json'],
'parameters' => [{ 'in' => 'path', 'name' => 'id', 'type' => 'integer', 'format' => 'int32', 'required' => true }],
Expand All @@ -308,6 +312,7 @@ class RecursiveModel < Representable::Decorator
'operationId' => 'getThingId'
},
'put' => {
'summary' => 'This updates Thing.',
'description' => 'This updates Thing.',
'produces' => ['application/json'],
'consumes' => ['application/json'],
Expand All @@ -321,6 +326,7 @@ class RecursiveModel < Representable::Decorator
'operationId' => 'putThingId'
},
'delete' => {
'summary' => 'This deletes Thing.',
'description' => 'This deletes Thing.',
'produces' => ['application/json'],
'parameters' => [{ 'in' => 'path', 'name' => 'id', 'type' => 'integer', 'format' => 'int32', 'required' => true }],
Expand All @@ -331,6 +337,7 @@ class RecursiveModel < Representable::Decorator
},
'/thing2' => {
'get' => {
'summary' => 'This gets Things.',
'description' => 'This gets Things.',
'produces' => ['application/json'],
'responses' => { '200' => { 'description' => 'get Horses', 'schema' => { '$ref' => '#/definitions/Something' } }, '401' => { 'description' => 'HorsesOutError', 'schema' => { '$ref' => '#/definitions/ApiError' } } },
Expand All @@ -340,6 +347,7 @@ class RecursiveModel < Representable::Decorator
},
'/dummy/{id}' => {
'delete' => {
'summary' => 'dummy route.',
'description' => 'dummy route.',
'produces' => ['application/json'],
'parameters' => [{ 'in' => 'path', 'name' => 'id', 'type' => 'integer', 'format' => 'int32', 'required' => true }],
Expand Down
3 changes: 3 additions & 0 deletions spec/swagger_v2/api_swagger_v2_response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def app
end
specify do
expect(subject['paths']['/nested_type']['get']).to eql(
'summary' => 'This returns something',
'description' => 'This returns something',
'produces' => ['application/json'],
'responses' => {
Expand All @@ -66,6 +67,7 @@ def app

specify do
expect(subject['paths']['/entity_response']['get']).to eql(
'summary' => 'This returns something',
'description' => 'This returns something',
'produces' => ['application/json'],
'responses' => {
Expand All @@ -87,6 +89,7 @@ def app

specify do
expect(subject['paths']['/params_response']['post']).to eql(
'summary' => 'This returns something',
'description' => 'This returns something',
'produces' => ['application/json'],
'consumes' => ['application/json'],
Expand Down
2 changes: 2 additions & 0 deletions spec/swagger_v2/default_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def app
'paths' => {
'/something' => {
'get' => {
'summary' => 'This gets something.',
'description' => 'This gets something.',
'produces' => ['application/json'],
'tags' => ['something'],
Expand Down Expand Up @@ -75,6 +76,7 @@ def app
'paths' => {
'/something' => {
'get' => {
'summary' => 'This gets something.',
'description' => 'This gets something.',
'produces' => ['application/json'],
'tags' => ['something'],
Expand Down
4 changes: 4 additions & 0 deletions spec/swagger_v2/hide_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def app
'paths' => {
'/simple' => {
'get' => {
'summary' => 'Show this endpoint',
'description' => 'Show this endpoint',
'produces' => ['application/json'],
'tags' => ['simple'],
Expand All @@ -53,6 +54,7 @@ def app
},
'/lazy' => {
'get' => {
'summary' => 'Lazily show endpoint',
'description' => 'Lazily show endpoint',
'produces' => ['application/json'],
'tags' => ['lazy'],
Expand Down Expand Up @@ -105,6 +107,7 @@ def app
'paths' => {
'/simple/show' => {
'get' => {
'summary' => 'Show this endpoint',
'description' => 'Show this endpoint',
'produces' => ['application/json'],
'operationId' => 'getSimpleShow',
Expand All @@ -126,6 +129,7 @@ def app
'paths' => {
'/simple/show' => {
'get' => {
'summary' => 'Show this endpoint',
'description' => 'Show this endpoint',
'produces' => ['application/json'],
'tags' => ['simple'],
Expand Down
2 changes: 2 additions & 0 deletions spec/swagger_v2/mounted_target_class_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def app
'paths' => {
'/simple' => {
'get' => {
'summary' => 'This gets something.',
'description' => 'This gets something.',
'produces' => ['application/json'],
'responses' => { '200' => { 'description' => 'This gets something.' } },
Expand All @@ -59,6 +60,7 @@ def app
'paths' => {
'/simple' => {
'get' => {
'summary' => 'This gets something.',
'description' => 'This gets something.',
'produces' => ['application/json'],
'responses' => {
Expand Down
Loading

0 comments on commit dd4ba4a

Please sign in to comment.