@@ -15,6 +15,7 @@ import Control.Applicative
15import Control.Monad
15import Control.Monad
16import Data.Aeson
16import Data.Aeson
17import Data.Aeson.TH (deriveJSON)
17import Data.Aeson.TH (deriveJSON)
18+import qualified Data.Aeson.Types as JSON
18import Data.HashMap.Strict (HashMap)
19import Data.HashMap.Strict (HashMap)
19import qualified Data.HashMap.Strict as HashMap
20import qualified Data.HashMap.Strict as HashMap
20import Data.Map (Map)
21import Data.Map (Map)
@@ -811,9 +812,13 @@ instance ToJSON Example where
811instance ToJSON Reference where
812instance ToJSON Reference where
812 toJSON (Reference ref) = object [ "$ref" .= ref ]
813 toJSON (Reference ref) = object [ "$ref" .= ref ]
814−instance ToJSON a => ToJSON (Referenced a) where
815+referencedToJSON :: ToJSON a => Text -> Referenced a -> Value
815− toJSON (Ref ref) = toJSON ref
816+referencedToJSON prefix (Ref (Reference ref)) = object [ "$ref" .= (prefix <> ref) ]
816− toJSON (Inline x) = toJSON x
817+referencedToJSON _ (Inline x) = toJSON x
819+instance ToJSON (Referenced Schema) where toJSON = referencedToJSON "#/definitions/"
820+instance ToJSON (Referenced Param) where toJSON = referencedToJSON "#/parameters/"
821+instance ToJSON (Referenced Response) where toJSON = referencedToJSON "#/responses/"
818instance ToJSON (SwaggerType t) where
823instance ToJSON (SwaggerType t) where
819 toJSON SwaggerArray = "array"
824 toJSON SwaggerArray = "array"
@@ -959,10 +964,21 @@ instance FromJSON Reference where
959 parseJSON (Object o) = Reference <$> o .: "$ref"
964 parseJSON (Object o) = Reference <$> o .: "$ref"
960 parseJSON _ = empty
965 parseJSON _ = empty
962−instance FromJSON a => FromJSON (Referenced a) where
967+referencedParseJSON :: FromJSON a => Text -> Value -> JSON.Parser (Referenced a)
963− parseJSON js
968+referencedParseJSON prefix js@(Object o) = do
964− = Ref <$> parseJSON js
969+ ms <- o .:? "$ref"
965− <|> Inline <$> parseJSON js
971+ Nothing -> Inline <$> parseJSON js
972+ Just s -> Ref <$> parseRef s
975+ case Text.stripPrefix prefix s of
976+ Nothing -> fail $ "expected $ref of the form \"" <> Text.unpack prefix <> "*\", but got " <> show s
977+ Just suffix -> pure (Reference suffix)
979+instance FromJSON (Referenced Schema) where parseJSON = referencedParseJSON "#/definitions/"
980+instance FromJSON (Referenced Param) where parseJSON = referencedParseJSON "#/parameters/"
981+instance FromJSON (Referenced Response) where parseJSON = referencedParseJSON "#/responses/"
967instance FromJSON Xml where
983instance FromJSON Xml where
968 parseJSON = genericParseJSON (jsonPrefix "xml")
984 parseJSON = genericParseJSON (jsonPrefix "xml")