@@ -12,12 +12,17 @@
12{-# LANGUAGE ScopedTypeVariables #-}
12{-# LANGUAGE ScopedTypeVariables #-}
13{-# LANGUAGE StandaloneDeriving #-}
13{-# LANGUAGE StandaloneDeriving #-}
14{-# LANGUAGE UndecidableInstances #-}
14{-# LANGUAGE UndecidableInstances #-}
16+{-# LANGUAGE DataKinds #-}
17+{-# LANGUAGE TypeFamilies #-}
18+{-# LANGUAGE TemplateHaskell #-}
15#include "overlapping-compat.h"
19#include "overlapping-compat.h"
16module Data.Swagger.Internal where
20module Data.Swagger.Internal where
18import Prelude ()
22import Prelude ()
19import Prelude.Compat
23import Prelude.Compat
25+import Control.Lens ((&), (.~), (?~))
21import Control.Applicative
26import Control.Applicative
22import Control.Monad
27import Control.Monad
23import Data.Aeson
28import Data.Aeson
@@ -41,6 +46,11 @@ import Text.Read (readMaybe)
41import Data.HashMap.Strict.InsOrd (InsOrdHashMap)
46import Data.HashMap.Strict.InsOrd (InsOrdHashMap)
42import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
47import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
49+import Generics.SOP.TH (deriveGeneric)
50+import Data.Swagger.Internal.AesonUtils (sopSwaggerGenericToJSON
51+ ,mkSwaggerAesonOptions
44import Data.Swagger.Internal.Utils
54import Data.Swagger.Internal.Utils
46-- | A list of definitions that can be used in references.
56-- | A list of definitions that can be used in references.
@@ -921,7 +931,8 @@ instance ToJSON OAuth2Flow where
921 , "tokenUrl" .= tokenUrl ]
931 , "tokenUrl" .= tokenUrl ]
923instance ToJSON OAuth2Params where
933instance ToJSON OAuth2Params where
924− toJSON = omitEmpties . genericToJSONWithSub "flow" (jsonPrefix "oauth2")
934+ toJSON = sopSwaggerGenericToJSON $ mkSwaggerAesonOptions "oauth2" &
935+ saoSubObject ?~ "flow"
926instance ToJSON SecuritySchemeType where
937instance ToJSON SecuritySchemeType where
927 toJSON SecuritySchemeBasic
938 toJSON SecuritySchemeBasic
@@ -934,22 +945,20 @@ instance ToJSON SecuritySchemeType where
934 <+> object [ "type" .= ("oauth2" :: Text) ]
945 <+> object [ "type" .= ("oauth2" :: Text) ]
936instance ToJSON Swagger where
947instance ToJSON Swagger where
937− toJSON = omitEmpties . addVersion . genericToJSON (jsonPrefix "swagger")
948+ toJSON = sopSwaggerGenericToJSON $ mkSwaggerAesonOptions "swagger" &
938− where
949+ saoAdditionalPairs .~ [("swagger", "2.0")]
939− addVersion (Object o) = Object (HashMap.insert "swagger" "2.0" o)
940− addVersion _ = error "impossible"
942instance ToJSON SecurityScheme where
951instance ToJSON SecurityScheme where
943− toJSON = genericToJSONWithSub "type" (jsonPrefix "securityScheme")
952+ toJSON = sopSwaggerGenericToJSON $ mkSwaggerAesonOptions "securityScheme" &
953+ saoSubObject ?~ "type"
945instance ToJSON Schema where
955instance ToJSON Schema where
946− toJSON = omitEmptiesExcept f . genericToJSONWithSub "paramSchema" (jsonPrefix "schema")
956+ toJSON = sopSwaggerGenericToJSON $ mkSwaggerAesonOptions "schema" &
947− where
957+ saoSubObject ?~ "paramSchema"
948− f "items" (Array _) = True
951instance ToJSON Header where
959instance ToJSON Header where
952− toJSON = genericToJSONWithSub "paramSchema" (jsonPrefix "header")
960+ toJSON = sopSwaggerGenericToJSON $ mkSwaggerAesonOptions "header" &
961+ saoSubObject ?~ "paramSchema"
954instance ToJSON (SwaggerItems t) where
963instance ToJSON (SwaggerItems t) where
955 toJSON (SwaggerItemsPrimitive fmt schema) = object
964 toJSON (SwaggerItemsPrimitive fmt schema) = object
@@ -968,26 +977,29 @@ instance ToJSON MimeList where
968 toJSON (MimeList xs) = toJSON (map show xs)
977 toJSON (MimeList xs) = toJSON (map show xs)
970instance ToJSON Param where
979instance ToJSON Param where
971− toJSON = genericToJSONWithSub "schema" (jsonPrefix "param")
980+ toJSON = sopSwaggerGenericToJSON $ mkSwaggerAesonOptions "param" &
981+ saoSubObject ?~ "schema"
973instance ToJSON ParamAnySchema where
983instance ToJSON ParamAnySchema where
974 toJSON (ParamBody s) = object [ "in" .= ("body" :: Text), "schema" .= s ]
984 toJSON (ParamBody s) = object [ "in" .= ("body" :: Text), "schema" .= s ]
975 toJSON (ParamOther s) = toJSON s
985 toJSON (ParamOther s) = toJSON s
977instance ToJSON ParamOtherSchema where
987instance ToJSON ParamOtherSchema where
978− toJSON = genericToJSONWithSub "paramSchema" (jsonPrefix "paramOtherSchema")
988+ toJSON = sopSwaggerGenericToJSON $ mkSwaggerAesonOptions "paramOtherSchema" &
989+ saoSubObject ?~ "paramSchema"
980instance ToJSON Responses where
991instance ToJSON Responses where
981− toJSON = omitEmpties . genericToJSONWithSub "responses" (jsonPrefix "responses")
992+ toJSON = sopSwaggerGenericToJSON $ mkSwaggerAesonOptions "responses" &
993+ saoSubObject ?~ "responses"
983instance ToJSON Response where
995instance ToJSON Response where
984− toJSON = omitEmpties . genericToJSON (jsonPrefix "response")
996+ toJSON = sopSwaggerGenericToJSON $ mkSwaggerAesonOptions "response"
986instance ToJSON Operation where
998instance ToJSON Operation where
987− toJSON = omitEmpties . genericToJSON (jsonPrefix "operation")
999+ toJSON = sopSwaggerGenericToJSON $ mkSwaggerAesonOptions "operation"
989instance ToJSON PathItem where
1001instance ToJSON PathItem where
990− toJSON = omitEmpties . genericToJSON (jsonPrefix "pathItem")
1002+ toJSON = sopSwaggerGenericToJSON $ mkSwaggerAesonOptions "pathItem"
992instance ToJSON Example where
1004instance ToJSON Example where
993 toJSON = toJSON . Map.mapKeys show . getExample
1005 toJSON = toJSON . Map.mapKeys show . getExample
@@ -1021,10 +1033,8 @@ instance ToJSON (CollectionFormat t) where
1021 toJSON CollectionMulti = "multi"
1033 toJSON CollectionMulti = "multi"
1023instance ToJSON (ParamSchema t) where
1035instance ToJSON (ParamSchema t) where
1024− toJSON = omitEmptiesExcept f . genericToJSONWithSub "items" (jsonPrefix "paramSchema")
1036+ toJSON = sopSwaggerGenericToJSON $ mkSwaggerAesonOptions "paramSchema" &
1025− where
1037+ saoSubObject ?~ "items"
1026− f "items" (Array _) = True
1029-- =======================================================================
1039-- =======================================================================
1030-- Manual FromJSON instances
1040-- Manual FromJSON instances
@@ -1193,3 +1203,19 @@ instance FromJSON (CollectionFormat ParamOtherSchema) where
1193instance (FromJSON (SwaggerType t), FromJSON (SwaggerItems t)) => FromJSON (ParamSchema t) where
1203instance (FromJSON (SwaggerType t), FromJSON (SwaggerItems t)) => FromJSON (ParamSchema t) where
1194 parseJSON = genericParseJSONWithSub "items" (jsonPrefix "ParamSchema")
1204 parseJSON = genericParseJSONWithSub "items" (jsonPrefix "ParamSchema")
1206+-------------------------------------------------------------------------------
1208+-------------------------------------------------------------------------------
1210+deriveGeneric ''Header
1211+deriveGeneric ''OAuth2Params
1212+deriveGeneric ''Operation
1213+deriveGeneric ''Param
1214+deriveGeneric ''ParamOtherSchema
1215+deriveGeneric ''PathItem
1216+deriveGeneric ''Response
1217+deriveGeneric ''Responses
1218+deriveGeneric ''SecurityScheme
1219+deriveGeneric ''Schema
1220+deriveGeneric ''ParamSchema
1221+deriveGeneric ''Swagger