git.iko.soy/typeable/openapi3
on master help sign in
Omit empty arrays/objects from toJSON output where appropriate
commitf18f8b27465d67c9281bf460f3ef002aa7e076b6 authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateSat 12 Dec 2015, 14:03 (11 years ago) parent49d1fad
2 files changed+20−7
src/Data/Swagger/Internal.hs+14−7 src/Data/Swagger/Internal/Utils.hs+6
src/Data/Swagger/Internal.hs+14−7
@@ -722,9 +722,6 @@ deriveJSONDefault ''Scheme
722722deriveJSON' ''Tag
723723deriveJSON' ''ExternalDocs
724724
725−deriveToJSON' ''Operation
726−deriveToJSON' ''Response
727−deriveToJSON' ''PathItem
728725deriveToJSON' ''Xml
729726
730727-- =======================================================================
@@ -747,7 +744,7 @@ instance ToJSON OAuth2Flow where
747744 , "tokenUrl" .= tokenUrl ]
748745
749746instance ToJSON OAuth2Params where
750− toJSON = genericToJSONWithSub "flow" (jsonPrefix "oauth2")
747+ toJSON = omitEmpties . genericToJSONWithSub "flow" (jsonPrefix "oauth2")
751748
752749instance ToJSON SecuritySchemeType where
753750 toJSON SecuritySchemeBasic
@@ -760,7 +757,7 @@ instance ToJSON SecuritySchemeType where
760757 <+> object [ "type" .= ("oauth2" :: Text) ]
761758
762759instance ToJSON Swagger where
763− toJSON = addVersion . genericToJSON (jsonPrefix "")
760+ toJSON = omitEmpties . addVersion . genericToJSON (jsonPrefix "")
764761 where
765762 addVersion (Object o) = Object (HashMap.insert "swagger" "2.0" o)
766763 addVersion _ = error "impossible"
@@ -769,7 +766,7 @@ instance ToJSON SecurityScheme where
769766 toJSON = genericToJSONWithSub "type" (jsonPrefix "securityScheme")
770767
771768instance ToJSON Schema where
772− toJSON = genericToJSONWithSub "paramSchema" (jsonPrefix "schema")
769+ toJSON = omitEmpties . genericToJSONWithSub "paramSchema" (jsonPrefix "schema")
773770
774771instance ToJSON Header where
775772 toJSON = genericToJSONWithSub "paramSchema" (jsonPrefix "header")
@@ -804,7 +801,17 @@ instance ToJSON SchemaItems where
804801 toJSON (SchemaItemsArray xs) = toJSON xs
805802
806803instance ToJSON Responses where
807− toJSON (Responses def rs) = toJSON (hashMapMapKeys show rs) <+> object [ "default" .= def ]
804+ toJSON (Responses def rs) = omitEmpties $
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")
808815
809816instance ToJSON Example where
810817 toJSON = toJSON . Map.mapKeys show . getExample
src/Data/Swagger/Internal/Utils.hs+6
@@ -60,6 +60,12 @@ parseOneOf xs js =
6060 where
6161 ys = zip (map toJSON xs) xs
6262
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+
6369genericToJSONWithSub :: (Generic a, GToJSON (Rep a)) => Text -> Options -> a -> Value
6470genericToJSONWithSub sub opts x =
6571 case genericToJSON opts x of