fix: totalItems/totalPages missing from PagingData JSON serialization

This commit is contained in:
2026-08-13 15:12:30 +08:00
parent 56203dfb71
commit 15eeaf7c81

View File

@@ -26,7 +26,7 @@ import kotlin.math.roundToLong
* ## buildPagingJsonObject
* - Serializes the pagination metadata into JSON format:
* - "config" → Contains the current page index and page size.
* - "page" → Can include the total number of pages if totalItemsCount is set.
* - "page" → Includes "totalItems" and "totalPages" when totalItemsCount is set.
*/
data class PagingData<T>(
val i: Int, val pageSize: Int, val totalItemsCount: Long? = null,
@@ -52,13 +52,15 @@ data class PagingData<T>(
put("size", JsonPrimitive(pageSize))
}
putJsonObject("page") {
totalItemsCount?.also { itemCount ->
max(
totalItemsCount?.let { itemCount ->
val totalPages = max(
1, ceil(
itemCount.toFloat()
.div(pageSize)
).roundToLong()
)
put("totalItems", JsonPrimitive(itemCount))
put("totalPages", JsonPrimitive(totalPages))
}
}
}