Skip to content

Commit

Permalink
Merge pull request #192 from phadej/tags-insord-set
Browse files Browse the repository at this point in the history
Fix #165: Use InsOrdHashSet
  • Loading branch information
phadej authored May 28, 2019
2 parents 5232015 + ac10503 commit c367e06
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 10 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
2.4
---

- Allow hashable-1.3, semigroups-0.19, network-3.1, time-1.9, generics-sop-0.5
- Tags aren't sorted
(see [#165](https://github.com/GetShopTV/swagger2/issues/165))
- Schema type is optional
(see [#138](https://github.com/GetShopTV/swagger2/issues/138), [#164](https://github.com/GetShopTV/swagger2/pull/164))
- Take concrete 'Proxy' as argument
(see [#180](https://github.com/GetShopTV/swagger2/pull/180))

2.3.1.1
-------
- Allow `network-3.0`
Expand Down
2 changes: 2 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
packages: .
tests: true
14 changes: 10 additions & 4 deletions src/Data/Swagger/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ import Control.Applicative
import Data.Aeson
import qualified Data.Aeson.Types as JSON
import Data.Data (Data(..), Typeable, mkConstr, mkDataType, Fixity(..), Constr, DataType, constrIndex)
import Data.Hashable (Hashable)
import qualified Data.HashMap.Strict as HashMap
import Data.HashSet.InsOrd (InsOrdHashSet)
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Monoid (Monoid (..))
import Data.Semigroup.Compat (Semigroup (..))
import Data.Scientific (Scientific)
import Data.Set (Set)
import Data.String (IsString(..))
import Data.Text (Text)
import qualified Data.Text as Text
Expand Down Expand Up @@ -129,7 +130,7 @@ data Swagger = Swagger
-- 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 :: Set Tag
, _swaggerTags :: InsOrdHashSet Tag

-- | Additional external documentation.
, _swaggerExternalDocs :: Maybe ExternalDocs
Expand Down Expand Up @@ -251,7 +252,7 @@ data PathItem = PathItem
data Operation = Operation
{ -- | A list of tags for API documentation control.
-- Tags can be used for logical grouping of operations by resources or any other qualifier.
_operationTags :: Set TagName
_operationTags :: InsOrdHashSet TagName

-- | A short summary of what the operation does.
-- For maximum readability in the swagger-ui, this field SHOULD be less than 120 characters.
Expand Down Expand Up @@ -778,6 +779,8 @@ data Tag = Tag
, _tagExternalDocs :: Maybe ExternalDocs
} deriving (Eq, Ord, Show, Generic, Data, Typeable)

instance Hashable Tag

instance IsString Tag where
fromString s = Tag (fromString s) Nothing Nothing

Expand All @@ -791,6 +794,8 @@ data ExternalDocs = ExternalDocs
, _externalDocsUrl :: URL
} deriving (Eq, Ord, Show, Generic, Data, Typeable)

instance Hashable ExternalDocs

-- | A simple object to allow referencing other definitions in the specification.
-- It can be used to reference parameters and responses that are defined at the top level for reuse.
newtype Reference = Reference { getReference :: Text }
Expand All @@ -804,7 +809,7 @@ data Referenced a
instance IsString a => IsString (Referenced a) where
fromString = Inline . fromString

newtype URL = URL { getUrl :: Text } deriving (Eq, Ord, Show, ToJSON, FromJSON, Data, Typeable)
newtype URL = URL { getUrl :: Text } deriving (Eq, Ord, Show, Hashable, ToJSON, FromJSON, Data, Typeable)

data AdditionalProperties
= AdditionalPropertiesAllowed Bool
Expand Down Expand Up @@ -913,6 +918,7 @@ instance SwaggerMonoid Responses
instance SwaggerMonoid Response
instance SwaggerMonoid ExternalDocs
instance SwaggerMonoid Operation
instance (Eq a, Hashable a) => SwaggerMonoid (InsOrdHashSet a)

instance SwaggerMonoid MimeList
deriving instance SwaggerMonoid URL
Expand Down
2 changes: 2 additions & 0 deletions src/Data/Swagger/Internal/AesonUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import qualified Data.Text as T
import qualified Data.HashMap.Strict as HM
import qualified Data.Set as Set
import qualified Data.HashMap.Strict.InsOrd as InsOrd
import qualified Data.HashSet.InsOrd as InsOrdHS

#if MIN_VERSION_aeson(0,10,0)
import Data.Aeson (Encoding, pairs, (.=), Series)
Expand Down Expand Up @@ -85,6 +86,7 @@ instance AesonDefaultValue Text where defaultValue = Nothing
instance AesonDefaultValue (Maybe a) where defaultValue = Just Nothing
instance AesonDefaultValue [a] where defaultValue = Just []
instance AesonDefaultValue (Set.Set a) where defaultValue = Just Set.empty
instance AesonDefaultValue (InsOrdHS.InsOrdHashSet k) where defaultValue = Just InsOrdHS.empty
instance AesonDefaultValue (InsOrd.InsOrdHashMap k v) where defaultValue = Just InsOrd.empty

-------------------------------------------------------------------------------
Expand Down
5 changes: 3 additions & 2 deletions src/Data/Swagger/Operation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import Data.Swagger.Lens
import Data.Swagger.Schema

import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
import qualified Data.HashSet.InsOrd as InsOrdHS

-- $setup
-- >>> import Data.Aeson
Expand Down Expand Up @@ -111,8 +112,8 @@ applyTags = applyTagsFor allOperations
-- list of tags.
applyTagsFor :: Traversal' Swagger Operation -> [Tag] -> Swagger -> Swagger
applyTagsFor ops ts swag = swag
& ops . tags %~ (<> Set.fromList (map _tagName ts))
& tags %~ (<> Set.fromList ts)
& ops . tags %~ (<> InsOrdHS.fromList (map _tagName ts))
& tags %~ (<> InsOrdHS.fromList ts)

-- | Construct a response with @'Schema'@ while declaring all
-- necessary schema definitions.
Expand Down
1 change: 1 addition & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ extra-deps:
- aeson-1.4.2.0
- base-compat-batteries-0.10.4
- contravariant-1.5
- insert-ordered-containers-0.2.2
2 changes: 1 addition & 1 deletion swagger2.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ library
, generics-sop >=0.3.2.0 && <0.6
, hashable >=1.2.7.0 && <1.4
, http-media >=0.7.1.2 && <0.9
, insert-ordered-containers >=0.2.1.0 && <0.3
, insert-ordered-containers >=0.2.2 && <0.3
, lens >=4.16.1 && <4.18
, network >=2.6.3.5 && <3.2
, scientific >=0.3.6.2 && <0.4
Expand Down
6 changes: 3 additions & 3 deletions test/Data/SwaggerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Control.Lens
import Data.Aeson
import Data.Aeson.QQ.Simple
import Data.HashMap.Strict (HashMap)
import qualified Data.Set as Set
import qualified Data.HashSet.InsOrd as InsOrdHS
import Data.Text (Text)

import Data.Swagger
Expand Down Expand Up @@ -127,7 +127,7 @@ licenseExampleJSON = [aesonQQ|

operationExample :: Operation
operationExample = mempty
& tags .~ Set.fromList ["pet"]
& tags .~ InsOrdHS.fromList ["pet"]
& summary ?~ "Updates a pet in the store with form data"
& description ?~ ""
& operationId ?~ "updatePetWithForm"
Expand Down Expand Up @@ -531,7 +531,7 @@ swaggerExample = mempty
& schema .~ ParamOther (mempty
& in_ .~ ParamPath
& type_ ?~ SwaggerString ) ]
& tags .~ Set.fromList [ "todo" ] ))
& tags .~ InsOrdHS.fromList [ "todo" ] ))

swaggerExampleJSON :: Value
swaggerExampleJSON = [aesonQQ|
Expand Down

0 comments on commit c367e06

Please sign in to comment.