Skip to content

Commit

Permalink
fixed zapier objects array mapping (#16780)
Browse files Browse the repository at this point in the history
  • Loading branch information
emajo committed Oct 11, 2023
1 parent f180aa0 commit bbf9ccd
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ module.exports = {
const {keyPrefix} = utils.buildKeyAndLabel(prefix)
return {
{{#vars}}
'{{baseName}}': {{#isPrimitiveType}}bundle.inputData?.[`${keyPrefix}{{baseName}}`]{{/isPrimitiveType}}{{^isPrimitiveType}}{{^allowableValues}}{{^isArray}}utils.removeIfEmpty({{complexType}}.mapping(bundle, `${keyPrefix}{{baseName}}`)){{/isArray}}{{#isArray}}utils.removeKeyPrefixes(bundle.inputData?.[`${keyPrefix}{{baseName}}`], `${keyPrefix}{{baseName}}`){{/isArray}}{{/allowableValues}}{{#allowableValues}}bundle.inputData?.[`${keyPrefix}{{baseName}}`]{{/allowableValues}}{{/isPrimitiveType}},
'{{baseName}}': {{#isPrimitiveType}}bundle.inputData?.[`${keyPrefix}{{baseName}}`]{{/isPrimitiveType}}{{^isPrimitiveType}}{{^allowableValues}}{{^isArray}}utils.removeIfEmpty({{complexType}}.mapping(bundle, `${keyPrefix}{{baseName}}`)){{/isArray}}{{#isArray}}{{^isEnumRef}}utils.childMapping(bundle.inputData?.[`${keyPrefix}{{baseName}}`], `${keyPrefix}{{baseName}}`, {{complexType}}){{/isEnumRef}}{{#isEnumRef}}bundle.inputData?.[`${keyPrefix}{{baseName}}`]{{/isEnumRef}}{{/isArray}}{{/allowableValues}}{{#allowableValues}}bundle.inputData?.[`${keyPrefix}{{baseName}}`]{{/allowableValues}}{{/isPrimitiveType}},
{{/vars}}
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const _ = require('lodash')

const replacePathParameters = (url) => url.replace(/{([^{}]+)}/g, (keyExpr, key) => `{{bundle.inputData.${key}}}`)
const removeKeyPrefixes = (objectsArray, currentPath) => objectsArray == undefined || typeof objectsArray[0] != 'object' ? objectsArray : objectsArray.map((obj) => Object.keys(obj).reduce((res, key) =>_.set(res, key.replace(`${currentPath}.`, ''), obj[key]), {}))
const childMapping = (objectsArray, prefix, model) => objectsArray.map(object => model.mapping({inputData: object}, prefix))
const removeIfEmpty = (obj) => _.isEmpty(JSON.parse(JSON.stringify(obj))) ? undefined : obj
const buildKeyAndLabel = (prefix, isInput = true, isArrayChild = false) => {
const keyPrefix = !_.isEmpty(prefix) && (!isArrayChild || isInput) ? `${prefix}${isInput ? '.' : '__'}` : prefix
Expand All @@ -25,7 +25,7 @@ const searchMiddleware = (action) => {

module.exports = {
replacePathParameters: replacePathParameters,
removeKeyPrefixes: removeKeyPrefixes,
childMapping: childMapping,
removeIfEmpty: removeIfEmpty,
buildKeyAndLabel: buildKeyAndLabel,
hasSearchRequisites: hasSearchRequisites,
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/zapier/models/Pet.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module.exports = {
'category': utils.removeIfEmpty(Category.mapping(bundle, `${keyPrefix}category`)),
'name': bundle.inputData?.[`${keyPrefix}name`],
'photoUrls': bundle.inputData?.[`${keyPrefix}photoUrls`],
'tags': utils.removeKeyPrefixes(bundle.inputData?.[`${keyPrefix}tags`], `${keyPrefix}tags`),
'tags': utils.childMapping(bundle.inputData?.[`${keyPrefix}tags`], `${keyPrefix}tags`, Tag),
'status': bundle.inputData?.[`${keyPrefix}status`],
}
},
Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/zapier/utils/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const _ = require('lodash')

const replacePathParameters = (url) => url.replace(/{([^{}]+)}/g, (keyExpr, key) => `{{bundle.inputData.${key}}}`)
const removeKeyPrefixes = (objectsArray, currentPath) => objectsArray == undefined || typeof objectsArray[0] != 'object' ? objectsArray : objectsArray.map((obj) => Object.keys(obj).reduce((res, key) =>_.set(res, key.replace(`${currentPath}.`, ''), obj[key]), {}))
const childMapping = (objectsArray, prefix, model) => objectsArray.map(object => model.mapping({inputData: object}, prefix))
const removeIfEmpty = (obj) => _.isEmpty(JSON.parse(JSON.stringify(obj))) ? undefined : obj
const buildKeyAndLabel = (prefix, isInput = true, isArrayChild = false) => {
const keyPrefix = !_.isEmpty(prefix) && (!isArrayChild || isInput) ? `${prefix}${isInput ? '.' : '__'}` : prefix
Expand All @@ -25,7 +25,7 @@ const searchMiddleware = (action) => {

module.exports = {
replacePathParameters: replacePathParameters,
removeKeyPrefixes: removeKeyPrefixes,
childMapping: childMapping,
removeIfEmpty: removeIfEmpty,
buildKeyAndLabel: buildKeyAndLabel,
hasSearchRequisites: hasSearchRequisites,
Expand Down

0 comments on commit bbf9ccd

Please sign in to comment.