Skip to content

changes array parameter for /create/image isn't serialized correctly  #180

@Clovel

Description

@Clovel

I have a bug when using dockerode's importImage function when passing the changes parameter (see apocas/dockerode#785). I found out that the serialization of query strings isn't standard for every parameter.

I found a fix by modifying the Modem.prototype.buildQuerystring function like so :

Modem.prototype.buildQuerystring = function (opts) {
  var clone = {};

  // serialize map and array values as JSON strings, else querystring truncates.
  // 't' and 'extrahosts' can be arrays but need special treatment so that they're
  // passed as multiple qs parameters instead of JSON values.

  console.log(`[DEBUG] <Modem.prototype.buildQuerystring> opts :`, opts);

  Object.keys(opts)
  .map(
    (key, i) => {
      if (
        opts[key] &&
        typeof opts[key] === 'object' &&
        !Array.isArray(opts[key]) &&
        !['t', 'extrahosts'].includes(key)
      ) {
        clone[key] = JSON.stringify(opts[key]);
      } else if (Array.isArray(opts[key])) {
        clone[key] = opts[key]
          .map(
            (val) => {
              if(val && typeof val === 'object') {
                return JSON.stringify(val);
              } else {
                return val;
              }
            }
          );
      } else {
        clone[key] = opts[key];
      }
    }
  );

  const queryStr = querystring.stringify(clone);

  return queryStr;
};

It seems that this is related to #144, so I will try to make the same change.

By not using the JSON syntax for values in the query string, the rest of the dockerode functions I use seem to work fine. I wonder if we should generalize the behaviour of the Modem.prototype.buildQuerystring function for arrays.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions