From d9ce73b7bff6fbf49fade19a4756c12283650e53 Mon Sep 17 00:00:00 2001 From: Chad Wilson <29788154+chadlwilson@users.noreply.github.com> Date: Fri, 7 Nov 2025 00:05:43 +0800 Subject: [PATCH] [fix] Fix JSP forward/inclusion by monkey-patching the "correct" class This logic has been moved to re-usable helpers a long time back, which Rails and other frameworks rely upon. Patching modules already included doesn't appear to work on Ruby 2.6/JRuby 9.3 so add a fallback to patch the classes known to include. Don't know a better way; but this logic will be dropped on jruby-rack 1.3. --- CHANGELOG.md | 4 +++ examples/README.md | 31 +++++++++++++---------- src/main/ruby/jruby/rack/rack_ext.rb | 16 +++++++++--- src/spec/ruby/jruby/rack/rack_ext_spec.rb | 2 +- 4 files changed, 36 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 289d48da3..4e4837805 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.2.7 (UNRELEASED) + +- Fix ability to include and forward to JSPs under Rails (#370) + ## 1.2.6 - Add missing block-only signature for debug logging diff --git a/examples/README.md b/examples/README.md index c58c11fe8..3b6160845 100644 --- a/examples/README.md +++ b/examples/README.md @@ -28,19 +28,24 @@ As an executable jar within Jetty: ## Demo routes -| Example | Component | Embedded Route | Deployed War Route | -|---------|-------------------------------|-----------------------------------|------------------------------------------| -| Rails 7 | Status Page | http://localhost:8080/up | http://localhost:8080/rails7/up | -| Rails 7 | Snoop Dump | http://localhost:8080/snoop | http://localhost:8080/rails7/snoop | -| Rails 7 | Embedded JSP (non-functional) | http://localhost:8080/jsp/ | http://localhost:8080/rails7/jsp/ | -| Rails 7 | Simple Form submission | http://localhost:8080/simple_form | http://localhost:8080/rails7/simple_form | -| Rails 7 | Body Posts | http://localhost:8080/body | http://localhost:8080/rails7/body | -| Sinatra | Demo Index | http://localhost:8080/ | http://localhost:8080/sinatra | -| Sinatra | Info | http://localhost:8080/info | http://localhost:8080/sinatra/info | -| Sinatra | Snoop Dump | http://localhost:8080/env | http://localhost:8080/sinatra/env | -| Sinatra | Streaming Demo | http://localhost:8080/stream | http://localhost:8080/sinatra/stream | -| Camping | Demo Index | http://localhost:8080/ | http://localhost:8080/camping | -| Camping | Snoop Dump | http://localhost:8080/snoop | http://localhost:8080/camping/snoop | +| Example | Component | Embedded Route | Deployed War Route | +|---------|------------------------|-------------------------------------|---------------------------------------------| +| Rails 7 | Status Page | http://localhost:8080/up | http://localhost:8080/rails7/up | +| Rails 7 | Snoop Dump | http://localhost:8080/snoop | http://localhost:8080/rails7/snoop | +| Rails 7 | Simple Form submission | http://localhost:8080/simple_form | http://localhost:8080/rails7/simple_form | +| Rails 7 | Body Posts | http://localhost:8080/body | http://localhost:8080/rails7/body | +| Rails 7 | JSP (render) | http://localhost:8080/jsp/ | http://localhost:8080/rails7/jsp/ | +| Rails 7 | JSP (forward to) | http://localhost:8080/jsp-forward/ | http://localhost:8080/rails7/jsp-forward/ | +| Rails 7 | JSP (include) | http://localhost:8080/jsp-include/ | http://localhost:8080/rails7/jsp-include/ | +| Sinatra | Demo Index | http://localhost:8080/ | http://localhost:8080/sinatra | +| Sinatra | Info | http://localhost:8080/info | http://localhost:8080/sinatra/info | +| Sinatra | Snoop Dump | http://localhost:8080/env | http://localhost:8080/sinatra/env | +| Sinatra | JSP (render) | http://localhost:8080/jsp/index.jsp | http://localhost:8080/sinatra/jsp/index.jsp | +| Sinatra | JSP (forward to) | http://localhost:8080/jsp_forward | http://localhost:8080/sinatra/jsp_forward | +| Sinatra | JSP (include) | http://localhost:8080/jsp_include | http://localhost:8080/sinatra/jsp_include | +| Sinatra | Streaming Demo | http://localhost:8080/stream | http://localhost:8080/sinatra/stream | +| Camping | Demo Index | http://localhost:8080/ | http://localhost:8080/camping | +| Camping | Snoop Dump | http://localhost:8080/snoop | http://localhost:8080/camping/snoop | ## Development diff --git a/src/main/ruby/jruby/rack/rack_ext.rb b/src/main/ruby/jruby/rack/rack_ext.rb index f86993570..36012697b 100644 --- a/src/main/ruby/jruby/rack/rack_ext.rb +++ b/src/main/ruby/jruby/rack/rack_ext.rb @@ -17,7 +17,7 @@ module JRuby module Rack module RackExt - module Request + module RequestHelpers java_import org.jruby.rack.servlet.ServletRackIncludedResponse def forward_to(path, params={}) @@ -38,8 +38,18 @@ def render(path, params={}) end end - ::Rack::Request.module_eval do - include ::JRuby::Rack::RackExt::Request + if JRUBY_VERSION >= '9.4' + ::Rack::Request::Helpers.module_eval do + include ::JRuby::Rack::RackExt::RequestHelpers + end + else + ::Rack::Request.module_eval do + include ::JRuby::Rack::RackExt::RequestHelpers + end + + ::ActionDispatch::Request.module_eval do + include ::JRuby::Rack::RackExt::RequestHelpers + end if defined?(::ActionDispatch::Request) end end end diff --git a/src/spec/ruby/jruby/rack/rack_ext_spec.rb b/src/spec/ruby/jruby/rack/rack_ext_spec.rb index 0e074d5a1..07dde8e1f 100644 --- a/src/spec/ruby/jruby/rack/rack_ext_spec.rb +++ b/src/spec/ruby/jruby/rack/rack_ext_spec.rb @@ -8,7 +8,7 @@ require File.expand_path('spec_helper', File.dirname(__FILE__) + '/../..') require 'jruby/rack/rack_ext' -describe Rack::Request do +describe JRuby::Rack::RackExt do before :each do @servlet_request = double("servlet_request") @servlet_response = double("servlet_response")