Skip to content
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

Add support hanami router #205

Merged
merged 13 commits into from
Apr 12, 2024
Merged

Conversation

AlexeyMatskevich
Copy link
Contributor

As part of this MR I added the ability to analyse the hanami router for proper documentation generation and to support generation of tags and summary similar to how it happens in rails.

I can also highlight the following changes:

  1. Framework-specific code is highlighted in extractor.
  2. Extractors are loaded only if there are corresponding dependencies in bundler.
  3. Framework-specific features such as Active Support for rails or dry for hanami can be used within extractors.
  4. Attempting to lay the architecture for creating similar extractors for any other framework.

Less significant changes:

  1. I tried to make openapi documentation for rails and hanami as identical as possible, so that it can be compared via diff and see where the generation differs.
  2. For hanami, a dry inflector is used to convert the strings

Now, it seems that in almost all cases the extractors for hanami and rails work identically, but I could be mistaken.

Copy link

codecov bot commented Apr 10, 2024

Codecov Report

Attention: Patch coverage is 97.70992% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 97.22%. Comparing base (a978adf) to head (aa5c8cf).

Files Patch % Lines
lib/rspec/openapi.rb 77.77% 2 Missing ⚠️
lib/rspec/openapi/extractors/rails.rb 96.77% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #205      +/-   ##
==========================================
+ Coverage   97.15%   97.22%   +0.06%     
==========================================
  Files          15       20       +5     
  Lines         528      612      +84     
  Branches      127      133       +6     
==========================================
+ Hits          513      595      +82     
- Misses         15       17       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -212,16 +236,11 @@ paths:
example:
items:
- secrets
'401':
Copy link
Owner

Choose a reason for hiding this comment

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

Hmm, why 401 responses are gone?
It looks like a regression.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have researched this issue, missed it unfortunately earlier. I think it's not a regression, it's just that this section of documentation in rails is manually added, maybe it's part of testing that checks that manually added documentation won't be removed automatically.

When I generated the hanami documentation I deleted the whole file and re-generated it, I will add this part manually if I understand the reason for the existence of this part correctly.

@@ -168,9 +167,31 @@ paths:
schema:
type: string
format: binary
"/images/{id}":
Copy link
Owner

Choose a reason for hiding this comment

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

Note to myself) Before this enhancement, /images/1 and /images/2 were separately generated, which indicates that Hanami's Routing mechanism is not honored.

@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'bundler/setup'
Copy link
Owner

@exoego exoego Apr 11, 2024

Choose a reason for hiding this comment

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

Is bundler gem always included in runtime?
If not, it should be added to add_dependency section:

spec.add_dependency 'actionpack', '>= 5.2.0'
spec.add_dependency 'rails-dom-testing'
spec.add_dependency 'rspec-core'

Since bundler is not so small (400KB+ since 2.4.3), I prefer not to include extra dependency.
I hope Ruby's native mechanism like defined?(Rails) && Rails.respond_to?(:application), defined?(Hanami) && Hanami.respond_to?(....) can be used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried not using bundler and using the standard methods you suggested, however at this point it seems due to the loading order, these constants don't exist yet.
If i try this:

module SharedHooks
  def self.find_extractor
    if defined?(Rails) && Rails.respond_to?(:application) && Rails.application
      require 'rspec/openapi/extractors/rails'

      RSpec::OpenAPI::Extractors::Rails
    elsif defined?(Hanami) && Hanami.respond_to?(:app) && Hanami.app?
      require 'rspec/openapi/extractors/hanami'

      RSpec::OpenAPI::Extractors::Hanami
      # elsif defined?(Roda)
      #   some Roda extractor
    else
      require 'rspec/openapi/extractors/rack'

      RSpec::OpenAPI::Extractors::Rack
    end
  end
end

it won't work for hanami because of the way I use to throw the inspector into the router.

Would be glad for help in this aspect

Copy link
Owner

Choose a reason for hiding this comment

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

How about

begin
   require 'hanami'
   require 'rspec/openapi/extractors/hanami'
rescue
   puts 'Hanami not detected'
end

begin
   require 'rails'
   require 'rspec/openapi/extractors/rails'
rescue
   puts 'Rails not detected'
end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

excellent solution

@exoego exoego added the enhancement New feature or request label Apr 11, 2024
lib/rspec/openapi/shared_hooks.rb Fixed Show fixed Hide fixed
lib/rspec/openapi/shared_hooks.rb Fixed Show fixed Hide fixed
lib/rspec/openapi/shared_hooks.rb Fixed Show fixed Hide fixed
lib/rspec/openapi/shared_hooks.rb Fixed Show fixed Hide fixed
@@ -0,0 +1,13 @@
module SharedHooks

Check notice

Code scanning / Rubocop

Document classes and non-namespace modules. Note

Style/Documentation: Missing top-level documentation comment for module SharedHooks.
@@ -0,0 +1,13 @@
module SharedHooks

Check notice

Code scanning / Rubocop

Add the frozen_string_literal comment to the top of files to help transition to frozen string literals by default. Note

Style/FrozenStringLiteralComment: Missing frozen string literal comment.
Copy link
Owner

@exoego exoego left a comment

Choose a reason for hiding this comment

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

LGTM
Thanks

@exoego exoego merged commit 12892b0 into exoego:master Apr 12, 2024
13 checks passed
@exoego exoego added this to the v0.17.0 milestone Apr 12, 2024
@exoego
Copy link
Owner

exoego commented Apr 12, 2024

This was released in v0.17.0

@AlexeyMatskevich AlexeyMatskevich deleted the hanami-router branch April 12, 2024 18:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants