scottie1984 / swagger-ui-express

Adds middleware to your express app to serve the Swagger UI bound to your Swagger document. This acts as living documentation for your API hosted from within your app.
MIT License
1.41k stars 225 forks source link

Circular reference #286

Open RaviVaranasi opened 2 years ago

RaviVaranasi commented 2 years ago

I have an object that has children of the same type.

 "Option": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        },
        "children": {
          "items": {
            "$ref": "#/definitions/Option"
          },
          "type": "array",
          "minItems": 0,
          "nullable": true,
          "uniqueItems": true,
          "default": []
        }
    }
}

Below is the error message

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Object'
    |     property 'properties' -> object with constructor 'Object'
    |     property 'children' -> object with constructor 'Object'
    --- property 'items' closes the circle
    at JSON.stringify (<anonymous>)
    at stringify (/Users/<user>/projects/<project>/node_modules/express/lib/response.js:1128:12)

Is there a way to work around this issue?

frank-dspeed commented 2 years ago

@RaviVaranasi that is not a issue with swagger that error tells you really detailed everything about your error.

a circular repeating fild in a Object can not get serialized to JSON it is infinit the workaround is to get Infinity amount of Ram and Harddisk and try to find the end of Infinity

RaviVaranasi commented 2 years ago

My apologies got the wrong stacktrace

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Object'
    |     property 'properties' -> object with constructor 'Object'
    |     property 'children' -> object with constructor 'Object'
    --- property 'items' closes the circle
    at JSON.stringify (<anonymous>)
    at stringify (/Users/projects/<project>/node_modules/swagger-ui-express/index.js:241:19)
    at generateHTML (/Users/projects/<project>/node_modules/swagger-ui-express/index.js:176:73)
    at /Users/projects/<project>/node_modules/swagger-ui-express/index.js:184:21
RaviVaranasi commented 2 years ago

Below is code change in index.js to work around this issue. Happy to issue a PR

var stringify = function (obj, prop) {
  var placeholder = '____FUNCTIONPLACEHOLDER____'
  var fns = []
  var seen = new WeakSet();
  var json = JSON.stringify(obj, function (key, value) {
    if (typeof value === 'function') {
      fns.push(value)
      return placeholder
    }
    if (typeof value === "object" && value !== null) {
      if (seen.has(value)) {
        return;
      }
      seen.add(value);
    }
    return value
  }, 2)
  json = json.replace(new RegExp('"' + placeholder + '"', 'g'), function (_) {
    return fns.shift()
  })
  return 'var options = ' + json + ';'
}
frank-dspeed commented 2 years ago

@RaviVaranasi the Changes look good to me at first view simply do the pr and maybe add a test for that case also thanks a lot i did not fully understand it before i tought the problem is a diffrent