Skip to content

Skip empty parameters and tags arrays #731

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

Conversation

fotos
Copy link
Contributor

@fotos fotos commented Dec 7, 2018

Skip empty parameters and tags arrays by using Object#presence.

This removes the redundant (yet not harmful) parameters and tags and retains the empty security array which is useful (for overriding the globally applicable security schemes).

This PR is related and a follow up to #729.

For convenience the complete diff over the 2 PRs is (excluding the CHANGELOG):

diff --git a/lib/grape-swagger/endpoint.rb b/lib/grape-swagger/endpoint.rb
index 57764aa..79dd3b1 100644
--- a/lib/grape-swagger/endpoint.rb
+++ b/lib/grape-swagger/endpoint.rb
@@ -125,7 +125,7 @@ module Grape
       method[:tags]        = route.options.fetch(:tags, tag_object(route, path))
       method[:operationId] = GrapeSwagger::DocMethods::OperationId.build(route, path)
       method[:deprecated] = deprecated_object(route)
-      method.delete_if { |_, value| value.blank? }
+      method.delete_if { |_, value| value.nil? }

       [route.request_method.downcase.to_sym, method]
     end
@@ -149,7 +149,6 @@ module Grape
     def description_object(route)
       description = route.description if route.description.present?
       description = route.options[:detail] if route.options.key?(:detail)
-      description ||= ''

       description
     end
@@ -192,7 +191,7 @@ module Grape
         parameters = GrapeSwagger::DocMethods::MoveParams.to_definition(path, parameters, route, @definitions)
       end

-      parameters
+      parameters.presence
     end

     def response_object(route)
@@ -258,11 +257,12 @@ module Grape
     def tag_object(route, path)
       version = GrapeSwagger::DocMethods::Version.get(route)
       version = [version] unless version.is_a?(Array)
+
       Array(
         path.split('{')[0].split('/').reject(&:empty?).delete_if do |i|
           i == route.prefix.to_s || version.map(&:to_s).include?(i)
         end.first
-      )
+      ).presence
     end

     private
diff --git a/spec/swagger_v2/security_requirement_spec.rb b/spec/swagger_v2/security_requirement_spec.rb
index 9c106d1..e7adb72 100644
--- a/spec/swagger_v2/security_requirement_spec.rb
+++ b/spec/swagger_v2/security_requirement_spec.rb
@@ -10,7 +10,24 @@ describe 'security requirement on endpoint method' do
         { foo: 'bar' }
       end

-      add_swagger_documentation
+      desc 'Endpoint without security requirement', security: []
+      get '/without_security' do
+        { foo: 'bar' }
+      end
+
+      add_swagger_documentation(
+        security_definitions: {
+          petstore_auth: {
+            type: 'oauth2',
+            flow: 'implicit',
+            authorizationUrl: 'https://petstore.swagger.io/oauth/dialog',
+            scopes: {
+              'read:pets': 'read your pets',
+              'write:pets': 'modify pets in your account'
+            }
+          }
+        }
+      )
     end
   end

@@ -22,4 +39,8 @@ describe 'security requirement on endpoint method' do
   it 'defines the security requirement on the endpoint method' do
     expect(subject['paths']['/with_security']['get']['security']).to eql ['oauth_pets' => ['read:pets', 'write:pets']]
   end
+
+  it 'defines an empty security requirement on the endpoint method' do
+    expect(subject['paths']['/without_security']['get']['security']).to eql []
+  end
 end

Copy link
Member

@LeFnord LeFnord left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeap, makes sense … looks good to me

@LeFnord
Copy link
Member

LeFnord commented Dec 7, 2018

ok will möge it … it seems coveralls doesn't work as expected

@LeFnord LeFnord merged commit 5e2329e into ruby-grape:master Dec 7, 2018
@fotos fotos deleted the skip-empty-parameters-and-tags-arrays branch December 8, 2018 00:14
LeFnord pushed a commit to LeFnord/grape-swagger that referenced this pull request Feb 9, 2019
* Skip empty parameters arrays

* Skip empty tags arrays

* Update CHANGELOG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants