Skip to content

Commit b3dee7f

Browse files
committed
test: Paginated ToJSON/ToSchema
1 parent 5f71f42 commit b3dee7f

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

primer-service/src/Primer/Pagination.hs

+17-6
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
module Primer.Pagination (
77
PaginationParams,
88
Pagination (..),
9-
Paginated,
9+
-- Constructor and field accessors of Pagination exported for testing
10+
Paginated (..),
1011
-- 'Positive' is abstract. Do not export its constructor.
1112
Positive (getPositive),
1213
mkPositive,
1314
pagedDefaultClamp,
1415
-- the following are exposed for testing
15-
meta,
1616
PaginatedMeta (PM),
1717
totalItems,
1818
pageSize,
@@ -21,7 +21,6 @@ module Primer.Pagination (
2121
thisPage,
2222
nextPage,
2323
lastPage,
24-
items,
2524
getNonNeg,
2625
NonNeg,
2726
mkNonNeg,
@@ -36,7 +35,9 @@ import Optics ((?~))
3635
import Primer.Database (
3736
OffsetLimit (OL, limit, offset),
3837
Page (Page, pageContents, total),
38+
Session,
3939
)
40+
import Primer.OpenAPI ()
4041
import Servant (
4142
DefaultErrorFormatters,
4243
ErrorFormatters,
@@ -108,10 +109,20 @@ data Paginated a = Paginated
108109
{ meta :: PaginatedMeta
109110
, items :: [a]
110111
}
111-
deriving (Generic)
112+
deriving (Generic, Show)
112113

113-
instance ToJSON a => ToJSON (Paginated a)
114-
instance ToSchema a => ToSchema (Paginated a)
114+
-- We may well need more instances than just Paginated Session in the future.
115+
-- However, giving polymorphic `instance To... (Paginated a)` can generate
116+
-- a schema inconsistent with the ToJSON for some 'a'.
117+
-- This happens because aeson and openapi3 differ in their special handling
118+
-- for lists (e.g. to serialise strings as strings rather than arrays of
119+
-- characters). In particular the instance for 'Paginated Char' is broken.
120+
-- See https://github.com/biocad/openapi3/issues/58
121+
-- We prefer to explicitly list the particular instances we need, rather
122+
-- than having a known broken polymorphic instance, even if we expect to
123+
-- never hit the broken case.
124+
instance ToJSON (Paginated Session)
125+
instance ToSchema (Paginated Session)
115126

116127
-- Used solely for nice bounds in schema
117128
newtype NonNeg = NonNeg Int

primer-service/test/Tests/OpenAPI.hs

+4-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import Primer.Gen.Core.Raw (
3434
genValConName,
3535
)
3636
import Primer.OpenAPI ()
37-
import Primer.Pagination (NonNeg, PaginatedMeta (..), Positive, mkNonNeg, mkPositive)
37+
import Primer.Pagination (NonNeg, Paginated (Paginated), PaginatedMeta (..), Positive, mkNonNeg, mkPositive)
3838
import Primer.Server (openAPIInfo)
3939
import Tasty (Property, property)
4040
import Test.Tasty (TestTree, testGroup)
@@ -178,3 +178,6 @@ genPaginatedMeta = do
178178

179179
tasty_PaginatedMeta :: Property
180180
tasty_PaginatedMeta = testToJSON genPaginatedMeta
181+
182+
tasty_Paginated :: Property
183+
tasty_Paginated = testToJSON $ Paginated <$> genPaginatedMeta <*> G.list (R.linear 0 10) genSession

0 commit comments

Comments
 (0)