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

Hanami framework support #176

Closed
AlexeyMatskevich opened this issue Feb 29, 2024 · 10 comments
Closed

Hanami framework support #176

AlexeyMatskevich opened this issue Feb 29, 2024 · 10 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@AlexeyMatskevich
Copy link
Contributor

After updating the dependencies, I noticed that the parsed_body

def safe_parse_body(response)
response.parsed_body
rescue JSON::ParserError
nil
end
uses new code from action_dispatch/testing/request_encoder.rb.
https://github.com/rails/rails/blob/main/actionpack/lib/action_dispatch/testing/request_encoder.rb#L57

register_encoder :html, response_parser: -> body { Rails::Dom::Testing.html_document.parse(body) }

Since rspec-openapi depends on actionpack this leads to dependency on rails regardless of the rails-dom-testing installation.

My project uses Roda and Hanami and no rails.

Failures:

  1) #create-account NOT create new account
     Failure/Error: example.run

     NameError:
       uninitialized constant ActionDispatch::RequestEncoder::Rails
     # /usr/local/bundle/gems/actionpack-7.1.3.2/lib/action_dispatch/testing/request_encoder.rb:55:in `block in <class:RequestEncoder>'
     # /usr/local/bundle/gems/actionpack-7.1.3.2/lib/action_dispatch/testing/test_response.rb:50:in `parsed_body'
     # /usr/local/bundle/bundler/gems/rspec-openapi-5469c81f4671/lib/rspec/openapi/record_builder.rb:44:in `safe_parse_body'
     # /usr/local/bundle/bundler/gems/rspec-openapi-5469c81f4671/lib/rspec/openapi/record_builder.rb:34:in `build'
     # /usr/local/bundle/bundler/gems/rspec-openapi-5469c81f4671/lib/rspec/openapi/rspec_hooks.rb:8:in `block in <top (required)>'
     # ./spec/support/database_cleaner.rb:23:in `block (4 levels) in <top (required)>'
     # /usr/local/bundle/gems/database_cleaner-core-2.0.1/lib/database_cleaner/strategy.rb:30:in `cleaning'
     # ./spec/support/database_cleaner.rb:22:in `block (3 levels) in <top (required)>'
     # /usr/local/bundle/gems/database_cleaner-core-2.0.1/lib/database_cleaner/strategy.rb:30:in `cleaning'
     # ./spec/support/database_cleaner.rb:21:in `block (2 levels) in <top (required)>'
     # /usr/local/bundle/gems/webmock-3.23.0/lib/webmock/rspec.rb:39:in `block (2 levels) in <top (required)>'
@exoego
Copy link
Owner

exoego commented Feb 29, 2024

This gem is tested only with Rails framework now.
I have no bandwidth to implement Hanami framework support, but happy to review and release it if someone volunteers for it.

According to git history, parsed_body has been added years ago.
IWhat was a workable rspec-openapi version in your project?
It might help contributors to understand how record_builder was implemented in the workable version, without depending Rails.

@exoego exoego added enhancement New feature or request help wanted Extra attention is needed labels Feb 29, 2024
@AlexeyMatskevich
Copy link
Contributor Author

Thanks for the reply, I have previously created a similar issue #5 so I am familiar with the fact that the library is only tested with rails. I was using 0.8.1 before, but I guess the error is related to the actionpack version update, and essentially the problem is on their side.

@exoego
Copy link
Owner

exoego commented Feb 29, 2024

Hmm, so, IIUC, a quick fix is to declare spec.add_dependency 'rails-dom-testing' in gemspec in this repo.

@AlexeyMatskevich
Copy link
Contributor Author

@exoego
Copy link
Owner

exoego commented Feb 29, 2024

@AlexeyMatskevich oops. Gemfile needs the fix too

@AlexeyMatskevich
Copy link
Contributor Author

Thanks, fixed that, but the result is the same

GIT
  remote: https://github.com/AlexeyMatskevich/rspec-openapi
  revision: 9d12c4657ce5b5b8d414007e7e30f0317d6dc160
  branch: AlexeyMatskevich-rails-dom-testing
  specs:
    rspec-openapi (0.12.0)
      actionpack (>= 5.2.0)
      rails-dom-testing
      rspec-core
     Failure/Error: response.parsed_body

     NameError:
       uninitialized constant ActionDispatch::RequestEncoder::Rails
     # /usr/local/bundle/gems/actionpack-7.1.3.2/lib/action_dispatch/testing/request_encoder.rb:55:in `block in <class:RequestEncoder>'
     # /usr/local/bundle/gems/actionpack-7.1.3.2/lib/action_dispatch/testing/test_response.rb:50:in `parsed_body'

@AlexeyMatskevich
Copy link
Contributor Author

Overall, I'm in no hurry as everything breaks on version 12, so I'm rolling back. All put requests, all 404 responses and more are gone.

@exoego
Copy link
Owner

exoego commented Mar 1, 2024

I've looked this using Roda without installing Rails
actionpack v7.1.3.2 depends on rails-dom-testing

gemfile.lock
GIT
  remote: https://github.com/exoego/simplecov.git
  revision: 1c364fe50dca0d19be754b917d846ce0ed0e2daa
  branch: branch-fix
  specs:
    simplecov (0.22.0)
      docile (~> 1.1)
      simplecov-html (~> 0.11)
      simplecov_json_formatter (~> 0.1)

PATH
  remote: .
  specs:
    rspec-openapi (0.12.0)
      actionpack (>= 5.2.0)
      rspec-core

