@@ -146,6 +146,14 @@ declareSchemaRef :: ToSchema a => proxy a -> Declare Definitions (Referenced Sch
146146declareSchemaRef proxy = do
147147 case toNamedSchema proxy of
148148 (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.
149157 known <- looks (HashMap.member name)
150158 when (not known) $ do
151159 declare [(name, schema)]
@@ -356,6 +364,14 @@ gdeclareSchemaRef :: GToSchema a => SchemaOptions -> proxy a -> Declare Definiti
356364gdeclareSchemaRef opts proxy = do
357365 case gtoNamedSchema opts proxy of
358366 (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.
359375 known <- looks (HashMap.member name)
360376 when (not known) $ do
361377 declare [(name, schema)]