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 * ## buildPagingJsonObject
* - Serializes the pagination metadata into JSON format: * - Serializes the pagination metadata into JSON format:
* - "config" → Contains the current page index and page size. * - "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>( data class PagingData<T>(
val i: Int, val pageSize: Int, val totalItemsCount: Long? = null, val i: Int, val pageSize: Int, val totalItemsCount: Long? = null,
@@ -52,13 +52,15 @@ data class PagingData<T>(
put("size", JsonPrimitive(pageSize)) put("size", JsonPrimitive(pageSize))
} }
putJsonObject("page") { putJsonObject("page") {
totalItemsCount?.also { itemCount -> totalItemsCount?.let { itemCount ->
max( val totalPages = max(
1, ceil( 1, ceil(
itemCount.toFloat() itemCount.toFloat()
.div(pageSize) .div(pageSize)
).roundToLong() ).roundToLong()
) )
put("totalItems", JsonPrimitive(itemCount))
put("totalPages", JsonPrimitive(totalPages))
} }
} }
} }