Skip to content

Commit ccb9daa

Browse files
committed
Move swagger2 helper function upstream.
1 parent 0b828aa commit ccb9daa

File tree

3 files changed

+13
-68
lines changed

3 files changed

+13
-68
lines changed

servant-swagger.cabal

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ name: servant-swagger
22
version: 1.1.9
33
synopsis: Generate a Swagger/OpenAPI/OAS 2.0 specification for your servant API.
44
description:
5-
Swagger is a project used to describe and document RESTful APIs. The core of the
6-
project is the [OpenAPI Specification (OAS)](https://swagger.io/docs/specification/about/).
7-
This library implements v2.0 of the spec. Unlike Servant it is language-agnostic and thus is
8-
quite popular among developers in different languages. It has also existed for a longer time
5+
Swagger is a project used to describe and document RESTful APIs. The core of the
6+
project is the [OpenAPI Specification (OAS)](https://swagger.io/docs/specification/about/).
7+
This library implements v2.0 of the spec. Unlike Servant it is language-agnostic and thus is
8+
quite popular among developers in different languages. It has also existed for a longer time
99
and has more helpful tooling.
1010
.
1111
This package provides means to generate a Swagger/OAS specification for a Servant API
@@ -80,11 +80,11 @@ library
8080
, base-compat >=0.10.5 && <0.12
8181
, bytestring >=0.10.8.1 && <0.11
8282
, http-media >=0.7.1.3 && <0.9
83-
, insert-ordered-containers >=0.2.1.0 && <0.3
83+
, insert-ordered-containers >=0.2.2 && <0.3
8484
, lens >=4.17 && <4.20
8585
, servant >=0.17 && <0.18
8686
, singleton-bool >=0.1.4 && <0.2
87-
, swagger2 >=2.3.0.1 && <2.7
87+
, swagger2 >=2.4.1 && <2.7
8888
, text >=1.2.3.0 && <1.3
8989
, unordered-containers >=0.2.9.0 && <0.3
9090

src/Servant/Swagger/Internal/Test.hs

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ validateEveryToJSON
8585
-> Spec
8686
validateEveryToJSON _ = props
8787
(Proxy :: Proxy [ToJSON, ToSchema])
88-
(maybeCounterExample . prettyValidateWith validateToJSON)
88+
(maybeCounterExample . renderValidationErrors validateToJSON)
8989
(Proxy :: Proxy (BodyTypes JSON api))
9090

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

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

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

stack.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
resolver: lts-13.25
1+
resolver: lts-16.7
22
packages:
33
- '.'
44
- example/
5+
6+
extra-deps:
7+
- servant-0.17
8+
- servant-server-0.17

0 commit comments

Comments
 (0)