diff --git a/modules/openapi-generator/src/main/resources/nodejs-express-server/controllers/Controller.mustache b/modules/openapi-generator/src/main/resources/nodejs-express-server/controllers/Controller.mustache index 5248666fb7e..c791a891587 100644 --- a/modules/openapi-generator/src/main/resources/nodejs-express-server/controllers/Controller.mustache +++ b/modules/openapi-generator/src/main/resources/nodejs-express-server/controllers/Controller.mustache @@ -1,15 +1,16 @@ const fs = require('fs'); const path = require('path'); +const camelCase = require('camelcase'); const config = require('../config'); const logger = require('../logger'); class Controller { static sendResponse(response, payload) { /** - * The default response-code is 200. We want to allow to change that. in That case, - * payload will be an object consisting of a code and a payload. If not customized - * send 200 and the payload as received in this method. - */ + * The default response-code is 200. We want to allow to change that. in That case, + * payload will be an object consisting of a code and a payload. If not customized + * send 200 and the payload as received in this method. + */ response.status(payload.code || 200); const responsePayload = payload.payload !== undefined ? payload.payload : payload; if (responsePayload instanceof Object) { @@ -29,16 +30,16 @@ class Controller { } /** - * Files have been uploaded to the directory defined by config.js as upload directory - * Files have a temporary name, that was saved as 'filename' of the file object that is - * referenced in reuquest.files array. - * This method finds the file and changes it to the file name that was originally called - * when it was uploaded. To prevent files from being overwritten, a timestamp is added between - * the filename and its extension - * @param request - * @param fieldName - * @returns {string} - */ + * Files have been uploaded to the directory defined by config.js as upload directory + * Files have a temporary name, that was saved as 'filename' of the file object that is + * referenced in reuquest.files array. + * This method finds the file and changes it to the file name that was originally called + * when it was uploaded. To prevent files from being overwritten, a timestamp is added between + * the filename and its extension + * @param request + * @param fieldName + * @returns {string} + */ static collectFile(request, fieldName) { let uploadedFileName = ''; if (request.files && request.files.length > 0) { @@ -55,44 +56,25 @@ class Controller { return uploadedFileName; } - // static collectFiles(request) { - // logger.info('Checking if files are expected in schema'); - // const requestFiles = {}; - // if (request.openapi.schema.requestBody !== undefined) { - // const [contentType] = request.headers['content-type'].split(';'); - // if (contentType === 'multipart/form-data') { - // const contentSchema = request.openapi.schema.requestBody.content[contentType].schema; - // Object.entries(contentSchema.properties).forEach(([name, property]) => { - // if (property.type === 'string' && ['binary', 'base64'].indexOf(property.format) > -1) { - // const fileObject = request.files.find(file => file.fieldname === name); - // const fileArray = fileObject.originalname.split('.'); - // const extension = fileArray.pop(); - // fileArray.push(`_${Date.now()}`); - // const uploadedFileName = `${fileArray.join('')}.${extension}`; - // fs.renameSync(path.join(config.FILE_UPLOAD_PATH, fileObject.filename), - // path.join(config.FILE_UPLOAD_PATH, uploadedFileName)); - // requestFiles[name] = uploadedFileName; - // } - // }); - // } else if (request.openapi.schema.requestBody.content[contentType] !== undefined - // && request.files !== undefined) { - // [request.body] = request.files; - // } - // } - // return requestFiles; - // } + static getRequestBodyName(request) { + const codeGenDefinedBodyName = request.openapi.schema['x-codegen-request-body-name']; + if (codeGenDefinedBodyName !== undefined) { + return codeGenDefinedBodyName; + } + const refObjectPath = request.openapi.schema.requestBody.content['application/json'].schema.$ref; + if (refObjectPath !== undefined && refObjectPath.length > 0) { + return (refObjectPath.substr(refObjectPath.lastIndexOf('/') + 1)); + } + return 'body'; + } static collectRequestParams(request) { const requestParams = {}; if (request.openapi.schema.requestBody !== undefined) { const { content } = request.openapi.schema.requestBody; if (content['application/json'] !== undefined) { - const schema = request.openapi.schema.requestBody.content['application/json']; - if (schema.$ref) { - requestParams[schema.$ref.substr(schema.$ref.lastIndexOf('.'))] = request.body; - } else { - requestParams.body = request.body; - } + const requestBodyName = camelCase(this.getRequestBodyName(request)); + requestParams[requestBodyName] = request.body; } else if (content['multipart/form-data'] !== undefined) { Object.keys(content['multipart/form-data'].schema.properties).forEach( (property) => { @@ -106,14 +88,7 @@ class Controller { ); } } - // if (request.openapi.schema.requestBody.content['application/json'] !== undefined) { - // const schema = request.openapi.schema.requestBody.content['application/json']; - // if (schema.$ref) { - // requestParams[schema.$ref.substr(schema.$ref.lastIndexOf('.'))] = request.body; - // } else { - // requestParams.body = request.body; - // } - // } + request.openapi.schema.parameters.forEach((param) => { if (param.in === 'path') { requestParams[param.name] = request.openapi.pathParams[param.name]; diff --git a/samples/server/petstore/nodejs-express-server/.openapi-generator/VERSION b/samples/server/petstore/nodejs-express-server/.openapi-generator/VERSION index d99e7162d01..71d2eb1c7fc 100644 --- a/samples/server/petstore/nodejs-express-server/.openapi-generator/VERSION +++ b/samples/server/petstore/nodejs-express-server/.openapi-generator/VERSION @@ -1 +1 @@ -5.0.0-SNAPSHOT \ No newline at end of file +5.0.0-SNAPSHOT diff --git a/samples/server/petstore/nodejs-express-server/controllers/Controller.js b/samples/server/petstore/nodejs-express-server/controllers/Controller.js index 5248666fb7e..c791a891587 100644 --- a/samples/server/petstore/nodejs-express-server/controllers/Controller.js +++ b/samples/server/petstore/nodejs-express-server/controllers/Controller.js @@ -1,15 +1,16 @@ const fs = require('fs'); const path = require('path'); +const camelCase = require('camelcase'); const config = require('../config'); const logger = require('../logger'); class Controller { static sendResponse(response, payload) { /** - * The default response-code is 200. We want to allow to change that. in That case, - * payload will be an object consisting of a code and a payload. If not customized - * send 200 and the payload as received in this method. - */ + * The default response-code is 200. We want to allow to change that. in That case, + * payload will be an object consisting of a code and a payload. If not customized + * send 200 and the payload as received in this method. + */ response.status(payload.code || 200); const responsePayload = payload.payload !== undefined ? payload.payload : payload; if (responsePayload instanceof Object) { @@ -29,16 +30,16 @@ class Controller { } /** - * Files have been uploaded to the directory defined by config.js as upload directory - * Files have a temporary name, that was saved as 'filename' of the file object that is - * referenced in reuquest.files array. - * This method finds the file and changes it to the file name that was originally called - * when it was uploaded. To prevent files from being overwritten, a timestamp is added between - * the filename and its extension - * @param request - * @param fieldName - * @returns {string} - */ + * Files have been uploaded to the directory defined by config.js as upload directory + * Files have a temporary name, that was saved as 'filename' of the file object that is + * referenced in reuquest.files array. + * This method finds the file and changes it to the file name that was originally called + * when it was uploaded. To prevent files from being overwritten, a timestamp is added between + * the filename and its extension + * @param request + * @param fieldName + * @returns {string} + */ static collectFile(request, fieldName) { let uploadedFileName = ''; if (request.files && request.files.length > 0) { @@ -55,44 +56,25 @@ class Controller { return uploadedFileName; } - // static collectFiles(request) { - // logger.info('Checking if files are expected in schema'); - // const requestFiles = {}; - // if (request.openapi.schema.requestBody !== undefined) { - // const [contentType] = request.headers['content-type'].split(';'); - // if (contentType === 'multipart/form-data') { - // const contentSchema = request.openapi.schema.requestBody.content[contentType].schema; - // Object.entries(contentSchema.properties).forEach(([name, property]) => { - // if (property.type === 'string' && ['binary', 'base64'].indexOf(property.format) > -1) { - // const fileObject = request.files.find(file => file.fieldname === name); - // const fileArray = fileObject.originalname.split('.'); - // const extension = fileArray.pop(); - // fileArray.push(`_${Date.now()}`); - // const uploadedFileName = `${fileArray.join('')}.${extension}`; - // fs.renameSync(path.join(config.FILE_UPLOAD_PATH, fileObject.filename), - // path.join(config.FILE_UPLOAD_PATH, uploadedFileName)); - // requestFiles[name] = uploadedFileName; - // } - // }); - // } else if (request.openapi.schema.requestBody.content[contentType] !== undefined - // && request.files !== undefined) { - // [request.body] = request.files; - // } - // } - // return requestFiles; - // } + static getRequestBodyName(request) { + const codeGenDefinedBodyName = request.openapi.schema['x-codegen-request-body-name']; + if (codeGenDefinedBodyName !== undefined) { + return codeGenDefinedBodyName; + } + const refObjectPath = request.openapi.schema.requestBody.content['application/json'].schema.$ref; + if (refObjectPath !== undefined && refObjectPath.length > 0) { + return (refObjectPath.substr(refObjectPath.lastIndexOf('/') + 1)); + } + return 'body'; + } static collectRequestParams(request) { const requestParams = {}; if (request.openapi.schema.requestBody !== undefined) { const { content } = request.openapi.schema.requestBody; if (content['application/json'] !== undefined) { - const schema = request.openapi.schema.requestBody.content['application/json']; - if (schema.$ref) { - requestParams[schema.$ref.substr(schema.$ref.lastIndexOf('.'))] = request.body; - } else { - requestParams.body = request.body; - } + const requestBodyName = camelCase(this.getRequestBodyName(request)); + requestParams[requestBodyName] = request.body; } else if (content['multipart/form-data'] !== undefined) { Object.keys(content['multipart/form-data'].schema.properties).forEach( (property) => { @@ -106,14 +88,7 @@ class Controller { ); } } - // if (request.openapi.schema.requestBody.content['application/json'] !== undefined) { - // const schema = request.openapi.schema.requestBody.content['application/json']; - // if (schema.$ref) { - // requestParams[schema.$ref.substr(schema.$ref.lastIndexOf('.'))] = request.body; - // } else { - // requestParams.body = request.body; - // } - // } + request.openapi.schema.parameters.forEach((param) => { if (param.in === 'path') { requestParams[param.name] = request.openapi.pathParams[param.name];