git.iko.soy/typeable/swagger2
on master help sign in
fix parsing of host ports
006bdb0Sönke Hahn
src/Data/Swagger/Internal.hs+6−7
@@ -1088,13 +1088,12 @@ instance OVERLAPPABLE_ FromJSON (SwaggerItems Schema) where
1088 parseJSON _ = empty
1088 parseJSON _ = empty
1089
1089
1090instance FromJSON Host where
1090instance FromJSON Host where
1091− parseJSON (String s) =
1091+ parseJSON (String s) = case map Text.unpack $ Text.split (== ':') s of
1092− case fromInteger <$> readMaybe portStr of
1092+ [host] -> return $ Host host Nothing
1093− Nothing | not (null portStr) -> fail $ "Invalid port `" ++ portStr ++ "'"
1093+ [host, port] -> case readMaybe port of
1094− mport -> pure $ Host host mport
1094+ Nothing -> fail $ "Invalid port `" ++ port ++ "'"
1095− where
1095+ Just p -> return $ Host host (Just (fromInteger p))
1096− (hostText, portText) = Text.breakOn ":" s
1096+ _ -> fail $ "Invalid host `" ++ Text.unpack s ++ "'"
1097− [host, portStr] = map Text.unpack [hostText, portText]
1098 parseJSON _ = empty
1097 parseJSON _ = empty
1099
1098
1100instance FromJSON MimeList where
1099instance FromJSON MimeList where
test/Data/SwaggerSpec.hs+8
@@ -21,6 +21,14 @@ import Test.Hspec hiding (example)
21
21
22spec :: Spec
22spec :: Spec
23spec = do
23spec = do
24+ describe "host" $ do
25+ it "can decode the host port" $ do
26+ let h = Just $ Host "localhost" (Just (fromInteger 8000))
27+ swagger :: Swagger
28+ swagger = swaggerExample
29+ & host .~ h
30+ parsed :: Swagger = either error id $ eitherDecode' $ encode swagger
31+ parsed ^. host `shouldBe` h
24 describe "License Object" $ licenseExample <=> licenseExampleJSON
32 describe "License Object" $ licenseExample <=> licenseExampleJSON
25 describe "Contact Object" $ contactExample <=> contactExampleJSON
33 describe "Contact Object" $ contactExample <=> contactExampleJSON
26 describe "Info Object" $ infoExample <=> infoExampleJSON
34 describe "Info Object" $ infoExample <=> infoExampleJSON