Repository navigation
data_hash next returns different datatype depending on paginator
#844
Replies: 2 comments 3 replies
...or there is a bug that has never been triggered by any test (which seems to be quite unlikely)... Anyway, I would suggest you to try to reproduce it with the repro app.
By definition the keyset pages are encoded arrays of the keyset columns values. That must be exchanged between client and server. Keyset pagination (not only the pagy implementation) has no concept of numbered pages at all, so there is NO expectation at all for the keyset pages (including next) to be integers, or even the possibility to be integers.
No, because the real consequence is that you wouldn't be able to handle keyset pagination at all.
You mean checking whether there is a next page or not, I guess. Because if your client make some weird decision based on the next page number, you are tied to the offset paginators family (i.e. numeric pages), which is not ideal for APIs. So the best decision an API should make is checking whether any value exist for next or not, and decouple it from the integer constraint.
🤣 Just read the basic concept of keyset pagination... I strongly recommend you to use it for API or infinite scrolling, unless you really have complex dynamic ordering to support (which would make the indices tricky to maintain). If you don't care about ordering, then it's a no-brainer decision and implementation based on the |
|
@ddnexus thanks, that helps. I was previously using Only a very tiny minority of routes require complex filter and ordering, but planning to move I did a minor refactor on the client, where pagination is now only driven by My metadata is typed optionals, and was the source for most of my issues, responses weren't even being decoded, which is a me problem and not even related. My most recent issue (with next_url refactor + countless) i ran into was logic error that i missed in a controller test, where the client was relying on Example of a very dumb pagination on the client. let codable = try await ItemRequest.items.save()
var nextUrl: String? = codable?.pagy?.next_url
repeat {
guard path = nextURL else { return }
let codable = try await ItemRequest.items(path: path).save()
var nextUrl: String? = codable?.pagy?.next_url
} while nextUrl != nilCollection with metadata response structure. I think these specific responses are via public struct PaginatedResponse<T: Decodable>: Decodable {
/// An array of decoded data objects.
public let data: [T]
/// Optional pagination metadata for navigating through result pages.
public let pagy: Pagy?
}First request {
"data" => [], // truncated
"pagy" => {
"page" => 1,
"next" => 2,
"next_url" => "/api/items?limit=4&page=2+15"
}
}[page 2-3 truncated, normal/what i expected] "Last" request, where i got in trouble on the client because i expected next_url to be nil here. {
"data" => [], // truncated
"pagy" => {
"page" => 4,
"next" => nil,
"next_url" => "/api/items?limit=4&page=+15"
}
}I'm still convinced this is user error though, but after I do more digging, if i find something i'll work on a repro app, or more likely, probably just a single line doc PR. I don't have any issues with the same routes on WWW so it's something smelly, specific to JSON api. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
After upgrading to 43, countless behavior regressed for me. I'm still investigating why, but
nextwhen using:countlessis always returning nil. I was able to get normal behavior after using:offset. I'm either not understanding something, or have been always using it incorrectly and was only working by accident.I also attempted migrating to
keyset, which seemed to be working fine (and is working fine with standard view helpers), but the value returns a string. I understand this is encoded, but this is not something the client knows how to handle and violates expected datatype ofpublic let next: Int?For more context, this a REST json api, collection pagination on the client uses
nextto drive request logic.Is there a global kill switch to disable base64, fully accepting potential consequences? That also means disabling any rails request processing, eg client api requests with query param
page=nis throwingArgumentError: invalid base64 (ArgumentError)This would give me some breathing room before committing to a pretty painful refactor.Or is there at least an escape hatch for a softer landing from these fatals? The requests that throw this fatal, look totally sane to me, so I clearly need to RTFM.
All reactions