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

PDE-2946 fix(core): fix value.replace is not a function when resolving missing curlies (9.x backport) #467

Merged
merged 4 commits into from
Jan 4, 2022

Conversation

eliangcs
Copy link
Member

@eliangcs eliangcs commented Dec 22, 2021

Given bundle.inputData.foo is non-existent, doing this will give you value.replace is not a function error:

const perform = (z, bundle) => {
  const response = await z.request({
    url: 'https://example.com',
    method: 'POST',
    body: {
      inputs: ['{{bundle.inputData.foo}}'],
    }
  });
  return z.JSON.parse(response.content);
};

And this is the traceback:

Unhandled error: TypeError: value.replace is not a function
TypeError: value.replace is not a function
    at handleEmpty (:censored:9:d1ba0cf2aa:/node_modules/zapier-platform-core/src/tools/cleaner.js:137:27)
    at :censored:9:d1ba0cf2aa:/node_modules/zapier-platform-core/src/tools/cleaner.js:150:7
    at Array.forEach (<anonymous>)
    at normalizeEmptyRequestFields (:censored:9:d1ba0cf2aa:/node_modules/zapier-platform-core/src/tools/cleaner.js:148:30)
    at coerceBody (:censored:9:d1ba0cf2aa:/node_modules/zapier-platform-core/src/http-middlewares/before/prepare-request.js:63:5)
    at prepareRequest (:censored:9:d1ba0cf2aa:/node_modules/zapier-platform-core/src/http-middlewares/before/prepare-request.js:137:9)
    at Object.<anonymous> (:censored:9:d1ba0cf2aa:/node_modules/zapier-platform-core/src/middleware.js:66:31)

There are two problems with the regex .test() method.

First, .test() method can actually return true even if the input is a non-string. In this case, you can pass in an array and get a true:

> isCurlies = /{{.*?}}/g
/{{.*?}}/g
> isCurlies.test(['{{foo}}'])
true      // <- surprise!

Second, when working with the "global" (/.../g) flag, .test() saves the state between calls:

> isCurlies = /{{.*?}}/g
/{{.*?}}/g
> s = '{{test}}'
> isCurlies.test(s)
true
> isCurlies.test(s)
false    // <- another surprise!

Here is how we fix:

  • make sure value is a string before we match it with a regex
  • don't use isCurlies.test(value); use value.search(isCurlies) >= 0 instead

@eliangcs eliangcs changed the base branch from master to release-9.x December 22, 2021 09:52
@eliangcs eliangcs changed the title PDE-2946 fix(core): Fix value.replace is not a function when rendering missing curlies PDE-2946 fix(core): Fix value.replace is not a function when rendering missing curlies (9.x backport) Dec 22, 2021
@eliangcs eliangcs marked this pull request as ready for review December 22, 2021 13:56
@eliangcs eliangcs changed the title PDE-2946 fix(core): Fix value.replace is not a function when rendering missing curlies (9.x backport) PDE-2946 fix(core): fix value.replace is not a function when rendering missing curlies (9.x backport) Dec 23, 2021
@eliangcs eliangcs changed the title PDE-2946 fix(core): fix value.replace is not a function when rendering missing curlies (9.x backport) PDE-2946 fix(core): fix value.replace is not a function when resolving missing curlies (9.x backport) Jan 4, 2022
@eliangcs eliangcs merged commit 76298fa into release-9.x Jan 4, 2022
@eliangcs eliangcs deleted the PDE-2946-fix-normalizeEmptyRequestFields branch January 4, 2022 06:10
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

Successfully merging this pull request may close these issues.

None yet

2 participants