Skip to content

Conversation

erunion
Copy link
Member

@erunion erunion commented Sep 2, 2021

🧰 What's being changed?

This upgrades @readme/oas-to-har to fix a quirk with query parameter handling where data is not being URL encoded before it's sent off to fetch-har to making a request.

See readmeio/oas-to-har#16 for more details on the fix.

🧬 Testing

You can see this quirk in action right now against main by running the following snippet:

const spec = {
  servers: [
    { url: 'https://httpbin.org' }
  ],
  paths: {
    '/anything': {
      get: {
        parameters: [
          { name: 'pound', in: 'query' },
          { name: 'hash', in: 'query' },
          { name: 'array', in: 'query' },
          { name: 'weird', in: 'query' },
        ],
      },
    },
  },
};

const sdk = require('./packages/api')(spec);

(async () => {
  await sdk.get('/anything', {
    pound: 'somethign&nothing=true',
    hash: 'hash#data',
    array: 'where[4]=10',
    weird: 'properties["$email"] == "testing"',
  }).then(res => {
    console.log(res)
  });
})();
{
  args: { hash: 'hash', nothing: 'true', pound: 'somethign' },
  data: '',
  files: {},
  form: {},
  headers: {
    Accept: '*/*',
    'Accept-Encoding': 'gzip,deflate',
    Host: 'httpbin.org',
    'User-Agent': 'api (node)/3.3.2',
    'X-Amzn-Trace-Id': 'Root=1-613015bc-09f7a08854acbc2b14eadfbf'
  },
  json: null,
  method: 'GET',
  origin: '<my ip address>',
  url: 'https://httpbin.org/anything?pound=somethign&nothing=true&hash=hash'
}

Whereas if you run those parameters through encodeURIComponent(), or with this oas-to-har update, the resulting payload from https://httpbin.org looks right:

{
  args: {
    array: 'where[4]=10',
    hash: 'hash#data',
    pound: 'somethign&nothing=true',
    weird: 'properties["$email"] == "testing"'
  },
  data: '',
  files: {},
  form: {},
  headers: {
    Accept: '*/*',
    'Accept-Encoding': 'gzip,deflate',
    Host: 'httpbin.org',
    'User-Agent': 'api (node)/3.3.2',
    'X-Amzn-Trace-Id': 'Root=1-61301651-2a45f56f00c7c0797ad92f9d'
  },
  json: null,
  method: 'GET',
  origin: '<my ip address>',
  url: 'https://httpbin.org/anything?pound=somethign%26nothing%3Dtrue&hash=hash%23data&array=where[4]%3D10&weird=properties["%24email"] %3D%3D "testing"'
}

@erunion erunion added bug Something isn't working dependencies Pull requests that update a dependency file labels Sep 2, 2021
@erunion erunion marked this pull request as ready for review September 2, 2021 21:43
@Dashron
Copy link

Dashron commented Sep 3, 2021

% characters seem to crash this PR

(node:14407) UnhandledPromiseRejectionWarning: URIError: URI malformed

@erunion
Copy link
Member Author

erunion commented Sep 13, 2021

Precent encoding issue is now resolved:

const sdk = require('./packages/api')(spec);

(async () => {
  await sdk.get('/anything', {
    pound: 's%omethign&nothing=true',
    hash: 'hash#data',
    array: 'where[4]=10',
    weird: 'properties["$email"] == "testing"',
  }).then(res => {
    console.log(res)
  });
})();
{
  args: {
    array: 'where[4]=10',
    hash: 'hash#data',
    pound: 's%omethign&nothing=true',
    weird: 'properties["$email"] == "testing"'
  },
  data: '',
  files: {},
  form: {},
  headers: {
    Accept: '*/*',
    'Accept-Encoding': 'gzip,deflate',
    Host: 'httpbin.org',
    'User-Agent': 'api (node)/3.3.2',
    'X-Amzn-Trace-Id': 'Root=1-613f9684-1a0d9667216e65aa3f6026a6'
  },
  json: null,
  method: 'GET',
  origin: '<my ip address>',
  url: 'https://httpbin.org/anything?pound=s%25omethign%26nothing%3Dtrue&hash=hash%23data&array=where[4]%3D10&weird=properties["%24email"] %3D%3D "testing"'
}

@erunion erunion merged commit fe43a41 into main Sep 13, 2021
@erunion erunion deleted the fix/url-encoding-quirks branch September 13, 2021 18:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working dependencies Pull requests that update a dependency file

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants