git.iko.soy/typeable/openapi3
on master help sign in
Merge pull request #22 from GetShopTV/reduce-noise-#11
d776474Nickolay Kudasov
src/Data/Swagger/Internal.hs+14−7
@@ -722,9 +722,6 @@ deriveJSONDefault ''Scheme
722deriveJSON' ''Tag
722deriveJSON' ''Tag
723deriveJSON' ''ExternalDocs
723deriveJSON' ''ExternalDocs
724
724
725−deriveToJSON' ''Operation
726−deriveToJSON' ''Response
727−deriveToJSON' ''PathItem
728deriveToJSON' ''Xml
725deriveToJSON' ''Xml
729
726
730-- =======================================================================
727-- =======================================================================
@@ -747,7 +744,7 @@ instance ToJSON OAuth2Flow where
747 , "tokenUrl" .= tokenUrl ]
744 , "tokenUrl" .= tokenUrl ]
748
745
749instance ToJSON OAuth2Params where
746instance ToJSON OAuth2Params where
750− toJSON = genericToJSONWithSub "flow" (jsonPrefix "oauth2")
747+ toJSON = omitEmpties . genericToJSONWithSub "flow" (jsonPrefix "oauth2")
751
748
752instance ToJSON SecuritySchemeType where
749instance ToJSON SecuritySchemeType where
753 toJSON SecuritySchemeBasic
750 toJSON SecuritySchemeBasic
@@ -760,7 +757,7 @@ instance ToJSON SecuritySchemeType where
760 <+> object [ "type" .= ("oauth2" :: Text) ]
757 <+> object [ "type" .= ("oauth2" :: Text) ]
761
758
762instance ToJSON Swagger where
759instance ToJSON Swagger where
763− toJSON = addVersion . genericToJSON (jsonPrefix "")
760+ toJSON = omitEmpties . addVersion . genericToJSON (jsonPrefix "")
764 where
761 where
765 addVersion (Object o) = Object (HashMap.insert "swagger" "2.0" o)
762 addVersion (Object o) = Object (HashMap.insert "swagger" "2.0" o)
766 addVersion _ = error "impossible"
763 addVersion _ = error "impossible"
@@ -769,7 +766,7 @@ instance ToJSON SecurityScheme where
769 toJSON = genericToJSONWithSub "type" (jsonPrefix "securityScheme")
766 toJSON = genericToJSONWithSub "type" (jsonPrefix "securityScheme")
770
767
771instance ToJSON Schema where
768instance ToJSON Schema where
772− toJSON = genericToJSONWithSub "paramSchema" (jsonPrefix "schema")
769+ toJSON = omitEmpties . genericToJSONWithSub "paramSchema" (jsonPrefix "schema")
773
770
774instance ToJSON Header where
771instance ToJSON Header where
775 toJSON = genericToJSONWithSub "paramSchema" (jsonPrefix "header")
772 toJSON = genericToJSONWithSub "paramSchema" (jsonPrefix "header")
@@ -804,7 +801,17 @@ instance ToJSON SchemaItems where
804 toJSON (SchemaItemsArray xs) = toJSON xs
801 toJSON (SchemaItemsArray xs) = toJSON xs
805
802
806instance ToJSON Responses where
803instance ToJSON Responses where
804+ toJSON (Responses def rs) = omitEmpties $
807− toJSON (Responses def rs) = toJSON (hashMapMapKeys show rs) <+> object [ "default" .= def ]
805+ toJSON (hashMapMapKeys show rs) <+> object [ "default" .= def ]
806+
807+instance ToJSON Response where
808+ toJSON = omitEmpties . genericToJSON (jsonPrefix "response")
809+
810+instance ToJSON Operation where
811+ toJSON = omitEmpties . genericToJSON (jsonPrefix "operation")
812+
813+instance ToJSON PathItem where
814+ toJSON = omitEmpties . genericToJSON (jsonPrefix "pathItem")
808
815
809instance ToJSON Example where
816instance ToJSON Example where
810 toJSON = toJSON . Map.mapKeys show . getExample
817 toJSON = toJSON . Map.mapKeys show . getExample
src/Data/Swagger/Internal/Utils.hs+6
@@ -60,6 +60,12 @@ parseOneOf xs js =
60 where
60 where
61 ys = zip (map toJSON xs) xs
61 ys = zip (map toJSON xs) xs
62
62
63+omitEmpties :: Value -> Value
64+omitEmpties (Object o) = Object (HashMap.filter nonEmpty o)
65+ where
66+ nonEmpty js = (js /= Object mempty) && (js /= Array mempty) && (js /= Null)
67+omitEmpties js = js
68+
63genericToJSONWithSub :: (Generic a, GToJSON (Rep a)) => Text -> Options -> a -> Value
69genericToJSONWithSub :: (Generic a, GToJSON (Rep a)) => Text -> Options -> a -> Value
64genericToJSONWithSub sub opts x =
70genericToJSONWithSub sub opts x =
65 case genericToJSON opts x of
71 case genericToJSON opts x of
test/Data/Swagger/SchemaSpec.hs+1−1
@@ -23,7 +23,7 @@ import SpecCommon
23import Test.Hspec
23import Test.Hspec
24
24
25checkToSchema :: ToSchema a => Proxy a -> Value -> Spec
25checkToSchema :: ToSchema a => Proxy a -> Value -> Spec
26−checkToSchema proxy js = toSchema proxy <~> js
26+checkToSchema proxy js = toSchema proxy <=> js
27
27
28checkSchemaName :: ToSchema a => Maybe String -> Proxy a -> Spec
28checkSchemaName :: ToSchema a => Maybe String -> Proxy a -> Spec
29checkSchemaName sname proxy =
29checkSchemaName sname proxy =
test/Data/SwaggerSpec.hs+10−10
@@ -21,18 +21,18 @@ spec = do
21 describe "License Object" $ licenseExample <=> licenseExampleJSON
21 describe "License Object" $ licenseExample <=> licenseExampleJSON
22 describe "Contact Object" $ contactExample <=> contactExampleJSON
22 describe "Contact Object" $ contactExample <=> contactExampleJSON
23 describe "Info Object" $ infoExample <=> infoExampleJSON
23 describe "Info Object" $ infoExample <=> infoExampleJSON
24− describe "Operation Object" $ operationExample <~> operationExampleJSON
24+ describe "Operation Object" $ operationExample <=> operationExampleJSON
25 describe "Schema Object" $ do
25 describe "Schema Object" $ do
26− context "Primitive Sample" $ schemaPrimitiveExample <~> schemaPrimitiveExampleJSON
26+ context "Primitive Sample" $ schemaPrimitiveExample <=> schemaPrimitiveExampleJSON
27− context "Simple Model" $ schemaSimpleModelExample <~> schemaSimpleModelExampleJSON
27+ context "Simple Model" $ schemaSimpleModelExample <=> schemaSimpleModelExampleJSON
28− context "Model with Map/Dictionary Properties" $ schemaModelDictExample <~> schemaModelDictExampleJSON
28+ context "Model with Map/Dictionary Properties" $ schemaModelDictExample <=> schemaModelDictExampleJSON
29− context "Model with Example" $ schemaWithExampleExample <~> schemaWithExampleExampleJSON
29+ context "Model with Example" $ schemaWithExampleExample <=> schemaWithExampleExampleJSON
30− describe "Definitions Object" $ definitionsExample <~> definitionsExampleJSON
30+ describe "Definitions Object" $ definitionsExample <=> definitionsExampleJSON
31− describe "Parameters Definition Object" $ paramsDefinitionExample <~> paramsDefinitionExampleJSON
31+ describe "Parameters Definition Object" $ paramsDefinitionExample <=> paramsDefinitionExampleJSON
32− describe "Responses Definition Object" $ responsesDefinitionExample <~> responsesDefinitionExampleJSON
32+ describe "Responses Definition Object" $ responsesDefinitionExample <=> responsesDefinitionExampleJSON
33− describe "Security Definitions Object" $ securityDefinitionsExample <~> securityDefinitionsExampleJSON
33+ describe "Security Definitions Object" $ securityDefinitionsExample <=> securityDefinitionsExampleJSON
34 describe "Swagger Object" $ do
34 describe "Swagger Object" $ do
35− context "Todo Example" $ swaggerExample <~> swaggerExampleJSON
35+ context "Todo Example" $ swaggerExample <=> swaggerExampleJSON
36 context "PetStore Example" $
36 context "PetStore Example" $
37 it "decodes successfully" $ do
37 it "decodes successfully" $ do
38 fromJSON petstoreExampleJSON `shouldSatisfy` (\x -> case x of Success (_ :: Swagger) -> True; _ -> False)
38 fromJSON petstoreExampleJSON `shouldSatisfy` (\x -> case x of Success (_ :: Swagger) -> True; _ -> False)
test/SpecCommon.hs−7
@@ -15,13 +15,6 @@ isSubJSON (Object x) (Object y) = HashMap.keys x == HashMap.keys i && F.and i
15isSubJSON (Array xs) (Array ys) = Vector.length xs == Vector.length ys && F.and (Vector.zipWith isSubJSON xs ys)
15isSubJSON (Array xs) (Array ys) = Vector.length xs == Vector.length ys && F.and (Vector.zipWith isSubJSON xs ys)
16isSubJSON x y = x == y
16isSubJSON x y = x == y
17
17
18−(<~>) :: (Eq a, Show a, ToJSON a, FromJSON a) => a -> Value -> Spec
19−x <~> js = do
20− it "encodes correctly (probably with extra properties)" $ do
21− toJSON x `shouldSatisfy` (js `isSubJSON`)
22− it "decodes correctly" $ do
23− fromJSON js `shouldBe` Success x
24−
25(<=>) :: (Eq a, Show a, ToJSON a, FromJSON a) => a -> Value -> Spec
18(<=>) :: (Eq a, Show a, ToJSON a, FromJSON a) => a -> Value -> Spec
26x <=> js = do
19x <=> js = do
27 it "encodes correctly" $ do
20 it "encodes correctly" $ do