Defect #39760
closedSome API tests fail with Ruby 2.7
0%
Description
In the current trunk r22493, the following three API tests fail if you run tests with Ruby 2.7.
Failure: Redmine::ApiTest::MyTest#test_GET_/my/account.json_should_return_user [/path/to/redmine/test/integration/api_test/my_test.rb:30]: Expected: "application/json" Actual: "text/html" bin/rails test test/integration/api_test/my_test.rb:25
Failure: Redmine::ApiTest::NewsTest#test_GET_/news/:id.json [/path/to/redmine/test/integration/api_test/news_test.rb:84]: Expected: "application/json" Actual: "text/html" bin/rails test test/integration/api_test/news_test.rb:81
Failure: Redmine::ApiTest::NewsTest#test_GET_/news/:id.xml [/path/to/redmine/test/integration/api_test/news_test.rb:69]: Expected: "application/xml" Actual: "text/html" bin/rails test test/integration/api_test/news_test.rb:66
Related issues
Updated by Go MAEDA about 1 year ago
I found the following change fixes the errors.
diff --git a/app/controllers/my_controller.rb b/app/controllers/my_controller.rb
index 860a91f06..7f1f2180f 100644
--- a/app/controllers/my_controller.rb
+++ b/app/controllers/my_controller.rb
@@ -69,6 +69,11 @@ class MyController < ApplicationController
format.api {render_validation_errors(@user)}
end
end
+ else
+ respond_to do |format|
+ format.html
+ format.api
+ end
end
end
diff --git a/app/controllers/news_controller.rb b/app/controllers/news_controller.rb
index a4c48614f..3f1a13ab1 100644
--- a/app/controllers/news_controller.rb
+++ b/app/controllers/news_controller.rb
@@ -69,6 +69,11 @@ class NewsController < ApplicationController
def show
@comments = @news.comments.to_a
@comments.reverse! if User.current.wants_comments_in_reverse_order?
+
+ respond_to do |format|
+ format.html
+ format.api
+ end
end
def new
Updated by Minoru Maeda about 1 year ago
Thank you for the insightful report. I appreciate the detailed information, which has helped me understand the main problem.
In Ruby 2.7, some API tests fail due to changes in how Module#include and Module#prepend work from Ruby 3.0 onwards. In Ruby 2.7, the order in which modules are included affects whether include/prepend functions properly in certain cases. This issue is related to https://bugs.ruby-lang.org/issues/9573 .
Since trunk revision r22488, ActionView::Rendering has been prepended with an anonymous module in config/initializers/10-patches.rb. However, ActionView::Rendering is already included in ActionController::Base. Thus, in Ruby 2.7, prepending ActionView::Rendering at the beginning with this anonymous module seems to be ineffective, and the API tests fail to produce the expected context-type.
Implementing the following changes will allow the API tests to pass in Ruby 2.7.
diff --git a/config/initializers/10-patches.rb b/config/initializers/10-patches.rb
index adac86583..7694fb60d 100644
--- a/config/initializers/10-patches.rb
+++ b/config/initializers/10-patches.rb
@@ -148,8 +148,9 @@ module ActionView
super values
end
end)
-
- Rendering.prepend(Module.new do
+end
+module ActionController
+ Base.prepend(Module.new do
def rendered_format
if lookup_context.formats.first == :api
return request.format
I hope this proves helpful.
Updated by Marius BĂLTEANU about 1 year ago
- Related to Feature #36320: Migrate to Rails 7.2 added
Updated by Marius BĂLTEANU about 1 year ago
- Status changed from New to Closed
- Assignee set to Marius BĂLTEANU
- Resolution set to Fixed
Committed, thanks!