forked from loafle/openapi-generator-original
Pass body to service (#6041)
* Fixed a bug where request body names were not being transferred appropriately based on openapi definitions * Added changes made to output samples * Fixed formatting on Controller.mustache * Added sample code generated after last change
This commit is contained in:
parent
93bd8571d3
commit
5cdc9e9e35
@ -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];
|
||||
|
@ -1 +1 @@
|
||||
5.0.0-SNAPSHOT
|
||||
5.0.0-SNAPSHOT
|
||||
|
@ -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];
|
||||
|
Loading…
x
Reference in New Issue
Block a user