git.iko.soy/typeable/swagger2
on master help sign in
Add inlineSchemasWhen and inlineAllSchemas helpers, re-export them
a465deaNickolay Kudasov
src/Data/Swagger/Internal/Schema.hs+26−4
@@ -164,16 +164,38 @@ declareSchemaRef proxy = do
164 return $ Ref (Reference name)
164 return $ Ref (Reference name)
165 _ -> Inline <$> declareSchema proxy
165 _ -> Inline <$> declareSchema proxy
166
166
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.
167−inlineSchemas :: Data s => [T.Text] -> Definitions -> s -> s
173+inlineSchemasWhen :: Data s => (T.Text -> Bool) -> Definitions -> s -> s
168−inlineSchemas names defs = template %~ deref
174+inlineSchemasWhen p defs = template %~ deref
169 where
175 where
170 deref r@(Ref (Reference name))
176 deref r@(Ref (Reference name))
171− | name `elem` names =
177+ | p name =
172 case HashMap.lookup name defs of
178 case HashMap.lookup name defs of
173− Just schema -> Inline (inlineSchemas names defs schema)
179+ Just schema -> Inline (inlineSchemasWhen p defs schema)
174 Nothing -> r
180 Nothing -> r
175 deref r = r
181 deref r = r
176
182
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+
177class GToSchema (f :: * -> *) where
199class GToSchema (f :: * -> *) where
178 gdeclareNamedSchema :: SchemaOptions -> proxy f -> Schema -> Declare Definitions NamedSchema
200 gdeclareNamedSchema :: SchemaOptions -> proxy f -> Schema -> Declare Definitions NamedSchema
179
201
src/Data/Swagger/Schema.hs+5
@@ -25,6 +25,11 @@ module Data.Swagger.Schema (
25 paramSchemaToNamedSchema,
25 paramSchemaToNamedSchema,
26 paramSchemaToSchema,
26 paramSchemaToSchema,
27
27
28+ -- * Inlining @'Schema'@s
29+ inlineAllSchemas,
30+ inlineSchemas,
31+ inlineSchemasWhen,
32+
28 -- * Generic encoding configuration
33 -- * Generic encoding configuration
29 SchemaOptions(..),
34 SchemaOptions(..),
30 defaultSchemaOptions,
35 defaultSchemaOptions,