git.iko.soy/typeable/openapi3
on master help sign in
Prepend path automatically in JSON instances
49d1fadNickolay Kudasov
src/Data/Swagger/Internal.hs+23−7
@@ -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 ]
813
814
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
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/"
817
822
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
961
966
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"
970+ case ms of
965− <|> Inline <$> parseJSON js
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/"
966
982
967instance FromJSON Xml where
983instance FromJSON Xml where
968 parseJSON = genericParseJSON (jsonPrefix "xml")
984 parseJSON = genericParseJSON (jsonPrefix "xml")
src/Data/Swagger/Internal/Schema.hs+2−2
@@ -158,7 +158,7 @@ declareSchemaRef proxy = do
158 when (not known) $ do
158 when (not known) $ do
159 declare [(name, schema)]
159 declare [(name, schema)]
160 void $ declareNamedSchema proxy
160 void $ declareNamedSchema proxy
161− return $ Ref (Reference ("#/definitions/" <> name))
161+ return $ Ref (Reference name)
162 _ -> Inline <$> declareSchema proxy
162 _ -> Inline <$> declareSchema proxy
163
163
164class GToSchema (f :: * -> *) where
164class GToSchema (f :: * -> *) where
@@ -376,7 +376,7 @@ gdeclareSchemaRef opts proxy = do
376 when (not known) $ do
376 when (not known) $ do
377 declare [(name, schema)]
377 declare [(name, schema)]
378 void $ gdeclareNamedSchema opts proxy mempty
378 void $ gdeclareNamedSchema opts proxy mempty
379− return $ Ref (Reference ("#/definitions/" <> name))
379+ return $ Ref (Reference name)
380 _ -> Inline <$> gdeclareSchema opts proxy
380 _ -> Inline <$> gdeclareSchema opts proxy
381
381
382appendItem :: Referenced Schema -> Maybe SchemaItems -> Maybe SchemaItems
382appendItem :: Referenced Schema -> Maybe SchemaItems -> Maybe SchemaItems
test/Data/SwaggerSpec.hs+1−1
@@ -237,7 +237,7 @@ schemaSimpleModelExample = mempty
237 & schemaRequired .~ [ "name" ]
237 & schemaRequired .~ [ "name" ]
238 & schemaProperties .~
238 & schemaProperties .~
239 [ ("name", Inline (mempty & schemaType .~ SwaggerString))
239 [ ("name", Inline (mempty & schemaType .~ SwaggerString))
240− , ("address", Ref (Reference "#/definitions/Address"))
240+ , ("address", Ref (Reference "Address"))
241 , ("age", Inline $ mempty
241 , ("age", Inline $ mempty
242 & schemaMinimum ?~ 0
242 & schemaMinimum ?~ 0
243 & schemaType .~ SwaggerInteger
243 & schemaType .~ SwaggerInteger