Fix #4973: Wiki pages json index returns 404.
Fix regression introduced in 0db20e0ca. Setting `format: false` on the
wiki pages resource disabled format negotiation on all wiki page routes,
not just the show page, which meant /wiki_pages.json no longer worked.
The fix to monkey patch the internal Rails method that parses the file
extension from the URL, and have it ignore everything but the .html,
.json, .js, and .xml extensions. This is really hacky and may break in
future Rails releases.
This commit is contained in:
@@ -30,15 +30,6 @@ class WikiPagesController < ApplicationController
|
||||
end
|
||||
|
||||
def show
|
||||
if params[:format].present?
|
||||
request.format = params[:format]
|
||||
elsif params[:id].ends_with?(".html", ".json", ".xml")
|
||||
request.format = params[:id].split(".").last
|
||||
params[:id].delete_suffix!(".#{request.format.symbol}")
|
||||
else
|
||||
request.format = "html"
|
||||
end
|
||||
|
||||
@wiki_page, found_by = WikiPage.find_by_id_or_title(params[:id])
|
||||
|
||||
if request.format.html? && @wiki_page.blank? && found_by == :title
|
||||
|
||||
@@ -63,6 +63,23 @@ class String
|
||||
include Danbooru::Extensions::String
|
||||
end
|
||||
|
||||
module MimeNegotationExtension
|
||||
# Ignore all file extensions except for .html, .js, .json, and .xml when
|
||||
# parsing the file extension from the URL. Needed for wiki pages (e.g.
|
||||
# /wiki_pages/rnd.jpg).
|
||||
private def format_from_path_extension
|
||||
mime = super
|
||||
|
||||
if mime&.symbol.in?(%i[html js json xml])
|
||||
mime
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
ActionDispatch::Http::MimeNegotiation.prepend(MimeNegotationExtension)
|
||||
|
||||
# Make Symbol#to_s return a frozen string. This reduces allocations, but may be
|
||||
# incompatible with some libraries.
|
||||
#
|
||||
|
||||
@@ -280,7 +280,7 @@ Rails.application.routes.draw do
|
||||
resources :webhooks do
|
||||
post :receive, on: :collection
|
||||
end
|
||||
resources :wiki_pages, id: /.+/, format: false do
|
||||
resources :wiki_pages, id: /.+?(?=\.json|\.xml|\.html)|.+/ do
|
||||
put :revert, on: :member
|
||||
get :search, on: :collection
|
||||
get :show_or_new, on: :collection
|
||||
|
||||
@@ -31,6 +31,13 @@ class WikiPagesControllerTest < ActionDispatch::IntegrationTest
|
||||
assert_equal(WikiPage.count, response.parsed_body.css("urlset url loc").size)
|
||||
end
|
||||
|
||||
should "render for a JSON response" do
|
||||
get wiki_pages_path(format: :json)
|
||||
|
||||
assert_response :success
|
||||
assert_equal("application/json", response.media_type)
|
||||
end
|
||||
|
||||
should "redirect the legacy title param to the show page" do
|
||||
get wiki_pages_path(title: "tagme")
|
||||
assert_redirected_to wiki_pages_path(search: { title_normalize: "tagme" }, redirect: true)
|
||||
|
||||
Reference in New Issue
Block a user