Skip to content

Commit

Permalink
Expose max and default pagination limit in openapi
Browse files Browse the repository at this point in the history
We also revet the previous value to allow application to migrate to new
values.
  • Loading branch information
tdroxler committed Oct 8, 2024
1 parent 8eddbdf commit 8c60c71
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 24 deletions.
56 changes: 38 additions & 18 deletions app/src/main/resources/explorer-backend-openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,12 @@
"type"
]
},
"PaginationPageDefault": {
"type": "integer",
"enum": [
1
]
},
"ValBool": {
"type": "object",
"title": "ValBool",
Expand All @@ -1382,6 +1388,13 @@
"type"
]
},
"PaginationLimitMax": {
"type": "integer",
"enum": [
100,
20
]
},
"FungibleTokenMetadata": {
"type": "object",
"title": "FungibleTokenMetadata",
Expand Down Expand Up @@ -1431,6 +1444,13 @@
"aud"
]
},
"PaginationLimitDefault": {
"type": "integer",
"enum": [
20,
10
]
},
"AddressInfo": {
"type": "object",
"title": "AddressInfo",
Expand Down Expand Up @@ -1713,7 +1733,7 @@
{
"schema": {
"format": "int32",
"maximum": "20",
"maximum": "100",
"type": "integer",
"minimum": "0"
},
Expand Down Expand Up @@ -1958,7 +1978,7 @@
{
"schema": {
"format": "int32",
"maximum": "20",
"maximum": "100",
"type": "integer",
"minimum": "0"
},
Expand Down Expand Up @@ -2388,7 +2408,7 @@
{
"schema": {
"format": "int32",
"maximum": "20",
"maximum": "100",
"type": "integer",
"minimum": "0"
},
Expand Down Expand Up @@ -2660,7 +2680,7 @@
{
"schema": {
"format": "int32",
"maximum": "20",
"maximum": "100",
"type": "integer",
"minimum": "0"
},
Expand Down Expand Up @@ -3802,7 +3822,7 @@
{
"schema": {
"format": "int32",
"maximum": "20",
"maximum": "100",
"type": "integer",
"minimum": "0"
},
Expand Down Expand Up @@ -4087,7 +4107,7 @@
{
"schema": {
"format": "int32",
"maximum": "20",
"maximum": "100",
"type": "integer",
"minimum": "0"
},
Expand Down Expand Up @@ -4772,7 +4792,7 @@
{
"schema": {
"format": "int32",
"maximum": "20",
"maximum": "100",
"type": "integer",
"minimum": "0"
},
Expand Down Expand Up @@ -5057,7 +5077,7 @@
{
"schema": {
"format": "int32",
"maximum": "20",
"maximum": "100",
"type": "integer",
"minimum": "0"
},
Expand Down Expand Up @@ -5506,7 +5526,7 @@
{
"schema": {
"format": "int32",
"maximum": "20",
"maximum": "100",
"type": "integer",
"minimum": "0"
},
Expand Down Expand Up @@ -6442,7 +6462,7 @@
{
"schema": {
"format": "int32",
"maximum": "20",
"maximum": "100",
"type": "integer",
"minimum": "0"
},
Expand Down Expand Up @@ -6570,7 +6590,7 @@
{
"schema": {
"format": "int32",
"maximum": "20",
"maximum": "100",
"type": "integer",
"minimum": "0"
},
Expand Down Expand Up @@ -7022,7 +7042,7 @@
{
"schema": {
"format": "int32",
"maximum": "20",
"maximum": "100",
"type": "integer",
"minimum": "0"
},
Expand Down Expand Up @@ -7238,7 +7258,7 @@
{
"schema": {
"format": "int32",
"maximum": "20",
"maximum": "100",
"type": "integer",
"minimum": "0"
},
Expand Down Expand Up @@ -7374,7 +7394,7 @@
{
"schema": {
"format": "int32",
"maximum": "20",
"maximum": "100",
"type": "integer",
"minimum": "0"
},
Expand Down Expand Up @@ -7920,7 +7940,7 @@
{
"schema": {
"format": "int32",
"maximum": "20",
"maximum": "100",
"type": "integer",
"minimum": "0"
},
Expand Down Expand Up @@ -8252,7 +8272,7 @@
{
"schema": {
"format": "int32",
"maximum": "20",
"maximum": "100",
"type": "integer",
"minimum": "0"
},
Expand Down Expand Up @@ -8372,7 +8392,7 @@
{
"schema": {
"format": "int32",
"maximum": "20",
"maximum": "100",
"type": "integer",
"minimum": "0"
},
Expand Down Expand Up @@ -9210,7 +9230,7 @@
{
"schema": {
"format": "int32",
"maximum": "20",
"maximum": "100",
"type": "integer",
"minimum": "0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ object Pagination {
Reversible(page, limit, reverse)
}
}
val defaultPage: Int = 1
val defaultLimit: Int = 10
val maxLimit: Int = 20
val defaultPage: Int = 1
val defaultLimit: Int = 20
val futureDefaultLimit: Int = 10
val maxLimit: Int = 100
val futureMaxLimit: Int = 20

def default: Pagination = Pagination(defaultPage, defaultLimit)

Expand Down
37 changes: 37 additions & 0 deletions app/src/main/scala/org/alephium/explorer/docs/Documentation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import sttp.apispec.openapi.OpenAPI
import sttp.tapir.docs.openapi.OpenAPIDocsInterpreter

import org.alephium.explorer.api._
import org.alephium.explorer.api.model.Pagination

trait Documentation
extends BlockEndpoints
Expand Down Expand Up @@ -105,10 +106,46 @@ trait Documentation
)

// Expose some variables to the openAPI file
// scalastyle:off method.length
@SuppressWarnings(Array("org.wartremover.warts.OptionPartial"))
private def addComponents(openApi: OpenAPI): OpenAPI =
openApi.components(
openApi.components.get
.addSchema(
"PaginationLimitDefault",
Schema(
`type` = Some(List(SchemaType.Integer)),
`enum` = Some(
List(
ExampleSingleValue(Pagination.defaultLimit),
ExampleSingleValue(Pagination.futureDefaultLimit)
)
)
)
)
.addSchema(
"PaginationLimitMax",
Schema(
`type` = Some(List(SchemaType.Integer)),
`enum` = Some(
List(
ExampleSingleValue(Pagination.maxLimit),
ExampleSingleValue(Pagination.futureMaxLimit)
)
)
)
)
.addSchema(
"PaginationPageDefault",
Schema(
`type` = Some(List(SchemaType.Integer)),
`enum` = Some(
List(
ExampleSingleValue(Pagination.defaultPage)
)
)
)
)
.addSchema(
"MaxSizeTokens",
Schema(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ trait ExplorerSpec

val networkId: NetworkId = NetworkId.AlephiumDevNet

val txLimit = 10
val txLimit = Pagination.defaultLimit

val blockflow: ArraySeq[ArraySeq[model.BlockEntry]] =
blockFlowGen(maxChainSize = 5, startTimestamp = TimeStamp.now()).sample.get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class AddressServerSpec()
val routes = server.routes

"validate and forward `txLimit` query param" in {
val maxLimit = 20
val maxLimit = Pagination.maxLimit
forAll(addressGen, Gen.chooseNum[Int](-10, 120)) { case (address, txLimit) =>
Get(s"/addresses/${address}/transactions?limit=$txLimit") check { response =>
if (txLimit < 0) {
Expand All @@ -225,7 +225,7 @@ class AddressServerSpec()
}

Get(s"/addresses/${address}/transactions") check { _ =>
testLimit is 10 // default txLimit
testLimit is Pagination.defaultLimit // default txLimit
}
}
}
Expand Down

0 comments on commit 8c60c71

Please sign in to comment.