git.iko.soy/typeable/swagger2
on master help sign in
Prepend path automatically in JSON instances
commit49d1fadd2100644e70c442667180d0d73e107a5f authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateThu 10 Dec 2015, 16:31 (11 years ago) parentbb33aeb
3 files changed+26−10
src/Data/Swagger/Internal.hs+23−7 src/Data/Swagger/Internal/Schema.hs+2−2 test/Data/SwaggerSpec.hs+1−1
src/Data/Swagger/Internal.hs+23−7
@@ -15,6 +15,7 @@ import Control.Applicative
1515import Control.Monad
1616import Data.Aeson
1717import Data.Aeson.TH (deriveJSON)
18+import qualified Data.Aeson.Types as JSON
1819import Data.HashMap.Strict (HashMap)
1920import qualified Data.HashMap.Strict as HashMap
2021import Data.Map (Map)
@@ -811,9 +812,13 @@ instance ToJSON Example where
811812instance ToJSON Reference where
812813 toJSON (Reference ref) = object [ "$ref" .= ref ]
813814
814−instance ToJSON a => ToJSON (Referenced a) where
815− toJSON (Ref ref) = toJSON ref
816− toJSON (Inline x) = toJSON x
815+referencedToJSON :: ToJSON a => Text -> Referenced a -> Value
816+referencedToJSON prefix (Ref (Reference ref)) = object [ "$ref" .= (prefix <> ref) ]
817+referencedToJSON _ (Inline x) = toJSON x
818+
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/"
817822
818823instance ToJSON (SwaggerType t) where
819824 toJSON SwaggerArray = "array"
@@ -959,10 +964,21 @@ instance FromJSON Reference where
959964 parseJSON (Object o) = Reference <$> o .: "$ref"
960965 parseJSON _ = empty
961966
962−instance FromJSON a => FromJSON (Referenced a) where
963− parseJSON js
964− = Ref <$> parseJSON js
965− <|> Inline <$> parseJSON js
967+referencedParseJSON :: FromJSON a => Text -> Value -> JSON.Parser (Referenced a)
968+referencedParseJSON prefix js@(Object o) = do
969+ ms <- o .:? "$ref"
970+ case ms of
971+ Nothing -> Inline <$> parseJSON js
972+ Just s -> Ref <$> parseRef s
973+ where
974+ parseRef s = do
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)
978+
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/"
966982
967983instance FromJSON Xml where
968984 parseJSON = genericParseJSON (jsonPrefix "xml")
src/Data/Swagger/Internal/Schema.hs+2−2
@@ -158,7 +158,7 @@ declareSchemaRef proxy = do
158158 when (not known) $ do
159159 declare [(name, schema)]
160160 void $ declareNamedSchema proxy
161− return $ Ref (Reference ("#/definitions/" <> name))
161+ return $ Ref (Reference name)
162162 _ -> Inline <$> declareSchema proxy
163163
164164class GToSchema (f :: * -> *) where
@@ -376,7 +376,7 @@ gdeclareSchemaRef opts proxy = do
376376 when (not known) $ do
377377 declare [(name, schema)]
378378 void $ gdeclareNamedSchema opts proxy mempty
379− return $ Ref (Reference ("#/definitions/" <> name))
379+ return $ Ref (Reference name)
380380 _ -> Inline <$> gdeclareSchema opts proxy
381381
382382appendItem :: Referenced Schema -> Maybe SchemaItems -> Maybe SchemaItems
test/Data/SwaggerSpec.hs+1−1
@@ -237,7 +237,7 @@ schemaSimpleModelExample = mempty
237237 & schemaRequired .~ [ "name" ]
238238 & schemaProperties .~
239239 [ ("name", Inline (mempty & schemaType .~ SwaggerString))
240− , ("address", Ref (Reference "#/definitions/Address"))
240+ , ("address", Ref (Reference "Address"))
241241 , ("age", Inline $ mempty
242242 & schemaMinimum ?~ 0
243243 & schemaType .~ SwaggerInteger