git.iko.soy/typeable/openapi3
on master help sign in
rename `data Swagger` to `data OpenApi`
commit03b5707da535e8520e80d600a35632dae7bebda5 authorPaweł Nosal <pawel.nosal@ocado.com> dateTue 4 Aug 2020, 08:49 (6 years ago) parentd7deede
7 files changed+43−43
examples/hackage.hs+2−2 src/Data/OpenApi.hs+6−6 src/Data/OpenApi/Internal.hs+15−15 src/Data/OpenApi/Lens.hs+1−1 src/Data/OpenApi/Operation.hs+13−13 src/Data/OpenApi/Optics.hs+2−2 test/Data/OpenApiSpec.hs+4−4
examples/hackage.hs+2−2
@@ -47,12 +47,12 @@ data UserDetailed = UserDetailed
4747newtype Package = Package { packageName :: Text }
4848 deriving (Generic, ToSchema)
4949
50−hackageSwagger :: Swagger
50+hackageSwagger :: OpenApi
5151hackageSwagger = spec & components.schemas .~ defs
5252 where
5353 (defs, spec) = runDeclare declareHackageSwagger mempty
5454
55−declareHackageSwagger :: Declare (Definitions Schema) Swagger
55+declareHackageSwagger :: Declare (Definitions Schema) OpenApi
5656declareHackageSwagger = do
5757 -- param schemas
5858 let usernameParamSchema = toParamSchema (Proxy :: Proxy Username)
src/Data/OpenApi.hs+6−6
@@ -37,7 +37,7 @@ module Data.OpenApi (
3737 module Data.OpenApi.Schema.Validation,
3838
3939 -- * Swagger specification
40− Swagger(..),
40+ OpenApi(..),
4141 Server(..),
4242 ServerVariable(..),
4343 Components(..),
@@ -152,7 +152,7 @@ import Data.OpenApi.Internal
152152--
153153-- In this library you can use @'mempty'@ for a default/empty value. For instance:
154154--
155−-- >>> BSL.putStrLn $ encode (mempty :: Swagger)
155+-- >>> BSL.putStrLn $ encode (mempty :: OpenApi)
156156-- {"openapi":"3.0.0","info":{"version":"","title":""},"components":{}}
157157--
158158-- As you can see some spec properties (e.g. @"version"@) are there even when the spec is empty.
@@ -173,14 +173,14 @@ import Data.OpenApi.Internal
173173--
174174-- @
175175-- \-\- /account subAPI specification
176−-- accountAPI :: Swagger
176+-- accountAPI :: OpenApi
177177--
178178-- \-\- /task subAPI specification
179−-- taskAPI :: Swagger
179+-- taskAPI :: OpenApi
180180--
181181-- \-\- while API specification is just a combination
182182-- \-\- of subAPIs' specifications
183−-- api :: Swagger
183+-- api :: OpenApi
184184-- api = accountAPI <> taskAPI
185185-- @
186186
@@ -193,7 +193,7 @@ import Data.OpenApi.Internal
193193-- make it fairly simple to construct/modify any part of the specification:
194194--
195195-- >>> :{
196−-- BSL.putStrLn $ encode $ (mempty :: Swagger)
196+-- BSL.putStrLn $ encode $ (mempty :: OpenApi)
197197-- & components . schemas .~ [ ("User", mempty & type_ ?~ SwaggerString) ]
198198-- & paths .~
199199-- [ ("/user", mempty & get ?~ (mempty
src/Data/OpenApi/Internal.hs+15−15
@@ -65,38 +65,38 @@ import Data.OpenApi.Internal.AesonUtils (sopSwaggerGenericToEncoding)
6565type Definitions = InsOrdHashMap Text
6666
6767-- | This is the root document object for the API specification.
68−data Swagger = Swagger
68+data OpenApi = OpenApi
6969 { -- | Provides metadata about the API.
7070 -- The metadata can be used by the clients if needed.
71− _swaggerInfo :: Info
71+ _openApiInfo :: Info
7272
7373 -- | An array of Server Objects, which provide connectivity information
7474 -- to a target server. If the servers property is not provided, or is an empty array,
7575 -- the default value would be a 'Server' object with a url value of @/@.
76− , _swaggerServers :: [Server]
76+ , _openApiServers :: [Server]
7777
7878 -- | The available paths and operations for the API.
79− , _swaggerPaths :: InsOrdHashMap FilePath PathItem
79+ , _openApiPaths :: InsOrdHashMap FilePath PathItem
8080
8181 -- | An element to hold various schemas for the specification.
82− , _swaggerComponents :: Components
82+ , _openApiComponents :: Components
8383
8484 -- | A declaration of which security mechanisms can be used across the API.
8585 -- The list of values includes alternative security requirement objects that can be used.
8686 -- Only one of the security requirement objects need to be satisfied to authorize a request.
8787 -- Individual operations can override this definition.
8888 -- To make security optional, an empty security requirement can be included in the array.
89− , _swaggerSecurity :: [SecurityRequirement]
89+ , _openApiSecurity :: [SecurityRequirement]
9090
9191 -- | A list of tags used by the specification with additional metadata.
9292 -- The order of the tags can be used to reflect on their order by the parsing tools.
9393 -- Not all tags that are used by the 'Operation' Object must be declared.
9494 -- The tags that are not declared MAY be organized randomly or based on the tools' logic.
9595 -- Each tag name in the list MUST be unique.
96− , _swaggerTags :: InsOrdHashSet Tag
96+ , _openApiTags :: InsOrdHashSet Tag
9797
9898 -- | Additional external documentation.
99− , _swaggerExternalDocs :: Maybe ExternalDocs
99+ , _openApiExternalDocs :: Maybe ExternalDocs
100100 } deriving (Eq, Show, Generic, Data, Typeable)
101101
102102-- | The object provides metadata about the API.
@@ -936,7 +936,7 @@ deriveGeneric ''MediaTypeObject
936936deriveGeneric ''Responses
937937deriveGeneric ''SecurityScheme
938938deriveGeneric ''Schema
939−deriveGeneric ''Swagger
939+deriveGeneric ''OpenApi
940940deriveGeneric ''Example
941941deriveGeneric ''Encoding
942942deriveGeneric ''Link
@@ -945,9 +945,9 @@ deriveGeneric ''Link
945945-- Monoid instances
946946-- =======================================================================
947947
948−instance Semigroup Swagger where
948+instance Semigroup OpenApi where
949949 (<>) = genericMappend
950−instance Monoid Swagger where
950+instance Monoid OpenApi where
951951 mempty = genericMempty
952952 mappend = (<>)
953953
@@ -1243,9 +1243,9 @@ instance ToJSON SecuritySchemeType where
12431243 , "openIdConnectUrl" .= url
12441244 ]
12451245
1246−instance ToJSON Swagger where
1246+instance ToJSON OpenApi where
12471247 toJSON a = sopSwaggerGenericToJSON a &
1248− if InsOrdHashMap.null (_swaggerPaths a)
1248+ if InsOrdHashMap.null (_openApiPaths a)
12491249 then (<+> object ["paths" .= object []])
12501250 else id
12511251 toEncoding = sopSwaggerGenericToEncoding
@@ -1386,7 +1386,7 @@ instance FromJSON SecuritySchemeType where
13861386 _ -> empty
13871387 parseJSON _ = empty
13881388
1389−instance FromJSON Swagger where
1389+instance FromJSON OpenApi where
13901390 parseJSON = sopSwaggerGenericParseJSON
13911391
13921392instance FromJSON Server where
@@ -1526,7 +1526,7 @@ instance HasSwaggerAesonOptions SecurityScheme where
15261526 swaggerAesonOptions _ = mkSwaggerAesonOptions "securityScheme" & saoSubObject ?~ "type"
15271527instance HasSwaggerAesonOptions Schema where
15281528 swaggerAesonOptions _ = mkSwaggerAesonOptions "schema" & saoSubObject ?~ "paramSchema"
1529−instance HasSwaggerAesonOptions Swagger where
1529+instance HasSwaggerAesonOptions OpenApi where
15301530 swaggerAesonOptions _ = mkSwaggerAesonOptions "swagger" & saoAdditionalPairs .~ [("openapi", "3.0.0")]
15311531instance HasSwaggerAesonOptions Example where
15321532 swaggerAesonOptions _ = mkSwaggerAesonOptions "example"
src/Data/OpenApi/Lens.hs+1−1
@@ -26,7 +26,7 @@ import Data.Text (Text)
2626
2727-- * Classy lenses
2828
29−makeFields ''Swagger
29+makeFields ''OpenApi
3030makeFields ''Components
3131makeFields ''Server
3232makeFields ''RequestBody
src/Data/OpenApi/Operation.hs+13−13
@@ -59,10 +59,10 @@ import qualified Data.HashSet.InsOrd as InsOrdHS
5959-- | Prepend path piece to all operations of the spec.
6060-- Leading and trailing slashes are trimmed/added automatically.
6161--
62−-- >>> let api = (mempty :: Swagger) & paths .~ [("/info", mempty)]
62+-- >>> let api = (mempty :: OpenApi) & paths .~ [("/info", mempty)]
6363-- >>> BSL.putStrLn $ encode $ prependPath "user/{user_id}" api ^. paths
6464-- {"/user/{user_id}/info":{}}
65−prependPath :: FilePath -> Swagger -> Swagger
65+prependPath :: FilePath -> OpenApi -> OpenApi
6666prependPath path = paths %~ InsOrdHashMap.mapKeys (path </>)
6767 where
6868 x </> y = case trim y of
@@ -72,7 +72,7 @@ prependPath path = paths %~ InsOrdHashMap.mapKeys (path </>)
7272 trim = dropWhile (== '/') . dropWhileEnd (== '/')
7373
7474-- | All operations of a Swagger spec.
75−allOperations :: Traversal' Swagger Operation
75+allOperations :: Traversal' OpenApi Operation
7676allOperations = paths.traverse.template
7777
7878-- | @'operationsOf' sub@ will traverse only those operations
@@ -80,13 +80,13 @@ allOperations = paths.traverse.template
8080-- by both path and method.
8181--
8282-- >>> let ok = (mempty :: Operation) & at 200 ?~ "OK"
83−-- >>> let api = (mempty :: Swagger) & paths .~ [("/user", mempty & get ?~ ok & post ?~ ok)]
84−-- >>> let sub = (mempty :: Swagger) & paths .~ [("/user", mempty & get ?~ mempty)]
83+-- >>> let api = (mempty :: OpenApi) & paths .~ [("/user", mempty & get ?~ ok & post ?~ ok)]
84+-- >>> let sub = (mempty :: OpenApi) & paths .~ [("/user", mempty & get ?~ mempty)]
8585-- >>> BSL.putStrLn $ encode api
8686-- {"openapi":"3.0.0","info":{"version":"","title":""},"paths":{"/user":{"get":{"responses":{"200":{"description":"OK"}}},"post":{"responses":{"200":{"description":"OK"}}}}},"components":{}}
8787-- >>> BSL.putStrLn $ encode $ api & operationsOf sub . at 404 ?~ "Not found"
8888-- {"openapi":"3.0.0","info":{"version":"","title":""},"paths":{"/user":{"get":{"responses":{"404":{"description":"Not found"},"200":{"description":"OK"}}},"post":{"responses":{"200":{"description":"OK"}}}}},"components":{}}
89−operationsOf :: Swagger -> Traversal' Swagger Operation
89+operationsOf :: OpenApi -> Traversal' OpenApi Operation
9090operationsOf sub = paths.itraversed.withIndex.subops
9191 where
9292 -- | Traverse operations that correspond to paths and methods of the sub API.
@@ -108,12 +108,12 @@ operationsOf sub = paths.itraversed.withIndex.subops
108108-- @
109109-- 'applyTags' = 'applyTagsFor' 'allOperations'
110110-- @
111−applyTags :: [Tag] -> Swagger -> Swagger
111+applyTags :: [Tag] -> OpenApi -> OpenApi
112112applyTags = applyTagsFor allOperations
113113
114114-- | Apply tags to a part of Swagger spec and update the global
115115-- list of tags.
116−applyTagsFor :: Traversal' Swagger Operation -> [Tag] -> Swagger -> Swagger
116+applyTagsFor :: Traversal' OpenApi Operation -> [Tag] -> OpenApi -> OpenApi
117117applyTagsFor ops ts swag = swag
118118 & ops . tags %~ (<> InsOrdHS.fromList (map _tagName ts))
119119 & tags %~ (<> InsOrdHS.fromList ts)
@@ -141,13 +141,13 @@ declareResponse cType proxy = do
141141--
142142-- Example:
143143--
144−-- >>> let api = (mempty :: Swagger) & paths .~ [("/user", mempty & get ?~ mempty)]
144+-- >>> let api = (mempty :: OpenApi) & paths .~ [("/user", mempty & get ?~ mempty)]
145145-- >>> let res = declareResponse "application/json" (Proxy :: Proxy Day)
146146-- >>> BSL.putStrLn $ encode $ api & setResponse 200 res
147147-- {"openapi":"3.0.0","info":{"version":"","title":""},"paths":{"/user":{"get":{"responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/Day"}}},"description":""}}}}},"components":{"schemas":{"Day":{"example":"2016-07-22","format":"date","type":"string"}}}}
148148--
149149-- See also @'setResponseWith'@.
150−setResponse :: HttpStatusCode -> Declare (Definitions Schema) Response -> Swagger -> Swagger
150+setResponse :: HttpStatusCode -> Declare (Definitions Schema) Response -> OpenApi -> OpenApi
151151setResponse = setResponseFor allOperations
152152
153153-- | Set or update response for all operations.
@@ -161,7 +161,7 @@ setResponse = setResponseFor allOperations
161161-- @
162162--
163163-- See also @'setResponse'@.
164−setResponseWith :: (Response -> Response -> Response) -> HttpStatusCode -> Declare (Definitions Schema) Response -> Swagger -> Swagger
164+setResponseWith :: (Response -> Response -> Response) -> HttpStatusCode -> Declare (Definitions Schema) Response -> OpenApi -> OpenApi
165165setResponseWith = setResponseForWith allOperations
166166
167167-- | Set response for specified operations.
@@ -170,7 +170,7 @@ setResponseWith = setResponseForWith allOperations
170170-- If the response already exists it will be overwritten.
171171--
172172-- See also @'setResponseForWith'@.
173−setResponseFor :: Traversal' Swagger Operation -> HttpStatusCode -> Declare (Definitions Schema) Response -> Swagger -> Swagger
173+setResponseFor :: Traversal' OpenApi Operation -> HttpStatusCode -> Declare (Definitions Schema) Response -> OpenApi -> OpenApi
174174setResponseFor ops code dres swag = swag
175175 & components.schemas %~ (<> defs)
176176 & ops . at code ?~ Inline res
@@ -184,7 +184,7 @@ setResponseFor ops code dres swag = swag
184184-- then just the new response is used.
185185--
186186-- See also @'setResponseFor'@.
187−setResponseForWith :: Traversal' Swagger Operation -> (Response -> Response -> Response) -> HttpStatusCode -> Declare (Definitions Schema) Response -> Swagger -> Swagger
187+setResponseForWith :: Traversal' OpenApi Operation -> (Response -> Response -> Response) -> HttpStatusCode -> Declare (Definitions Schema) Response -> OpenApi -> OpenApi
188188setResponseForWith ops f code dres swag = swag
189189 & components.schemas %~ (<> defs)
190190 & ops . at code %~ Just . Inline . combine
src/Data/OpenApi/Optics.hs+2−2
@@ -22,7 +22,7 @@
2222-- Example from the "Data.OpenApi" module using @optics@:
2323--
2424-- >>> :{
25−-- BSL.putStrLn $ encode $ (mempty :: Swagger)
25+-- BSL.putStrLn $ encode $ (mempty :: OpenApi)
2626-- & #components % #schemas .~ [ ("User", mempty & #type ?~ SwaggerString) ]
2727-- & #paths .~
2828-- [ ("/user", mempty & #get ?~ (mempty
@@ -65,7 +65,7 @@ import Optics.TH
6565
6666-- Lenses
6767
68−makeFieldLabels ''Swagger
68+makeFieldLabels ''OpenApi
6969makeFieldLabels ''Components
7070makeFieldLabels ''Server
7171makeFieldLabels ''ServerVariable
test/Data/OpenApiSpec.hs+4−4
@@ -42,9 +42,9 @@ spec = do
4242 context "Todo Example" $ swaggerExample <=> swaggerExampleJSON
4343 context "PetStore Example" $ do
4444 it "decodes successfully" $ do
45− fromJSON petstoreExampleJSON `shouldSatisfy` (\x -> case x of Success (_ :: Swagger) -> True; _ -> False)
45+ fromJSON petstoreExampleJSON `shouldSatisfy` (\x -> case x of Success (_ :: OpenApi) -> True; _ -> False)
4646 it "roundtrips: fmap toJSON . fromJSON" $ do
47− (toJSON :: Swagger -> Value) <$> fromJSON petstoreExampleJSON `shouldBe` Success petstoreExampleJSON
47+ (toJSON :: OpenApi -> Value) <$> fromJSON petstoreExampleJSON `shouldBe` Success petstoreExampleJSON
4848
4949main :: IO ()
5050main = hspec spec
@@ -531,7 +531,7 @@ oAuth2SecurityDefinitionsExampleJSON = [aesonQQ|
531531-- Swagger object
532532-- =======================================================================
533533
534−emptyPathsFieldExample :: Swagger
534+emptyPathsFieldExample :: OpenApi
535535emptyPathsFieldExample = mempty
536536
537537emptyPathsFieldExampleJSON :: Value
@@ -544,7 +544,7 @@ emptyPathsFieldExampleJSON = [aesonQQ|
544544}
545545|]
546546
547−swaggerExample :: Swagger
547+swaggerExample :: OpenApi
548548swaggerExample = mempty
549549 -- & basePath ?~ "/"
550550 -- & schemes ?~ [Http]