Skip to content

Generator hangs at a recursive type in methods definition #720

@rhysd

Description

@rhysd

Describe the bug

I have an API method which returns a tree structure. I created a definition file with describing the recursive type in JSON schema and I tried this code generator. And the generator instantly hung.

To Reproduce
Steps to reproduce the behavior:

  1. Put the following JSON content as openrpc.json in your current directory
  2. npm i @open-rpc/generator
  3. npx open-rpc-generator init
  4. The command hangs at the first question. npx open-rpc-generator generate hangs as well
{
  "openrpc": "1.3.2",
  "info": {
    "version": "0.0.0",
    "title": "My API"
  },
  "methods": [
    {
      "name": "getTree",
      "description": "",
      "params": [],
      "result": {
        "name": "tree",
        "description": "",
        "schema": { "$ref": "#/components/schemas/Node" }
      }
    }
  ],
  "components": {
    "schemas": {
      "Node": {
        "type": "object",
        "properties": {
          "id": { "type": "integer" },
          "children": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/Node" }
          }
        }
      }
    }
  }
}

Expected behavior

The npx open-rpc-generator init creates a configuration file for the project.

Screenshots

Instead of screenshot I captured terminal screen outputs:

> npx open-rpc-generator init
? Where is your OpenRPC document? May be a file path or url. (openrpc.json)

The command hung at the first question.

Desktop (please complete the following information):

  • @open-rpc/generator: v1.22.3
  • OS: Windows 10
  • Browser: N/A
  • Version: Node.js v18.20.0

Additional context

When I modified the children field as follows, this issue didn't occur.

"children": {
  "type": "array",
  "items": { "type": "object" }
}

I believe the recursive type made the generator hang. I guess the generator checks $ref recursively without checking the reference is already visit.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions