FIX: Ensure ordering is enforced in PostsController#replies (#31826)

This was picked up by the `PostsController#replies supports pagination`
requests spec as it was flaky from time to time.
This commit is contained in:
Alan Guo Xiang Tan
2025-03-14 17:56:26 +08:00
committed by GitHub
parent 992bdf173a
commit 577c043487
2 changed files with 10 additions and 2 deletions

View File

@ -451,6 +451,7 @@ class PostsController < ApplicationController
.replies
.secured(guardian)
.where(post_number: after + 1..)
.order(:post_number)
.limit(MAX_POST_REPLIES)
.pluck(:id)

View File

@ -211,25 +211,32 @@ RSpec.describe PostsController do
it "supports pagination" do
parent = Fabricate(:post)
child_posts = []
30.times do
reply = Fabricate(:post, topic: parent.topic, reply_to_post_number: parent.post_number)
PostReply.create!(post: parent, reply:)
child_posts << reply
end
get "/posts/#{parent.id}/replies.json", params: { after: parent.post_number }
expect(response.status).to eq(200)
replies = response.parsed_body
expect(replies.size).to eq(20)
expect(replies.map { |reply| reply["id"] }).to eq(child_posts[0..19].map(&:id))
after = replies.last["post_number"]
get "/posts/#{parent.id}/replies.json", params: { after: }
expect(response.status).to eq(200)
replies = response.parsed_body
expect(replies.size).to eq(10)
expect(replies.map { |reply| reply["id"] }).to eq(child_posts[20..-1].map(&:id))
expect(replies[0][:post_number]).to eq(after + 1)
get "/posts/#{parent.id}/replies.json", params: { after: 999_999 }
expect(response.status).to eq(200)
expect(response.parsed_body.size).to eq(0)
end