@@ -146,6 +146,14 @@ declareSchemaRef :: ToSchema a => proxy a -> Declare Definitions (Referenced Sch
146declareSchemaRef proxy = do
146declareSchemaRef proxy = do
147 case toNamedSchema proxy of
147 case toNamedSchema proxy of
148 (Just name, schema) -> do
148 (Just name, schema) -> do
149+ -- This check is very important as it allows generically
150+ -- derive used definitions for recursive schemas.
151+ -- Lazy Declare monad allows toNamedSchema to ignore
152+ -- any declarations (which would otherwise loop) and
153+ -- retrieve the schema and its name to check if we
154+ -- have already declared it.
155+ -- If we have, we don't need to declare anything for
156+ -- this schema this time and thus simply return the reference.
149 known <- looks (HashMap.member name)
157 known <- looks (HashMap.member name)
150 when (not known) $ do
158 when (not known) $ do
151 declare [(name, schema)]
159 declare [(name, schema)]
@@ -356,6 +364,14 @@ gdeclareSchemaRef :: GToSchema a => SchemaOptions -> proxy a -> Declare Definiti
356gdeclareSchemaRef opts proxy = do
364gdeclareSchemaRef opts proxy = do
357 case gtoNamedSchema opts proxy of
365 case gtoNamedSchema opts proxy of
358 (Just name, schema) -> do
366 (Just name, schema) -> do
367+ -- This check is very important as it allows generically
368+ -- derive used definitions for recursive schemas.
369+ -- Lazy Declare monad allows toNamedSchema to ignore
370+ -- any declarations (which would otherwise loop) and
371+ -- retrieve the schema and its name to check if we
372+ -- have already declared it.
373+ -- If we have, we don't need to declare anything for
374+ -- this schema this time and thus simply return the reference.
359 known <- looks (HashMap.member name)
375 known <- looks (HashMap.member name)
360 when (not known) $ do
376 when (not known) $ do
361 declare [(name, schema)]
377 declare [(name, schema)]