From 6da19415531e4c40f48df2c604efe51578cea295 Mon Sep 17 00:00:00 2001 From: blackst0ne Date: Wed, 20 Jun 2018 21:42:52 +1100 Subject: [Rails5] Set request.format in commits_controller Since Rails 5.0 `request.format` is being set depending on the route extension. In commits pages we pass a ref as a URL parameter. If the ref is an extension-like, Rails converts it and sets to `request.format`. E.g. if the ref is `some_branch.atom`, Rails starts responding with `atom` format, but it should response with `html` because `.atom` is the part of ref name, not the format. This commit explicitly sets `request.format` to `:html` when needed. --- app/controllers/projects/commits_controller.rb | 14 ++++++++++++++ ...ne-rails5-set-request-format-in--commits-controller.yml | 5 +++++ 2 files changed, 19 insertions(+) create mode 100644 changelogs/unreleased/blackst0ne-rails5-set-request-format-in--commits-controller.yml diff --git a/app/controllers/projects/commits_controller.rb b/app/controllers/projects/commits_controller.rb index 7b7cb52d7ed..9e495061f4e 100644 --- a/app/controllers/projects/commits_controller.rb +++ b/app/controllers/projects/commits_controller.rb @@ -9,6 +9,7 @@ class Projects::CommitsController < Projects::ApplicationController before_action :assign_ref_vars before_action :authorize_download_code! before_action :set_commits + before_action :set_request_format, only: :show def show @merge_request = MergeRequestsFinder.new(current_user, project_id: @project.id).execute.opened @@ -61,6 +62,19 @@ class Projects::CommitsController < Projects::ApplicationController @commits = prepare_commits_for_rendering(@commits) end + # Rails 5 sets request.format from the extension. + # Explicitly set to :html. + def set_request_format + request.format = :html if set_request_format? + end + + # Rails 5 sets request.format from extension. + # In this case if the ref ends with `.atom`, it's expected to be the html response, + # not the atom one. So explicitly set request.format as :html to act like rails4. + def set_request_format? + request.format.to_s == "text/html" || @commits.ref.ends_with?("atom") + end + def whitelist_query_limiting Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-ce/issues/42330') end diff --git a/changelogs/unreleased/blackst0ne-rails5-set-request-format-in--commits-controller.yml b/changelogs/unreleased/blackst0ne-rails5-set-request-format-in--commits-controller.yml new file mode 100644 index 00000000000..3f8f8fd5d66 --- /dev/null +++ b/changelogs/unreleased/blackst0ne-rails5-set-request-format-in--commits-controller.yml @@ -0,0 +1,5 @@ +--- +title: "[Rails5] Set request.format in commits_controller" +merge_request: 20023 +author: "@blackst0ne" +type: fixed -- cgit v1.2.1