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

Move swagger2 helper function upstream. #108

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions servant-swagger.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ library
, base-compat >=0.10.5 && <0.12
, bytestring >=0.10.8.1 && <0.11
, http-media >=0.7.1.3 && <0.9
, insert-ordered-containers >=0.2.1.0 && <0.3
, insert-ordered-containers >=0.2.2 && <0.3
, lens >=4.17 && <4.20
, servant >=0.17 && <0.18
, singleton-bool >=0.1.4 && <0.2
, swagger2 >=2.3.0.1 && <2.7
, swagger2 >=2.5 && <2.7
, text >=1.2.3.0 && <1.3
, unordered-containers >=0.2.9.0 && <0.3

Expand Down
63 changes: 2 additions & 61 deletions src/Servant/Swagger/Internal/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ validateEveryToJSON
-> Spec
validateEveryToJSON _ = props
(Proxy :: Proxy [ToJSON, ToSchema])
(maybeCounterExample . prettyValidateWith validateToJSON)
(maybeCounterExample . renderValidationErrors validateToJSON)
(Proxy :: Proxy (BodyTypes JSON api))

-- | Verify that every type used with @'JSON'@ content type in a servant API
Expand All @@ -98,7 +98,7 @@ validateEveryToJSONWithPatternChecker :: forall proxy api. TMap (Every [Typeable
-> Spec
validateEveryToJSONWithPatternChecker checker _ = props
(Proxy :: Proxy [ToJSON, ToSchema])
(maybeCounterExample . prettyValidateWith (validateToJSONWithPatternChecker checker))
(maybeCounterExample . renderValidationErrors (validateToJSONWithPatternChecker checker))
(Proxy :: Proxy (BodyTypes JSON api))

-- * QuickCheck-related stuff
Expand Down Expand Up @@ -137,65 +137,6 @@ props _ f px = sequence_ specs
aprop :: forall p' a. (EveryTF cs a, Typeable a, Show a, Arbitrary a) => p' a -> Spec
aprop _ = prop (show (typeOf (undefined :: a))) (f :: a -> Property)

-- | Pretty print validation errors
-- together with actual JSON and Swagger Schema
-- (using 'encodePretty').
--
-- >>> import Data.Aeson
-- >>> import Data.Foldable (traverse_)
-- >>> data Person = Person { name :: String, phone :: Integer } deriving (Generic)
-- >>> instance ToJSON Person where toJSON p = object [ "name" .= name p ]
-- >>> instance ToSchema Person
-- >>> let person = Person { name = "John", phone = 123456 }
-- >>> traverse_ putStrLn $ prettyValidateWith validateToJSON person
-- Validation against the schema fails:
-- * property "phone" is required, but not found in "{\"name\":\"John\"}"
-- <BLANKLINE>
-- JSON value:
-- {
-- "name": "John"
-- }
-- <BLANKLINE>
-- Swagger Schema:
-- {
-- "required": [
-- "name",
-- "phone"
-- ],
-- "type": "object",
-- "properties": {
-- "phone": {
-- "type": "integer"
-- },
-- "name": {
-- "type": "string"
-- }
-- }
-- }
-- <BLANKLINE>
--
-- FIXME: this belongs in "Data.Swagger.Schema.Validation" (in @swagger2@).
prettyValidateWith
:: forall a. (ToJSON a, ToSchema a)
=> (a -> [ValidationError]) -> a -> Maybe String
prettyValidateWith f x =
case f x of
[] -> Nothing
errors -> Just $ unlines
[ "Validation against the schema fails:"
, unlines (map (" * " ++) errors)
, "JSON value:"
, ppJSONString json
, ""
, "Swagger Schema:"
, ppJSONString (toJSON schema)
]
where
ppJSONString = TL.unpack . TL.decodeUtf8 . encodePretty

json = toJSON x
schema = toSchema (Proxy :: Proxy a)

-- | Provide a counterexample if there is any.
maybeCounterExample :: Maybe String -> Property
maybeCounterExample Nothing = property True
Expand Down
6 changes: 5 additions & 1 deletion stack.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
resolver: lts-13.25
resolver: lts-16.7
packages:
- '.'
- example/

extra-deps:
- servant-0.17
- servant-server-0.17