Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Clovel opened this issue Jan 8, 2025 · 1 comment
Open

Comments

@Clovel
Copy link

Clovel commented Jan 8, 2025

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.

@Clovel
Copy link
Author

Clovel commented Jan 8, 2025

Be removing the else if (Array.isArray(opts[key])) section, the fix still work as expected.

Extensive testing might be needed to ensure there are no breaking changes.

@Clovel Clovel changed the title changes array parameter for /create/image is'nt serialized correctly changes array parameter for /create/image isn't serialized correctly Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant