Skip to content

Commit

Permalink
Merge pull request #4 from paweln1986/rename-swagger-to-openApi
Browse files Browse the repository at this point in the history
Rename swagger to open api
  • Loading branch information
maksbotan authored Aug 4, 2020
2 parents a30c324 + 98e9537 commit bd9df53
Show file tree
Hide file tree
Showing 14 changed files with 259 additions and 259 deletions.
14 changes: 7 additions & 7 deletions examples/hackage.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ instance ToSchema UserSummary where
usernameSchema <- declareSchemaRef (Proxy :: Proxy Username)
useridSchema <- declareSchemaRef (Proxy :: Proxy Int)
return $ NamedSchema (Just "UserSummary") $ mempty
& type_ ?~ SwaggerObject
& type_ ?~ OpenApiObject
& properties .~
[ ("summaryUsername", usernameSchema )
, ("summaryUserid" , useridSchema )
Expand All @@ -47,13 +47,13 @@ data UserDetailed = UserDetailed
newtype Package = Package { packageName :: Text }
deriving (Generic, ToSchema)

hackageSwagger :: Swagger
hackageSwagger = spec & components.schemas .~ defs
hackageOpenApi :: OpenApi
hackageOpenApi = spec & components.schemas .~ defs
where
(defs, spec) = runDeclare declareHackageSwagger mempty
(defs, spec) = runDeclare declareHackageOpenApi mempty

declareHackageSwagger :: Declare (Definitions Schema) Swagger
declareHackageSwagger = do
declareHackageOpenApi :: Declare (Definitions Schema) OpenApi
declareHackageOpenApi = do
-- param schemas
let usernameParamSchema = toParamSchema (Proxy :: Proxy Username)

Expand All @@ -78,5 +78,5 @@ declareHackageSwagger = do
]

main :: IO ()
main = putStrLn . read . show . encode $ hackageSwagger
main = putStrLn . read . show . encode $ hackageOpenApi

