Skip to content

Commit 8c6ce4f

Browse files
committed
test: Paginated ToJSON/ToSchema
1 parent f6f873b commit 8c6ce4f

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

primer-service/src/Primer/Pagination.hs

+18-7
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,
@@ -132,11 +133,21 @@ data Paginated a = Paginated
132133
{ meta :: PaginatedMeta
133134
, items :: [a]
134135
}
135-
deriving (Generic)
136+
deriving (Generic, Show)
136137

137-
instance ToJSON a => ToJSON (Paginated a)
138-
instance FromJSON a => FromJSON (Paginated a)
139-
instance ToSchema a => ToSchema (Paginated a)
138+
-- We may well need more instances than just Paginated Session in the future.
139+
-- However, giving polymorphic `instance To... (Paginated a)` can generate
140+
-- a schema inconsistent with the ToJSON for some 'a'.
141+
-- This happens because aeson and openapi3 differ in their special handling
142+
-- for lists (e.g. to serialise strings as strings rather than arrays of
143+
-- characters). In particular the instance for 'Paginated Char' is broken.
144+
-- See https://github.com/biocad/openapi3/issues/58
145+
-- We prefer to explicitly list the particular instances we need, rather
146+
-- than having a known broken polymorphic instance, even if we expect to
147+
-- never hit the broken case.
148+
instance ToJSON (Paginated Session)
149+
instance FromJSON (Paginated Session)
150+
instance ToSchema (Paginated Session)
140151

141152
-- Used solely for nice bounds in schema
142153
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)