Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NonEmpty ToSchema instance #141

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Data/Swagger/Internal/Schema.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
import Data.Int
import Data.IntSet (IntSet)
import Data.IntMap (IntMap)
import Data.List.NonEmpty (NonEmpty)
import Data.Map (Map)
import Data.Proxy
import Data.Scientific (Scientific)
Expand Down Expand Up @@ -561,6 +562,12 @@ instance ToSchema a => ToSchema (Set a) where

instance ToSchema a => ToSchema (HashSet a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Set a))

instance ToSchema a => ToSchema (NonEmpty a) where
declareNamedSchema _ = do
schema <- declareSchema (Proxy :: Proxy [a])
return $ unnamed $ schema
& minItems .~ Just 1

instance ToSchema All where declareNamedSchema = plain . paramSchemaToSchema
instance ToSchema Any where declareNamedSchema = plain . paramSchemaToSchema

Expand Down
5 changes: 5 additions & 0 deletions swagger2.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ library
, uuid-types >=1.0.2 && <1.1
if !impl(ghc >= 7.10)
build-depends: nats >=1.1.1 && <1.2
if !impl(ghc >= 8.0)
build-depends: semigroups >= 0.18.3 && <0.19
default-language: Haskell2010

test-suite spec
Expand All @@ -96,6 +98,9 @@ test-suite spec
, unordered-containers
, vector
, lens
if !impl(ghc >= 8.0)
build-depends: semigroups >= 0.18.3 && <0.19
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to specify bounds in test-suite (as they are inherited from library). This helps if/when semigroups-0.19 is released.


other-modules:
SpecCommon
Data.SwaggerSpec
Expand Down
12 changes: 12 additions & 0 deletions test/Data/Swagger/Schema/ValidationSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import "unordered-containers" Data.HashSet (HashSet)
import qualified "unordered-containers" Data.HashSet as HashSet
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.List.NonEmpty (NonEmpty(..), nonEmpty)
import Data.Map (Map)
import Data.Monoid (mempty)
import Data.Proxy
Expand Down Expand Up @@ -69,6 +70,7 @@ spec = do
-- prop "(Maybe [Int])" $ shouldValidate (Proxy :: Proxy (Maybe [Int]))
prop "(IntMap String)" $ shouldValidate (Proxy :: Proxy (IntMap String))
prop "(Set Bool)" $ shouldValidate (Proxy :: Proxy (Set Bool))
prop "(NonEmpty Bool)" $ shouldValidate (Proxy :: Proxy (NonEmpty Bool))
prop "(HashSet Bool)" $ shouldValidate (Proxy :: Proxy (HashSet Bool))
prop "(Either Int String)" $ shouldValidate (Proxy :: Proxy (Either Int String))
prop "(Int, String)" $ shouldValidate (Proxy :: Proxy (Int, String))
Expand Down Expand Up @@ -260,3 +262,13 @@ instance Arbitrary ZonedTime where

instance Arbitrary UTCTime where
arbitrary = UTCTime <$> arbitrary <*> fmap fromInteger (choose (0, 86400))

#if MIN_VERSION_QuickCheck(2,10,0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-- This instance was removed in QuickCheck 2.10 because of dependencies.
-- The instance is available in `quickcheck-instances` package, but that
-- introduces a ton of conflicts with the other instances here.

instance Arbitrary a => Arbitrary (NonEmpty a) where
arbitrary = liftA2 (:|) arbitrary arbitrary
shrink (x :| xs) = mapMaybe nonEmpty (shrink (x:xs))
#endif