Skip to content

Commit 853b839

Browse files
committed
Move swagger2 helper function upstream.
1 parent 98ff31c commit 853b839

File tree

3 files changed

+14
-64
lines changed

3 files changed

+14
-64
lines changed

servant-swagger.cabal

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,12 @@ library
7979
, base-compat >=0.10.5 && <0.11
8080
, bytestring >=0.10.8.1 && <0.11
8181
, http-media >=0.7.1.3 && <0.9
82-
, insert-ordered-containers >=0.2.1.0 && <0.3
82+
, insert-ordered-containers >=0.2.2 && <0.3
8383
, lens >=4.17 && <4.18
8484
, servant >=0.15 && <0.17
8585
, singleton-bool >=0.1.4 && <0.2
86-
, swagger2 >=2.3.0.1 && <2.5
86+
, swagger2 >=2.4.1 && <2.5
87+
-- needs https://github.com/GetShopTV/swagger2/pull/195
8788
, text >=1.2.3.0 && <1.3
8889
, unordered-containers >=0.2.9.0 && <0.3
8990

src/Servant/Swagger/Internal/Test.hs

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ validateEveryToJSON :: forall proxy api. TMap (Every [Typeable, Show, Arbitrary,
8282
-> Spec
8383
validateEveryToJSON _ = props
8484
(Proxy :: Proxy [ToJSON, ToSchema])
85-
(maybeCounterExample . prettyValidateWith validateToJSON)
85+
(maybeCounterExample . renderValidationErrors validateToJSON)
8686
(Proxy :: Proxy (BodyTypes JSON api))
8787

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

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

137-
-- | Pretty print validation errors
138-
-- together with actual JSON and Swagger Schema
139-
-- (using 'encodePretty').
140-
--
141-
-- >>> import Data.Aeson
142-
-- >>> import Data.Foldable (traverse_)
143-
-- >>> data Person = Person { name :: String, phone :: Integer } deriving (Generic)
144-
-- >>> instance ToJSON Person where toJSON p = object [ "name" .= name p ]
145-
-- >>> instance ToSchema Person
146-
-- >>> let person = Person { name = "John", phone = 123456 }
147-
-- >>> traverse_ putStrLn $ prettyValidateWith validateToJSON person
148-
-- Validation against the schema fails:
149-
-- * property "phone" is required, but not found in "{\"name\":\"John\"}"
150-
-- <BLANKLINE>
151-
-- JSON value:
152-
-- {
153-
-- "name": "John"
154-
-- }
155-
-- <BLANKLINE>
156-
-- Swagger Schema:
157-
-- {
158-
-- "required": [
159-
-- "name",
160-
-- "phone"
161-
-- ],
162-
-- "type": "object",
163-
-- "properties": {
164-
-- "phone": {
165-
-- "type": "integer"
166-
-- },
167-
-- "name": {
168-
-- "type": "string"
169-
-- }
170-
-- }
171-
-- }
172-
-- <BLANKLINE>
173-
--
174-
-- FIXME: this belongs in "Data.Swagger.Schema.Validation" (in @swagger2@).
175-
prettyValidateWith
176-
:: forall a. (ToJSON a, ToSchema a)
177-
=> (a -> [ValidationError]) -> a -> Maybe String
178-
prettyValidateWith f x =
179-
case f x of
180-
[] -> Nothing
181-
errors -> Just $ unlines
182-
[ "Validation against the schema fails:"
183-
, unlines (map (" * " ++) errors)
184-
, "JSON value:"
185-
, ppJSONString json
186-
, ""
187-
, "Swagger Schema:"
188-
, ppJSONString (toJSON schema)
189-
]
190-
where
191-
ppJSONString = TL.unpack . TL.decodeUtf8 . encodePretty
192-
193-
json = toJSON x
194-
schema = toSchema (Proxy :: Proxy a)
195-
196137
-- | Provide a counterexample if there is any.
197138
maybeCounterExample :: Maybe String -> Property
198139
maybeCounterExample Nothing = property True

stack.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
resolver: lts-11.7
1+
resolver: lts-13.23
22
packages:
33
- '.'
44
- example/
5+
6+
extra-deps:
7+
- git: https://github.com/wireapp/swagger2 # see https://github.com/GetShopTV/swagger2/pull/195
8+
commit: 7ef5bfc60028d71a33db75a2da93676d336a93e1
9+
- aeson-1.4.2.0
10+
- base-compat-batteries-0.10.4
11+
- contravariant-1.5
12+
- insert-ordered-containers-0.2.2

0 commit comments

Comments
 (0)