interagent / prmd

JSON Schema tools and doc generation for HTTP APIs
MIT License
2.1k stars 172 forks source link

Stack level too deep #335

Open mrtj opened 6 years ago

mrtj commented 6 years ago

I am working with a schema that has recursive references (eg. comments on comments) and the document generator fails with the following error:

/Library/Ruby/Gems/2.3.0/gems/prmd-0.13.0/lib/prmd/schema.rb:103:in `merge': stack level too deep (SystemStackError)
    from /Library/Ruby/Gems/2.3.0/gems/prmd-0.13.0/lib/prmd/schema.rb:103:in `each'
    from /Library/Ruby/Gems/2.3.0/gems/prmd-0.13.0/lib/prmd/schema.rb:103:in `inject'
    from /Library/Ruby/Gems/2.3.0/gems/prmd-0.13.0/lib/prmd/schema.rb:103:in `dereference'
    from /Library/Ruby/Gems/2.3.0/gems/prmd-0.13.0/lib/prmd/templates/schemata/helper.erb:86:in `block in extract_schemata_refs'
    from /Library/Ruby/Gems/2.3.0/gems/prmd-0.13.0/lib/prmd/templates/schemata/helper.erb:84:in `each'
    from /Library/Ruby/Gems/2.3.0/gems/prmd-0.13.0/lib/prmd/templates/schemata/helper.erb:84:in `extract_schemata_refs'
    from /Library/Ruby/Gems/2.3.0/gems/prmd-0.13.0/lib/prmd/templates/schemata/helper.erb:88:in `block in extract_schemata_refs'
    from /Library/Ruby/Gems/2.3.0/gems/prmd-0.13.0/lib/prmd/templates/schemata/helper.erb:84:in `each'
     ... 9814 levels...
    from /Library/Ruby/Gems/2.3.0/gems/prmd-0.13.0/lib/prmd/cli.rb:95:in `run'
    from /Library/Ruby/Gems/2.3.0/gems/prmd-0.13.0/bin/prmd:6:in `<top (required)>'
    from /usr/local/bin/prmd:22:in `load'
    from /usr/local/bin/prmd:22:in `<main>'

Is there any support planned for recursive schemas?

geemus commented 6 years ago

It hadn't really come up yet, but perhaps there is no time like the present. Could you perhaps provide a bit more insight into what you are trying to do and a sample repro schema? Thanks!

mrtj commented 6 years ago

I am trying to model a (subset) of Facebook Graph API, and for example for the entity "comment" I came up the following json schema (simplified here but stil reproduces the bug):

{
  "title": "Comment entity",
  "description": "This entity represents a comment.",
  "id": "http://schema.neosperience.com/facebook-services/comment",
  "type": "object",
  "definitions": {
    "identity": {
      "$ref": "/comment#/definitions/id"
    },
    "id": {
      "type": "string",
      "description": "The identifier of the comment."
    },
    "message": {
      "type": "string",
      "description": "The comment text."
    },
    "comments": {
      "description": "Comments made on this comment.",
      "type": "array",
      "items": {
        "$ref": "/comment"
      }
    }
  },
  "properties": {
    "id": {
      "$ref": "/comment#/definitions/id"
    },
    "comments": {
      "$ref": "/comment#/definitions/comments"
    },
    "message": {
      "$ref": "/comment#/definitions/message"
    }
  },
  "links": []
}

I guess the problem is the recursive / circular reference made from the comments field to the comment itself. However this is a valid json schema and it has practical use as well.

The combine and the verify commands pass, I get the error when trying to generate the documentation with

prmd doc generated/schema.json > generated/schema.md