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

Use Rack::Headers to avoid deprecation warning about Rack 3 #240

Closed
wants to merge 2 commits into from
Closed

Use Rack::Headers to avoid deprecation warning about Rack 3 #240

wants to merge 2 commits into from

Conversation

3UR
Copy link

@3UR 3UR commented Oct 19, 2023

This resolves #239

@olleolleolle olleolleolle changed the title Resolve Issue Use Rack::Headers to avoid deprecation warning Oct 19, 2023
@olleolleolle
Copy link
Contributor

olleolleolle commented Oct 19, 2023

Thanks, @3UR for updating the code.

Background: The Rack::Headers class was introduced in Rack rack/rack@79328de in 3.0.

Blocker: httpi's gemspec lists rack < 3, so this can not go in yet.

https://github.com/savonrb/httpi/blob/master/httpi.gemspec#L24

And, more context to that blocker: savonrb/savon#992

@olleolleolle olleolleolle changed the title Use Rack::Headers to avoid deprecation warning Use Rack::Headers to avoid deprecation warning about Rack 3 Oct 19, 2023
@pcai
Copy link
Member

pcai commented Oct 19, 2023

Thanks @3UR and @olleolleolle - correct, this can't go in as-is. This is unlikely to go in at all, since this is a major breaking change that affects a public API method, and we're focused on maintenance only at the moment - see also #238

@dorianmarie
Copy link

Couldn't we check the Rack version and use Rack::Utils::HeaderHash or Rack::Headers accordingly?

@pcai
Copy link
Member

pcai commented Dec 7, 2023

@dorianmarie we could, but I disagree with that design choice: A library’s public API should not depend on the version of a transitive dependency. A change in API should be a change in version.

@gbs4ever
Copy link

so how can we silence this Rack::Utils::HeaderHash is deprecated warning

@pcai
Copy link
Member

pcai commented Jan 24, 2024

@gbs4ever

presumably you could

  • use rack 2.x
  • if you need rack 3+, monkeypatch it in classic ruby style. In rails the (untested) pseudocode would probably look like:
# config/initializers/httpi.rb

# use rack 3.x interface to silence a warning
module HTTPI
  class Response
    def initialize(code, headers, body)
      self.code = code.to_i
      self.headers = Rack::Headers.new(headers)
      self.raw_body = body
    end
  end
  
  class Request
    def headers
      @headers ||= Rack::Headers.new
    end
  end
end

@gbs4ever
Copy link

gbs4ever commented Jan 24, 2024

Thank you @pcai
upon testing Rack::Headers.new does not take arguments which is why this is my idea

Rails.logger.warn "Rewrote Httpi due to Rack::Utils::HeaderHash.new being deprecated https://github.com/rack/rack/blob/main/UPGRADE-GUIDE.md?plain=1 "
module HTTPI
 class Response
   def initialize(code, headers, body)
     self.code = code.to_i
     self.headers = !headers.is_a?(Hash) ? Rack::Headers.new.merge!(headers) : headers
     self.raw_body = body
   end
 end

 class Request
   def headers
     @headers ||= Rack::Headers.new
   end

   def headers=(headers)
     @headers = Rack::Headers.new.merge!(headers)
   end
 end
end`

@Slotos
Copy link

Slotos commented Jan 24, 2024

@dorianmarie we could, but I disagree with that design choice: A library’s public API should not depend on the version of a transitive dependency. A change in API should be a change in version.

The problem lies in #headers leaking implementation details. Would it return normalized library specific headers object, the library would be free to pick and choose whichever implementation fits the bill.

@gbs4ever
Copy link

gbs4ever commented Jan 24, 2024

It seems to be incorrect this version @3UR and @pcai as Rack::Headers.new does not accept a hash as an argument see below

Rack::Headers.new(headers) => {} vs  Rack::Utils::HeaderHash.new(headers) =>  {
 "Content-Type" => "application/json",
 "Authorization" => "Bearer your_token_here",
}

 This would be more correct
 
  
  Rack::Headers.new.merge!(headers)

@pcai
Copy link
Member

pcai commented Feb 11, 2024

Thanks everyone for your contributions.

I took some time to investigate and this is now fixed in 56cd67b which ended up being relatively minor overall.

I couldn't use this PR - as others have pointed out, the code isn't correct, and it doesnt account for tests and other dependencies that need updating (namely puma 6.x)

@pcai pcai closed this Feb 11, 2024
@pcai
Copy link
Member

pcai commented Feb 11, 2024

also @Slotos re:

The problem lies in #headers leaking implementation details. Would it return normalized library specific headers object, the library would be free to pick and choose whichever implementation fits the bill.

I agree that this is a mistake in the initial API design. But there is no turning back the clock on this one unfortunately.

@pcai
Copy link
Member

pcai commented Feb 14, 2024

Since it turns out that rack 3.x and 2.x have disjoint APIs and httpi 4.x is a public API change anyway, I actually went and implemented the suggestion by @Slotos by vendoring the class: #245

I invite participants to review this change and release strategy.

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

Successfully merging this pull request may close these issues.

Rack::Utils::HeaderHash --> Rack::Headers
6 participants