Uploaded image for project: 'Nuxeo JS Client'
  1. Nuxeo JS Client
  2. NXJS-135

Prevent Converting circular structure to JSON when POST or PUT on a document with cycling references

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: 2.0.0, 3.0.0
    • Fix Version/s: None
    • Component/s: Browser

      Description

      In the context of NXP-24471, we are about to PUT/POST the full entity (and not only the id) when it comes to set Document, Directory Entry or User/Group in a document schema field.

      Problem is that when setting the document full entity, we get a `Converting circular structure to JSON` from the js client:

      nuxeo-document-edit.html:111 TypeError: Converting circular structure to JSON
      at JSON.stringify (<anonymous>)
      at Nuxeo._computeFetchOptions (nuxeo.js:2298)
      at Nuxeo.http (nuxeo.js:2172)
      at Request.execute (nuxeo.js:3236)
      at HTMLElement._doExecute (nuxeo-resource.html:227)
      at HTMLElement.<anonymous> (nuxeo-resource.html:209)

      The following diff fixes the issue:

      m3 fox@fox-laptop:~/workspace/nuxeo-js-client/lib$ git diff
      diff --git i/lib/nuxeo.js w/lib/nuxeo.js
      index 6b1e4d0..2eee3be 100644
      --- i/lib/nuxeo.js
      +++ w/lib/nuxeo.js
      @@ -237,7 +237,7 @@ class Nuxeo extends Base {
             options.headers['Content-Type'] = options.headers['Content-Type'] || 'application/json';
             // do not stringify FormData
             if (typeof options.body === 'object' && !(options.body instanceof FormData)) {
      -        options.body = JSON.stringify(options.body);
      +        options.body = this._stringify(options.body);
             }
           }
       
      @@ -250,7 +250,25 @@ class Nuxeo extends Base {
             options.url += qs.stringify(options.queryParams);
           }
           return options;
      -  }
      +  },
      +
      +  _stringify: function(obj) {
      +       var cache = [];
      +       var result = JSON.stringify(obj, function(key, value) {
      +      if (typeof value === 'object' && value !== null) {
      +        if (cache.indexOf(value) !== -1) {
      +          // Circular reference found, discard key
      +          return;
      +        }
      +        // Store value in our collection
      +        cache.push(value);
      +      }
      +      return value;
      +    });
      +       // Allow GC
      +    cache = null;
      +    return result;
      +  },
      

      But I also understand it is shameful to PUT/POST the whole entity in the payload when only the id would do the job. So I guess we could even make the the js client prune the payload when scanning one the following entity:

      • "entity-type": "document"
      • "entity-type": "directoryEntry"
      • "entity-type": "user"
      • "entity-type": "group"
        to keep only the uid/id.

        Attachments

          Issue Links

            Activity

              People

              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:

                  Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0 minutes
                  0m
                  Logged:
                  Time Spent - 2 days, 3 hours, 30 minutes
                  2d 3h 30m