git.iko.soy/typeable/openapi3
on master help sign in
Omit empty arrays/objects from toJSON output where appropriate
f18f8b2Nickolay 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