git.iko.soy/typeable/swagger2
on master help sign in
Add inlineSchemasWhen and inlineAllSchemas helpers, re-export them
commita465deae4f448d5da9f85ff9949942c7a2b71631 authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateSun 13 Dec 2015, 01:03 (11 years ago) parent4f8ddea
2 files changed+31−4
src/Data/Swagger/Internal/Schema.hs+26−4 src/Data/Swagger/Schema.hs+5
src/Data/Swagger/Internal/Schema.hs+26−4
@@ -164,16 +164,38 @@ declareSchemaRef proxy = do
164164 return $ Ref (Reference name)
165165 _ -> Inline <$> declareSchema proxy
166166
167−inlineSchemas :: Data s => [T.Text] -> Definitions -> s -> s
168−inlineSchemas names defs = template %~ deref
167+-- | Inline any referenced schema if its name satisfies given predicate.
168+--
169+-- /NOTE:/ if a referenced schema is not found in definitions the predicate is ignored
170+-- and schema stays referenced.
171+--
172+-- __WARNING:__ @'inlineSchemasWhen'@ will loop when inlining recursive schemas.
173+inlineSchemasWhen :: Data s => (T.Text -> Bool) -> Definitions -> s -> s
174+inlineSchemasWhen p defs = template %~ deref
169175 where
170176 deref r@(Ref (Reference name))
171− | name `elem` names =
177+ | p name =
172178 case HashMap.lookup name defs of
173− Just schema -> Inline (inlineSchemas names defs schema)
179+ Just schema -> Inline (inlineSchemasWhen p defs schema)
174180 Nothing -> r
175181 deref r = r
176182
183+-- | Inline any referenced schema if its name is in the given list.
184+--
185+-- /NOTE:/ if a referenced schema is not found in definitions
186+-- it stays referenced even if it appears in the list of names.
187+--
188+-- __WARNING:__ @'inlineSchemas'@ will loop when inlining recursive schemas.
189+inlineSchemas :: Data s => [T.Text] -> Definitions -> s -> s
190+inlineSchemas names = inlineSchemasWhen (`elem` names)
191+
192+-- | Inline all schema references for which the definition
193+-- can be found in @'Definitions'@.
194+--
195+-- __WARNING:__ @'inlineAllSchemas'@ will loop when inlining recursive schemas.
196+inlineAllSchemas :: Data s => Definitions -> s -> s
197+inlineAllSchemas = inlineSchemasWhen (const True)
198+
177199class GToSchema (f :: * -> *) where
178200 gdeclareNamedSchema :: SchemaOptions -> proxy f -> Schema -> Declare Definitions NamedSchema
179201
src/Data/Swagger/Schema.hs+5
@@ -25,6 +25,11 @@ module Data.Swagger.Schema (
2525 paramSchemaToNamedSchema,
2626 paramSchemaToSchema,
2727
28+ -- * Inlining @'Schema'@s
29+ inlineAllSchemas,
30+ inlineSchemas,
31+ inlineSchemasWhen,
32+
2833 -- * Generic encoding configuration
2934 SchemaOptions(..),
3035 defaultSchemaOptions,