22 changes: 11 additions & 11 deletions src/Data/OpenApi.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module Data.OpenApi (
module Data.OpenApi.Schema.Validation,

-- * Swagger specification
Swagger(..),
OpenApi(..),
Server(..),
ServerVariable(..),
Components(..),
Expand All @@ -56,7 +56,7 @@ module Data.OpenApi (
TagName,

-- ** Types and formats
SwaggerType(..),
OpenApiType(..),
Format,
Definitions,
Style(..),
Expand All @@ -75,7 +75,7 @@ module Data.OpenApi (
-- ** Schemas
Schema(..),
NamedSchema(..),
SwaggerItems(..),
OpenApiItems(..),
Xml(..),
Pattern,
AdditionalProperties(..),
Expand Down Expand Up @@ -152,7 +152,7 @@ import Data.OpenApi.Internal
--
-- In this library you can use @'mempty'@ for a default/empty value. For instance:
--
-- >>> BSL.putStrLn $ encode (mempty :: Swagger)
-- >>> BSL.putStrLn $ encode (mempty :: OpenApi)
-- {"openapi":"3.0.0","info":{"version":"","title":""},"components":{}}
--
-- As you can see some spec properties (e.g. @"version"@) are there even when the spec is empty.
Expand All @@ -173,14 +173,14 @@ import Data.OpenApi.Internal
--
-- @
-- \-\- /account subAPI specification
-- accountAPI :: Swagger
-- accountAPI :: OpenApi
--
-- \-\- /task subAPI specification
-- taskAPI :: Swagger
-- taskAPI :: OpenApi
--
-- \-\- while API specification is just a combination
-- \-\- of subAPIs' specifications
-- api :: Swagger
-- api :: OpenApi
-- api = accountAPI <> taskAPI
-- @

Expand All @@ -193,8 +193,8 @@ import Data.OpenApi.Internal
-- make it fairly simple to construct/modify any part of the specification:
--
-- >>> :{
-- BSL.putStrLn $ encode $ (mempty :: Swagger)
-- & components . schemas .~ [ ("User", mempty & type_ ?~ SwaggerString) ]
-- BSL.putStrLn $ encode $ (mempty :: OpenApi)
-- & components . schemas .~ [ ("User", mempty & type_ ?~ OpenApiString) ]
-- & paths .~
-- [ ("/user", mempty & get ?~ (mempty
-- & at 200 ?~ ("OK" & _Inline.content.at "application/json" ?~ (mempty & schema ?~ Ref (Reference "User")))
Expand All @@ -216,7 +216,7 @@ import Data.OpenApi.Internal
-- {"description":"No content"}
-- >>> :{
-- BSL.putStrLn $ encode $ (mempty :: Schema)
-- & type_ ?~ SwaggerBoolean
-- & type_ ?~ OpenApiBoolean
-- & description ?~ "To be or not to be"
-- :}
-- {"type":"boolean","description":"To be or not to be"}
Expand Down Expand Up @@ -244,7 +244,7 @@ import Data.OpenApi.Internal
-- @'ParamSchema'@ and @'Schema'@ are the two core types for data model specification.
--
-- @'ParamSchema' t@ specifies all the common properties, available for every data schema.
-- The @t@ parameter imposes some restrictions on @type@ and @items@ properties (see @'SwaggerType'@ and @'SwaggerItems'@).
-- The @t@ parameter imposes some restrictions on @type@ and @items@ properties (see @'OpenApiType'@ and @'OpenApiItems'@).
--
-- @'Schema'@ is used for request and response bodies and allows specifying objects
-- with properties in addition to what @'ParamSchema'@ provides.
Expand Down
94 changes: 47 additions & 47 deletions src/Data/OpenApi/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,38 +65,38 @@ import Data.OpenApi.Internal.AesonUtils (sopSwaggerGenericToEncoding)
type Definitions = InsOrdHashMap Text

-- | This is the root document object for the API specification.
data Swagger = Swagger
data OpenApi = OpenApi
{ -- | Provides metadata about the API.
-- The metadata can be used by the clients if needed.
_swaggerInfo :: Info
_openApiInfo :: Info

-- | An array of Server Objects, which provide connectivity information
-- to a target server. If the servers property is not provided, or is an empty array,
-- the default value would be a 'Server' object with a url value of @/@.
, _swaggerServers :: [Server]
, _openApiServers :: [Server]

-- | The available paths and operations for the API.
, _swaggerPaths :: InsOrdHashMap FilePath PathItem
, _openApiPaths :: InsOrdHashMap FilePath PathItem

-- | An element to hold various schemas for the specification.
, _swaggerComponents :: Components
, _openApiComponents :: Components

-- | A declaration of which security mechanisms can be used across the API.
-- The list of values includes alternative security requirement objects that can be used.
-- Only one of the security requirement objects need to be satisfied to authorize a request.
-- Individual operations can override this definition.
-- To make security optional, an empty security requirement can be included in the array.
, _swaggerSecurity :: [SecurityRequirement]
, _openApiSecurity :: [SecurityRequirement]

-- | A list of tags used by the specification with additional metadata.
-- The order of the tags can be used to reflect on their order by the parsing tools.
-- Not all tags that are used by the 'Operation' Object must be declared.
-- The tags that are not declared MAY be organized randomly or based on the tools' logic.
-- Each tag name in the list MUST be unique.
, _swaggerTags :: InsOrdHashSet Tag
, _openApiTags :: InsOrdHashSet Tag

-- | Additional external documentation.
, _swaggerExternalDocs :: Maybe ExternalDocs
, _openApiExternalDocs :: Maybe ExternalDocs
} deriving (Eq, Show, Generic, Data, Typeable)

-- | The object provides metadata about the API.
Expand Down Expand Up @@ -571,27 +571,27 @@ data Link = Link
, _linkServer :: Maybe Server
} deriving (Eq, Show, Generic, Typeable, Data)

-- | Items for @'SwaggerArray'@ schemas.
-- | Items for @'OpenApiArray'@ schemas.
--
-- __Warning__: OpenAPI 3.0 does not support tuple arrays. However, OpenAPI 3.1 will, as
-- it will incorporate Json Schema mostly verbatim.
--
-- @'SwaggerItemsObject'@ should be used to specify homogenous array @'Schema'@s.
-- @'OpenApiItemsObject'@ should be used to specify homogenous array @'Schema'@s.
--
-- @'SwaggerItemsArray'@ should be used to specify tuple @'Schema'@s.
data SwaggerItems where
SwaggerItemsObject :: Referenced Schema -> SwaggerItems
SwaggerItemsArray :: [Referenced Schema] -> SwaggerItems
-- @'OpenApiItemsArray'@ should be used to specify tuple @'Schema'@s.
data OpenApiItems where
OpenApiItemsObject :: Referenced Schema -> OpenApiItems
OpenApiItemsArray :: [Referenced Schema] -> OpenApiItems
deriving (Eq, Show, Typeable, Data)

data SwaggerType where
SwaggerString :: SwaggerType
SwaggerNumber :: SwaggerType
SwaggerInteger :: SwaggerType
SwaggerBoolean :: SwaggerType
SwaggerArray :: SwaggerType
SwaggerNull :: SwaggerType
SwaggerObject :: SwaggerType
data OpenApiType where
OpenApiString :: OpenApiType
OpenApiNumber :: OpenApiType
OpenApiInteger :: OpenApiType
OpenApiBoolean :: OpenApiType
OpenApiArray :: OpenApiType
OpenApiNull :: OpenApiType
OpenApiObject :: OpenApiType
deriving (Eq, Show, Typeable, Generic, Data)

data ParamLocation
Expand Down Expand Up @@ -643,9 +643,9 @@ data Schema = Schema
-- Unlike JSON Schema this value MUST conform to the defined type for this parameter.
_schemaDefault :: Maybe Value

, _schemaType :: Maybe SwaggerType
, _schemaType :: Maybe OpenApiType
, _schemaFormat :: Maybe Format
, _schemaItems :: Maybe SwaggerItems
, _schemaItems :: Maybe OpenApiItems
, _schemaMaximum :: Maybe Scientific
, _schemaExclusiveMaximum :: Maybe Bool
, _schemaMinimum :: Maybe Scientific
Expand Down Expand Up @@ -680,7 +680,7 @@ data NamedSchema = NamedSchema

data Xml = Xml
{ -- | Replaces the name of the element/attribute used for the described schema property.
-- When defined within the @'SwaggerItems'@ (items), it will affect the name of the individual XML elements within the list.
-- When defined within the @'OpenApiItems'@ (items), it will affect the name of the individual XML elements within the list.
-- When defined alongside type being array (outside the items),
-- it will affect the wrapping element and only if wrapped is true.
-- If wrapped is false, it will be ignored.
Expand Down Expand Up @@ -936,7 +936,7 @@ deriveGeneric ''MediaTypeObject
deriveGeneric ''Responses
deriveGeneric ''SecurityScheme
deriveGeneric ''Schema
deriveGeneric ''Swagger
deriveGeneric ''OpenApi
deriveGeneric ''Example
deriveGeneric ''Encoding
deriveGeneric ''Link
Expand All @@ -945,9 +945,9 @@ deriveGeneric ''Link
-- Monoid instances
-- =======================================================================

instance Semigroup Swagger where
instance Semigroup OpenApi where
(<>) = genericMappend
instance Monoid Swagger where
instance Monoid OpenApi where
mempty = genericMempty
mappend = (<>)

Expand Down Expand Up @@ -1085,8 +1085,8 @@ instance (Eq a, Hashable a) => SwaggerMonoid (InsOrdHashSet a)
instance SwaggerMonoid MimeList
deriving instance SwaggerMonoid URL

instance SwaggerMonoid SwaggerType where
swaggerMempty = SwaggerString
instance SwaggerMonoid OpenApiType where
swaggerMempty = OpenApiString
swaggerMappend _ y = y

instance SwaggerMonoid ParamLocation where
Expand All @@ -1109,7 +1109,7 @@ instance Monoid a => SwaggerMonoid (Referenced a) where
instance ToJSON Style where
toJSON = genericToJSON (jsonPrefix "Style")

instance ToJSON SwaggerType where
instance ToJSON OpenApiType where
toJSON = genericToJSON (jsonPrefix "Swagger")

instance ToJSON ParamLocation where
Expand Down Expand Up @@ -1164,7 +1164,7 @@ instance ToJSON OAuth2AuthorizationCodeFlow where
instance FromJSON Style where
parseJSON = genericParseJSON (jsonPrefix "Style")

instance FromJSON SwaggerType where
instance FromJSON OpenApiType where
parseJSON = genericParseJSON (jsonPrefix "Swagger")

instance FromJSON ParamLocation where
Expand Down Expand Up @@ -1243,9 +1243,9 @@ instance ToJSON SecuritySchemeType where
, "openIdConnectUrl" .= url
]

instance ToJSON Swagger where
instance ToJSON OpenApi where
toJSON a = sopSwaggerGenericToJSON a &
if InsOrdHashMap.null (_swaggerPaths a)
if InsOrdHashMap.null (_openApiPaths a)
then (<+> object ["paths" .= object []])
else id
toEncoding = sopSwaggerGenericToEncoding
Expand All @@ -1269,17 +1269,17 @@ instance ToJSON Header where
-- | As for nullary schema for 0-arity type constructors, see
-- <https://github.com/GetShopTV/swagger2/issues/167>.
--
-- >>> encode (SwaggerItemsArray [])
-- >>> encode (OpenApiItemsArray [])
-- "{\"example\":[],\"items\":{},\"maxItems\":0}"
--
instance ToJSON SwaggerItems where
toJSON (SwaggerItemsObject x) = object [ "items" .= x ]
toJSON (SwaggerItemsArray []) = object
instance ToJSON OpenApiItems where
toJSON (OpenApiItemsObject x) = object [ "items" .= x ]
toJSON (OpenApiItemsArray []) = object
[ "items" .= object []
, "maxItems" .= (0 :: Int)
, "example" .= Array mempty
]
toJSON (SwaggerItemsArray x) = object [ "items" .= x ]
toJSON (OpenApiItemsArray x) = object [ "items" .= x ]

instance ToJSON Components where
toJSON = sopSwaggerGenericToJSON
Expand Down Expand Up @@ -1386,7 +1386,7 @@ instance FromJSON SecuritySchemeType where
_ -> empty
parseJSON _ = empty

instance FromJSON Swagger where
instance FromJSON OpenApi where
parseJSON = sopSwaggerGenericParseJSON

instance FromJSON Server where
Expand All @@ -1399,7 +1399,7 @@ instance FromJSON Schema where
parseJSON = fmap nullaryCleanup . sopSwaggerGenericParseJSON
where nullaryCleanup :: Schema -> Schema
nullaryCleanup s =
if _schemaItems s == Just (SwaggerItemsArray [])
if _schemaItems s == Just (OpenApiItemsArray [])
then s { _schemaExample = Nothing
, _schemaMaxItems = Nothing
}
Expand All @@ -1408,11 +1408,11 @@ instance FromJSON Schema where
instance FromJSON Header where
parseJSON = sopSwaggerGenericParseJSON

instance FromJSON SwaggerItems where
instance FromJSON OpenApiItems where
parseJSON js@(Object obj)
| null obj = pure $ SwaggerItemsArray [] -- Nullary schema.
| otherwise = SwaggerItemsObject <$> parseJSON js
parseJSON js@(Array _) = SwaggerItemsArray <$> parseJSON js
| null obj = pure $ OpenApiItemsArray [] -- Nullary schema.
| otherwise = OpenApiItemsObject <$> parseJSON js
parseJSON js@(Array _) = OpenApiItemsArray <$> parseJSON js
parseJSON _ = empty

instance FromJSON Components where
Expand Down Expand Up @@ -1526,7 +1526,7 @@ instance HasSwaggerAesonOptions SecurityScheme where
swaggerAesonOptions _ = mkSwaggerAesonOptions "securityScheme" & saoSubObject ?~ "type"
instance HasSwaggerAesonOptions Schema where
swaggerAesonOptions _ = mkSwaggerAesonOptions "schema" & saoSubObject ?~ "paramSchema"
instance HasSwaggerAesonOptions Swagger where
instance HasSwaggerAesonOptions OpenApi where
swaggerAesonOptions _ = mkSwaggerAesonOptions "swagger" & saoAdditionalPairs .~ [("openapi", "3.0.0")]
instance HasSwaggerAesonOptions Example where
swaggerAesonOptions _ = mkSwaggerAesonOptions "example"
Expand All @@ -1545,7 +1545,7 @@ instance AesonDefaultValue OAuth2AuthorizationCodeFlow
instance AesonDefaultValue p => AesonDefaultValue (OAuth2Flow p)
instance AesonDefaultValue Responses
instance AesonDefaultValue SecuritySchemeType
instance AesonDefaultValue SwaggerType
instance AesonDefaultValue OpenApiType
instance AesonDefaultValue MimeList where defaultValue = Just mempty
instance AesonDefaultValue Info
instance AesonDefaultValue ParamLocation
Expand Down
Loading

0 comments on commit bd9df53

Please sign in to comment.