GEM
  remote: https://rubygems.org/
  specs:
    actionpack (7.1.3.2)
      actionview (= 7.1.3.2)
      activesupport (= 7.1.3.2)
      nokogiri (>= 1.8.5)
      racc
      rack (>= 2.2.4)
      rack-session (>= 1.0.1)
      rack-test (>= 0.6.3)
      rails-dom-testing (~> 2.2)
      rails-html-sanitizer (~> 1.6)
    actionview (7.1.3.2)
      activesupport (= 7.1.3.2)
      builder (~> 3.1)
      erubi (~> 1.11)
      rails-dom-testing (~> 2.2)
      rails-html-sanitizer (~> 1.6)
    activesupport (7.1.3.2)
      base64
      bigdecimal
      concurrent-ruby (~> 1.0, >= 1.0.2)
      connection_pool (>= 2.2.5)
      drb
      i18n (>= 1.6, < 2)
      minitest (>= 5.1)
      mutex_m
      tzinfo (~> 2.0)
    ast (2.4.2)
    attr_extras (7.1.0)
    base64 (0.2.0)
    bigdecimal (3.1.6)
    builder (3.2.4)
    code-scanning-rubocop (0.6.1)
      rubocop (~> 1.0)
    coderay (1.1.3)
    concurrent-ruby (1.2.3)
    connection_pool (2.4.1)
    crass (1.0.6)
    diff-lcs (1.5.0)
    docile (1.4.0)
    drb (2.2.1)
    erubi (1.12.0)
    i18n (1.14.1)
      concurrent-ruby (~> 1.0)
    json (2.6.3)
    loofah (2.22.0)
      crass (~> 1.0.2)
      nokogiri (>= 1.12.0)
    method_source (1.0.0)
    minitest (5.22.2)
    mutex_m (0.2.0)
    nokogiri (1.16.2-arm64-darwin)
      racc (~> 1.4)
    optimist (3.0.1)
    parallel (1.23.0)
    parser (3.2.2.0)
      ast (~> 2.4.1)
    patience_diff (1.2.0)
      optimist (~> 3.0)
    pry (0.14.2)
      coderay (~> 1.1)
      method_source (~> 1.0)
    racc (1.7.3)
    rack (3.0.9.1)
    rack-session (2.0.0)
      rack (>= 3.0.0)
    rack-test (2.1.0)
      rack (>= 1.3)
    rails-dom-testing (2.2.0)
      activesupport (>= 5.0.0)
      minitest
      nokogiri (>= 1.6)
    rails-html-sanitizer (1.6.0)
      loofah (~> 2.21)
      nokogiri (~> 1.14)
    rainbow (3.1.1)
    regexp_parser (2.8.0)
    rexml (3.2.5)
    roda (3.67.0)
      rack
    rspec (3.12.0)
      rspec-core (~> 3.12.0)
      rspec-expectations (~> 3.12.0)
      rspec-mocks (~> 3.12.0)
    rspec-core (3.12.2)
      rspec-support (~> 3.12.0)
    rspec-expectations (3.12.4)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.12.0)
    rspec-mocks (3.12.7)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.12.0)
    rspec-support (3.12.0)
    rubocop (1.50.2)
      json (~> 2.3)
      parallel (~> 1.10)
      parser (>= 3.2.0.0)
      rainbow (>= 2.2.2, < 4.0)
      regexp_parser (>= 1.8, < 3.0)
      rexml (>= 3.2.5, < 4.0)
      rubocop-ast (>= 1.28.0, < 2.0)
      ruby-progressbar (~> 1.7)
      unicode-display_width (>= 2.4.0, < 3.0)
    rubocop-ast (1.28.0)
      parser (>= 3.2.1.0)
    ruby-progressbar (1.13.0)
    simplecov-cobertura (2.1.0)
      rexml
      simplecov (~> 0.19)
    simplecov-html (0.12.3)
    simplecov_json_formatter (0.1.4)
    super_diff (0.10.0)
      attr_extras (>= 6.2.4)
      diff-lcs
      patience_diff
    tzinfo (2.0.6)
      concurrent-ruby (~> 1.0)
    unicode-display_width (2.4.2)

PLATFORMS
  arm64-darwin-22

DEPENDENCIES
  code-scanning-rubocop
  pry
  roda
  rspec
  rspec-openapi!
  simplecov!
  simplecov-cobertura
  super_diff

BUNDLED WITH
   2.4.6

response.parsed_body worked successfully on my end.
I change status to "can't repro" for my future refencese.
If this still happens on your end, minimum repro helps me understand the issue.

@exoego exoego closed this as not planned Won't fix, can't repro, duplicate, stale Mar 1, 2024
@exoego exoego added cant-repro and removed enhancement New feature or request help wanted Extra attention is needed labels Mar 1, 2024
@exoego
Copy link
Owner

exoego commented Mar 1, 2024

I think here is the cause.

def extract_request_response(context)
if rack_test?(context)
request = ActionDispatch::Request.new(context.last_request.env)
request.body.rewind if request.body.respond_to?(:rewind)
response = ActionDispatch::TestResponse.new(*context.last_response.to_a)
else
request = context.request
response = context.response
end
[request, response]
end

rspec-openapi converts request and response into ActionDispatch::Request and ActionDispatch::TestReponse.
TestResponse has parsed_body.
However, in case of Hanami, probably context.request is not an ActionDispatch::TestResponse that causes fails.

If one wants to support Hanami in rspec-openapi, I think this part needs update.

@exoego exoego reopened this Mar 1, 2024
@exoego exoego changed the title Implicit dependence on rails on 0.12.0 Hanami framework support Mar 1, 2024
@exoego exoego added enhancement New feature or request help wanted Extra attention is needed and removed cant-repro labels Mar 1, 2024
@exoego
Copy link
Owner

exoego commented Apr 1, 2024

Closed by #202

@exoego exoego closed this as completed Apr 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants