mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-07-03 06:00:52 +00:00
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 fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const camelCase = require('camelcase');
|
||||||
const config = require('../config');
|
const config = require('../config');
|
||||||
const logger = require('../logger');
|
const logger = require('../logger');
|
||||||
|
|
||||||
class Controller {
|
class Controller {
|
||||||
static sendResponse(response, payload) {
|
static sendResponse(response, payload) {
|
||||||
/**
|
/**
|
||||||
* The default response-code is 200. We want to allow to change that. in That case,
|
* 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
|
* 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.
|
* send 200 and the payload as received in this method.
|
||||||
*/
|
*/
|
||||||
response.status(payload.code || 200);
|
response.status(payload.code || 200);
|
||||||
const responsePayload = payload.payload !== undefined ? payload.payload : payload;
|
const responsePayload = payload.payload !== undefined ? payload.payload : payload;
|
||||||
if (responsePayload instanceof Object) {
|
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 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
|
* Files have a temporary name, that was saved as 'filename' of the file object that is
|
||||||
* referenced in reuquest.files array.
|
* referenced in reuquest.files array.
|
||||||
* This method finds the file and changes it to the file name that was originally called
|
* 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
|
* when it was uploaded. To prevent files from being overwritten, a timestamp is added between
|
||||||
* the filename and its extension
|
* the filename and its extension
|
||||||
* @param request
|
* @param request
|
||||||
* @param fieldName
|
* @param fieldName
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
static collectFile(request, fieldName) {
|
static collectFile(request, fieldName) {
|
||||||
let uploadedFileName = '';
|
let uploadedFileName = '';
|
||||||
if (request.files && request.files.length > 0) {
|
if (request.files && request.files.length > 0) {
|
||||||
@ -55,44 +56,25 @@ class Controller {
|
|||||||
return uploadedFileName;
|
return uploadedFileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
// static collectFiles(request) {
|
static getRequestBodyName(request) {
|
||||||
// logger.info('Checking if files are expected in schema');
|
const codeGenDefinedBodyName = request.openapi.schema['x-codegen-request-body-name'];
|
||||||
// const requestFiles = {};
|
if (codeGenDefinedBodyName !== undefined) {
|
||||||
// if (request.openapi.schema.requestBody !== undefined) {
|
return codeGenDefinedBodyName;
|
||||||
// const [contentType] = request.headers['content-type'].split(';');
|
}
|
||||||
// if (contentType === 'multipart/form-data') {
|
const refObjectPath = request.openapi.schema.requestBody.content['application/json'].schema.$ref;
|
||||||
// const contentSchema = request.openapi.schema.requestBody.content[contentType].schema;
|
if (refObjectPath !== undefined && refObjectPath.length > 0) {
|
||||||
// Object.entries(contentSchema.properties).forEach(([name, property]) => {
|
return (refObjectPath.substr(refObjectPath.lastIndexOf('/') + 1));
|
||||||
// if (property.type === 'string' && ['binary', 'base64'].indexOf(property.format) > -1) {
|
}
|
||||||
// const fileObject = request.files.find(file => file.fieldname === name);
|
return 'body';
|
||||||
// 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 collectRequestParams(request) {
|
static collectRequestParams(request) {
|
||||||
const requestParams = {};
|
const requestParams = {};
|
||||||
if (request.openapi.schema.requestBody !== undefined) {
|
if (request.openapi.schema.requestBody !== undefined) {
|
||||||
const { content } = request.openapi.schema.requestBody;
|
const { content } = request.openapi.schema.requestBody;
|
||||||
if (content['application/json'] !== undefined) {
|
if (content['application/json'] !== undefined) {
|
||||||
const schema = request.openapi.schema.requestBody.content['application/json'];
|
const requestBodyName = camelCase(this.getRequestBodyName(request));
|
||||||
if (schema.$ref) {
|
requestParams[requestBodyName] = request.body;
|
||||||
requestParams[schema.$ref.substr(schema.$ref.lastIndexOf('.'))] = request.body;
|
|
||||||
} else {
|
|
||||||
requestParams.body = request.body;
|
|
||||||
}
|
|
||||||
} else if (content['multipart/form-data'] !== undefined) {
|
} else if (content['multipart/form-data'] !== undefined) {
|
||||||
Object.keys(content['multipart/form-data'].schema.properties).forEach(
|
Object.keys(content['multipart/form-data'].schema.properties).forEach(
|
||||||
(property) => {
|
(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) => {
|
request.openapi.schema.parameters.forEach((param) => {
|
||||||
if (param.in === 'path') {
|
if (param.in === 'path') {
|
||||||
requestParams[param.name] = request.openapi.pathParams[param.name];
|
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 fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const camelCase = require('camelcase');
|
||||||
const config = require('../config');
|
const config = require('../config');
|
||||||
const logger = require('../logger');
|
const logger = require('../logger');
|
||||||
|
|
||||||
class Controller {
|
class Controller {
|
||||||
static sendResponse(response, payload) {
|
static sendResponse(response, payload) {
|
||||||
/**
|
/**
|
||||||
* The default response-code is 200. We want to allow to change that. in That case,
|
* 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
|
* 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.
|
* send 200 and the payload as received in this method.
|
||||||
*/
|
*/
|
||||||
response.status(payload.code || 200);
|
response.status(payload.code || 200);
|
||||||
const responsePayload = payload.payload !== undefined ? payload.payload : payload;
|
const responsePayload = payload.payload !== undefined ? payload.payload : payload;
|
||||||
if (responsePayload instanceof Object) {
|
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 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
|
* Files have a temporary name, that was saved as 'filename' of the file object that is
|
||||||
* referenced in reuquest.files array.
|
* referenced in reuquest.files array.
|
||||||
* This method finds the file and changes it to the file name that was originally called
|
* 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
|
* when it was uploaded. To prevent files from being overwritten, a timestamp is added between
|
||||||
* the filename and its extension
|
* the filename and its extension
|
||||||
* @param request
|
* @param request
|
||||||
* @param fieldName
|
* @param fieldName
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
static collectFile(request, fieldName) {
|
static collectFile(request, fieldName) {
|
||||||
let uploadedFileName = '';
|
let uploadedFileName = '';
|
||||||
if (request.files && request.files.length > 0) {
|
if (request.files && request.files.length > 0) {
|
||||||
@ -55,44 +56,25 @@ class Controller {
|
|||||||
return uploadedFileName;
|
return uploadedFileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
// static collectFiles(request) {
|
static getRequestBodyName(request) {
|
||||||
// logger.info('Checking if files are expected in schema');
|
const codeGenDefinedBodyName = request.openapi.schema['x-codegen-request-body-name'];
|
||||||
// const requestFiles = {};
|
if (codeGenDefinedBodyName !== undefined) {
|
||||||
// if (request.openapi.schema.requestBody !== undefined) {
|
return codeGenDefinedBodyName;
|
||||||
// const [contentType] = request.headers['content-type'].split(';');
|
}
|
||||||
// if (contentType === 'multipart/form-data') {
|
const refObjectPath = request.openapi.schema.requestBody.content['application/json'].schema.$ref;
|
||||||
// const contentSchema = request.openapi.schema.requestBody.content[contentType].schema;
|
if (refObjectPath !== undefined && refObjectPath.length > 0) {
|
||||||
// Object.entries(contentSchema.properties).forEach(([name, property]) => {
|
return (refObjectPath.substr(refObjectPath.lastIndexOf('/') + 1));
|
||||||
// if (property.type === 'string' && ['binary', 'base64'].indexOf(property.format) > -1) {
|
}
|
||||||
// const fileObject = request.files.find(file => file.fieldname === name);
|
return 'body';
|
||||||
// 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 collectRequestParams(request) {
|
static collectRequestParams(request) {
|
||||||
const requestParams = {};
|
const requestParams = {};
|
||||||
if (request.openapi.schema.requestBody !== undefined) {
|
if (request.openapi.schema.requestBody !== undefined) {
|
||||||
const { content } = request.openapi.schema.requestBody;
|
const { content } = request.openapi.schema.requestBody;
|
||||||
if (content['application/json'] !== undefined) {
|
if (content['application/json'] !== undefined) {
|
||||||
const schema = request.openapi.schema.requestBody.content['application/json'];
|
const requestBodyName = camelCase(this.getRequestBodyName(request));
|
||||||
if (schema.$ref) {
|
requestParams[requestBodyName] = request.body;
|
||||||
requestParams[schema.$ref.substr(schema.$ref.lastIndexOf('.'))] = request.body;
|
|
||||||
} else {
|
|
||||||
requestParams.body = request.body;
|
|
||||||
}
|
|
||||||
} else if (content['multipart/form-data'] !== undefined) {
|
} else if (content['multipart/form-data'] !== undefined) {
|
||||||
Object.keys(content['multipart/form-data'].schema.properties).forEach(
|
Object.keys(content['multipart/form-data'].schema.properties).forEach(
|
||||||
(property) => {
|
(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) => {
|
request.openapi.schema.parameters.forEach((param) => {
|
||||||
if (param.in === 'path') {
|
if (param.in === 'path') {
|
||||||
requestParams[param.name] = request.openapi.pathParams[param.name];
|
requestParams[param.name] = request.openapi.pathParams[param.name];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user