Skip to content

Commit

Permalink
test: Paginated ToJSON/ToSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
brprice committed Aug 16, 2022
1 parent f6f873b commit 8c6ce4f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
25 changes: 18 additions & 7 deletions primer-service/src/Primer/Pagination.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
module Primer.Pagination (
PaginationParams,
Pagination (..),
Paginated,
-- Constructor and field accessors of Pagination exported for testing
Paginated (..),
-- 'Positive' is abstract. Do not export its constructor.
Positive (getPositive),
mkPositive,
pagedDefaultClamp,
-- the following are exposed for testing
meta,
PaginatedMeta (PM),
totalItems,
pageSize,
Expand All @@ -21,7 +21,6 @@ module Primer.Pagination (
thisPage,
nextPage,
lastPage,
items,
getNonNeg,
NonNeg,
mkNonNeg,
Expand All @@ -36,7 +35,9 @@ import Optics ((?~))
import Primer.Database (
OffsetLimit (OL, limit, offset),
Page (Page, pageContents, total),
Session,
)
import Primer.OpenAPI ()
import Servant (
DefaultErrorFormatters,
ErrorFormatters,
Expand Down Expand Up @@ -132,11 +133,21 @@ data Paginated a = Paginated
{ meta :: PaginatedMeta
, items :: [a]
}
deriving (Generic)
deriving (Generic, Show)

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

-- Used solely for nice bounds in schema
newtype NonNeg = NonNeg Int
Expand Down
5 changes: 4 additions & 1 deletion primer-service/test/Tests/OpenAPI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import Primer.Gen.Core.Raw (
genValConName,
)
import Primer.OpenAPI ()
import Primer.Pagination (NonNeg, PaginatedMeta (..), Positive, mkNonNeg, mkPositive)
import Primer.Pagination (NonNeg, Paginated (Paginated), PaginatedMeta (..), Positive, mkNonNeg, mkPositive)
import Primer.Server (openAPIInfo)
import Tasty (Property, property)
import Test.Tasty (TestTree, testGroup)
Expand Down Expand Up @@ -178,3 +178,6 @@ genPaginatedMeta = do

tasty_PaginatedMeta :: Property
tasty_PaginatedMeta = testToJSON genPaginatedMeta

tasty_Paginated :: Property
tasty_Paginated = testToJSON $ Paginated <$> genPaginatedMeta <*> G.list (R.linear 0 10) genSession

0 comments on commit 8c6ce4f

Please sign in to comment.