From c7b93d6487d86dd1b36eee9bc100c8f798fb6eb1 Mon Sep 17 00:00:00 2001 From: snyk-bot Date: Fri, 11 Sep 2020 04:23:58 +0000 Subject: [PATCH 1/7] fix: upgrade axios from 0.19.2 to 0.20.0 Snyk has created this PR to upgrade axios from 0.19.2 to 0.20.0. See this package in npm: https://www.npmjs.com/package/axios See this project in Snyk: https://app.snyk.io/org/customer-facing-tools/project/6613bc94-4d46-4e73-b9b1-038f45a7e8f8?utm_source=github&utm_medium=upgrade-pr --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 998b0d5..61e71b8 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "@snyk/configstore": "^3.2.0-rc1", "@types/lodash": "^4.14.155", "@types/node": "^14.0.12", - "axios": "^0.19.2", + "axios": "^0.20.0", "debug": "^4.1.1", "jsonq": "^1.2.0", "lodash": "^4.17.15", From 154a255750ace342c9821957522c66b4a1a36194 Mon Sep 17 00:00:00 2001 From: ghe Date: Wed, 13 Jan 2021 16:36:39 +0000 Subject: [PATCH 2/7] feat: upgrade snyk-request-manager to 1.3.1 --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 61e71b8..66aed06 100644 --- a/package.json +++ b/package.json @@ -49,13 +49,13 @@ "@snyk/configstore": "^3.2.0-rc1", "@types/lodash": "^4.14.155", "@types/node": "^14.0.12", - "axios": "^0.20.0", + "axios": "0.21.1", "debug": "^4.1.1", "jsonq": "^1.2.0", "lodash": "^4.17.15", "snyk": "^1.360.0", "snyk-config": "^3.0.0", - "snyk-request-manager": "^1.1.0", + "snyk-request-manager": "1.3.1", "source-map-support": "^0.5.16", "tslib": "^1.10.0", "typescript": "^3.9.5" From 3f673e529790396971a75f1936c558d4fbe55075 Mon Sep 17 00:00:00 2001 From: aarlaud Date: Fri, 15 Jan 2021 12:59:31 +0100 Subject: [PATCH 3/7] fix: handle /user/me double get method special case to avoid codegen issue --- src/lib/generators/generate.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/lib/generators/generate.ts b/src/lib/generators/generate.ts index cdbc4a8..c268b50 100644 --- a/src/lib/generators/generate.ts +++ b/src/lib/generators/generate.ts @@ -31,6 +31,7 @@ interface PreparedMethod { name: string; argsList: Array; qsList?: Array; + paramList?: Array, url: string; response?: Response; } @@ -429,12 +430,14 @@ const generateMethods = (classToGenerateMethodsFor: ConsolidatedClass) => { const currentMethod: PreparedMethod = { name: method.verb, + paramList: paramsCode, argsList: argsList, url: urlForPreparedMethod, response: method.response, }; - + // console.log(method) if (methodsMap.has(method.verb)) { + let paramList = _.uniq(method.params.concat(method.qsParams)); let url = method.url; @@ -442,8 +445,13 @@ const generateMethods = (classToGenerateMethodsFor: ConsolidatedClass) => { let existingUrl = methodsMap.get(method.verb)?.url; let finalMethodParamList: string[] = []; - - if (existingMethodParamList.length > paramList.length) { + if(existingMethodParamList.length == 0 && paramList.length == 0) { + // Just passing through if no parameters but only different values in the path + // so far only /user/{usedId}, with userId=me being a special case called out + // in other words, userId being 'me' or another value if handled in the class constructor + // so no need to tweak the url + url = `${existingUrl}` + } else if (existingMethodParamList.length > paramList.length) { finalMethodParamList = existingMethodParamList; const paramListDifference = _.difference( existingMethodParamList, @@ -501,6 +509,7 @@ const generateMethods = (classToGenerateMethodsFor: ConsolidatedClass) => { const url = `${currentMethod.url}`; const updatedMethod: PreparedMethod = { name: method.verb, + paramList: currentMethod.paramList, argsList: currentMethod.argsList, url: url, response: currentMethod.response, @@ -588,7 +597,7 @@ import { requestsManager } from 'snyk-request-manager' const requestManager = new requestsManager(${requestManagerSettings}) `; -console.log('fdfd') + parsedJSON.forEach((classItem) => { fs.writeFileSync( From 9e359d6b4a229a49afcd09352540a856593db9fe Mon Sep 17 00:00:00 2001 From: aarlaud Date: Fri, 15 Jan 2021 23:53:49 +0100 Subject: [PATCH 4/7] fix: handle post requests with empty body --- src/lib/generators/generate.ts | 43 +++++++++++++++++++------ src/lib/generators/generateTestCases.ts | 25 +++++++++----- 2 files changed, 51 insertions(+), 17 deletions(-) diff --git a/src/lib/generators/generate.ts b/src/lib/generators/generate.ts index c268b50..95745f0 100644 --- a/src/lib/generators/generate.ts +++ b/src/lib/generators/generate.ts @@ -31,7 +31,7 @@ interface PreparedMethod { name: string; argsList: Array; qsList?: Array; - paramList?: Array, + paramList?: Array; url: string; response?: Response; } @@ -307,6 +307,22 @@ const generateConstructors = ( `; } let noInterface: boolean = false; + let optionalConstructorParam: string = ''; + if ( + classToGenerateConstructorsFor.methods && + classToGenerateConstructorsFor.methods.length > 0 + ) { + if ( + classToGenerateConstructorsFor.methods.some( + (method) => method.params.length == 0, + ) + ) { + // the root class constructor argument should therefore be optional + // as there might be methods not needing it (POST /org for example) + optionalConstructorParam = '?'; + } + } + if ( classToGenerateConstructorsFor.param && classToGenerateConstructorsFor.param.filter((x) => x).length > 0 @@ -314,7 +330,7 @@ const generateConstructors = ( constructorsDeclaration.push( `${utils.formatClassName( classToGenerateConstructorsFor.name, - )}param:${utils + )}param${optionalConstructorParam}:${utils .formatClassName(classToGenerateConstructorsFor.name) .toLowerCase()}Class`, ); @@ -339,7 +355,9 @@ const generateConstructors = ( parameter, )} = ${utils.formatClassName( classToGenerateConstructorsFor.name, - )}param.${utils.removeCurlyBraces(parameter)} || ${defaultValue} + )}param${optionalConstructorParam}.${utils.removeCurlyBraces( + parameter, + )} || ${defaultValue} `; } }); @@ -437,7 +455,6 @@ const generateMethods = (classToGenerateMethodsFor: ConsolidatedClass) => { }; // console.log(method) if (methodsMap.has(method.verb)) { - let paramList = _.uniq(method.params.concat(method.qsParams)); let url = method.url; @@ -445,13 +462,13 @@ const generateMethods = (classToGenerateMethodsFor: ConsolidatedClass) => { let existingUrl = methodsMap.get(method.verb)?.url; let finalMethodParamList: string[] = []; - if(existingMethodParamList.length == 0 && paramList.length == 0) { + if (existingMethodParamList.length == 0 && paramList.length == 0) { // Just passing through if no parameters but only different values in the path // so far only /user/{usedId}, with userId=me being a special case called out // in other words, userId being 'me' or another value if handled in the class constructor // so no need to tweak the url - url = `${existingUrl}` - } else if (existingMethodParamList.length > paramList.length) { + url = `${existingUrl}`; + } else if (existingMethodParamList.length > paramList.length) { finalMethodParamList = existingMethodParamList; const paramListDifference = _.difference( existingMethodParamList, @@ -540,6 +557,15 @@ const generateMethods = (classToGenerateMethodsFor: ConsolidatedClass) => { urlQueryParams.push('${qsParameterName}='+${qsParameterName}) }\n`; }); + + let emptyBodyNeeded = false; + if ( + (method.name == 'post' || method.name == 'put') && + !method.argsList.map((x) => x.split(':')[0]).includes('body') + ) { + emptyBodyNeeded = true; + } + codeToReturn += ` async ${method.name} (${method.argsList}):${ method.response?.type == 'bodyless' @@ -563,7 +589,7 @@ const generateMethods = (classToGenerateMethodsFor: ConsolidatedClass) => { }', url: url ${ method.argsList.map((x) => x.split(':')[0]).includes('body') ? ',body : JSON.stringify(body)' - : '' + : `${emptyBodyNeeded ? ', body: JSON.stringify({})' : ''}` }}) if(!Object(this.currentContext)['fullResponse'] && result.data){ return result.data @@ -598,7 +624,6 @@ const requestManager = new requestsManager(${requestManagerSettings}) `; - parsedJSON.forEach((classItem) => { fs.writeFileSync( path.join('./src/lib/client/generated/' + classItem.name + '.ts'), diff --git a/src/lib/generators/generateTestCases.ts b/src/lib/generators/generateTestCases.ts index a430e6f..1931936 100644 --- a/src/lib/generators/generateTestCases.ts +++ b/src/lib/generators/generateTestCases.ts @@ -92,7 +92,10 @@ const extractCommandDetailsFromClass = ( }); }); - if (method.verb == 'put' || method.verb == 'post') { + if ( + (method.verb == 'put' || method.verb == 'post') && + !_.isEmpty(method.body) + ) { listOfParams.unshift('body'); } @@ -293,13 +296,13 @@ const generateTestFile = ( }; `; - if (commandMethod == 'put' || commandMethod == 'post') { - // extract the namespace(s) from command after the first class instantiation - const bodyType = extractBodyTypeFromCommand(command[0]); + // if (commandMethod == 'put' || commandMethod == 'post') { + // // extract the namespace(s) from command after the first class instantiation + // const bodyType = extractBodyTypeFromCommand(command[0]); - // codeToReturn += `const body: ${bodyType} = {body:fixtures.request.body.${commandCoordinates.join('.')}} - // `; - } + // // codeToReturn += `const body: ${bodyType} = {body:fixtures.request.body.${commandCoordinates.join('.')}} + // // `; + // } let url = command[1].replace(/{/g, '${fixtures.request.'); if ( @@ -339,7 +342,13 @@ const generateTestFile = ( mockAxios.mockResponseFor({url: \`${url}\`},axiosResponse); expect(mockAxios.${commandMethod}).toHaveBeenCalledWith(\`${url}\`${ - body.join() == '' ? '' : ', JSON.stringify(' + body.join() + ')' + body.join() == '' + ? `${ + commandMethod == 'post' || commandMethod == 'put' + ? ',JSON.stringify({})' + : '' + }` + : ', JSON.stringify(' + body.join() + ')' }) From 53bdbd4dc658f89bf584abc6718cd8b0def20dd0 Mon Sep 17 00:00:00 2001 From: snyk-bot Date: Thu, 24 Dec 2020 04:24:24 +0000 Subject: [PATCH 5/7] fix: package.json to reduce vulnerabilities The following vulnerabilities are fixed with an upgrade: - https://snyk.io/vuln/SNYK-JS-AXIOS-1038255 --- .gitignore | 3 +-- .npmrc | 2 ++ package.json | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) create mode 100644 .npmrc diff --git a/.gitignore b/.gitignore index ab3f1a4..ce24b72 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ node_modules/ npm-debug.log -.npmrc # output dist .DS_Store @@ -14,4 +13,4 @@ snyk.json snyk-prepared.json *.bak test/fixtures/*.json -test/lib/*.test.ts \ No newline at end of file +test/lib/*.test.ts diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..b15cbc2 --- /dev/null +++ b/.npmrc @@ -0,0 +1,2 @@ +package-lock=false + diff --git a/package.json b/package.json index 66aed06..5f4dbe3 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "@snyk/configstore": "^3.2.0-rc1", "@types/lodash": "^4.14.155", "@types/node": "^14.0.12", - "axios": "0.21.1", + "axios": "^0.21.1", "debug": "^4.1.1", "jsonq": "^1.2.0", "lodash": "^4.17.15", From c401a122365f9c1dc857c3c7c0f5b457fea22593 Mon Sep 17 00:00:00 2001 From: snyk-bot Date: Tue, 24 Nov 2020 04:23:57 +0000 Subject: [PATCH 6/7] fix: package.json to reduce vulnerabilities The following vulnerabilities are fixed with an upgrade: - https://snyk.io/vuln/SNYK-JS-Y18N-1021887 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5f4dbe3..2441d3e 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "jsonq": "^1.2.0", "lodash": "^4.17.15", "snyk": "^1.360.0", - "snyk-config": "^3.0.0", + "snyk-config": "^4.0.0", "snyk-request-manager": "1.3.1", "source-map-support": "^0.5.16", "tslib": "^1.10.0", From ffad819406ac5cde2c4c6fd1afa7e3a8d8251dfd Mon Sep 17 00:00:00 2001 From: ghe Date: Mon, 18 Jan 2021 12:19:43 +0000 Subject: [PATCH 7/7] chore: drop console log --- src/lib/generators/generate.ts | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/lib/generators/generate.ts b/src/lib/generators/generate.ts index 95745f0..57ff23a 100644 --- a/src/lib/generators/generate.ts +++ b/src/lib/generators/generate.ts @@ -81,11 +81,11 @@ const generateClass = ( isRootClass, )} ${generateConstructors(classToGenerate, parentClassType, isRootClass)} - - + + ${generateMethods(classToGenerate)} - + } ${generateSubClasses(classToGenerate, isRootClass)} `; @@ -204,7 +204,7 @@ const generateResponseInterfaces = ( } { header: ${method.response.header} } - + `; break; case 'bodyless': @@ -269,7 +269,7 @@ const generateSubClasses = ( if (subClassArray) { subClassArray.forEach((subClass) => { codeToReturn += `${generateClass(subClass, classType, false)} - + `; }); } @@ -294,7 +294,7 @@ const generateConstructors = ( if (!isRootClass) { constructorsDeclaration.push(`parentContext: Object`); constructorsParameters += ` - + const properties = Object.getOwnPropertyNames(parentContext) properties.forEach(property => { Object(this.currentContext)[property] = Object(parentContext)[property] @@ -453,7 +453,6 @@ const generateMethods = (classToGenerateMethodsFor: ConsolidatedClass) => { url: urlForPreparedMethod, response: method.response, }; - // console.log(method) if (methodsMap.has(method.verb)) { let paramList = _.uniq(method.params.concat(method.qsParams)); let url = method.url; @@ -553,7 +552,7 @@ const generateMethods = (classToGenerateMethodsFor: ConsolidatedClass) => { let qsIfStatements = ''; qsParametersNamesList.forEach((qsParameterName) => { qsIfStatements += ` - if(${qsParameterName}){ + if(${qsParameterName}){ urlQueryParams.push('${qsParameterName}='+${qsParameterName}) }\n`; }); @@ -582,7 +581,7 @@ const generateMethods = (classToGenerateMethodsFor: ConsolidatedClass) => { if(urlQueryParams.length > 0){ url += \`?\${urlQueryParams.join("&")}\` } - + try { const result = await requestManager.request({verb: '${ method.name @@ -596,11 +595,11 @@ const generateMethods = (classToGenerateMethodsFor: ConsolidatedClass) => { } else { return result } - + } catch (err) { throw new ClientError(err) } - + } `; });