build entire json object for request and only support first requestBody content type

This commit is contained in:
bplatta 2023-10-13 11:30:27 -05:00
parent 35db296c1f
commit 36ee1f542a

View File

@ -20,6 +20,21 @@ const ensureUrl = (url) => {
return protUrl.replace(/([^:]\/)\/+/g, '$1');
};
const buildEmptyJsonBody = (bodySchema) => {
let _jsonBody = {};
each(bodySchema.properties || {}, (prop, name) => {
if (prop.type === 'object') {
_jsonBody[name] = buildEmptyJsonBody(prop);
// handle arrays
} else if (prop.type === 'array') {
_jsonBody[name] = [];
} else {
_jsonBody[name] = '';
}
});
return _jsonBody;
};
const transformOpenapiRequestItem = (request) => {
let _operationObject = request.operationObject;
const brunoRequestItem = {
@ -102,20 +117,19 @@ const transformOpenapiRequestItem = (request) => {
// TODO: handle allOf/anyOf/oneOf
if (_operationObject.requestBody) {
let content = get(_operationObject, 'requestBody.content', {});
Object.entries(content).forEach(([mimeType, body]) => {
let mimeType = Object.keys(content)[0];
let body = content[mimeType] || {};
let bodySchema = body.schema;
if (mimeType === 'application/json') {
brunoRequestItem.request.body.mode = 'json';
if (body.schema && body.schema.type === 'object') {
let _jsonBody = {};
each(body.schema.properties || {}, (prop, name) => {
_jsonBody[name] = '';
});
if (bodySchema && bodySchema.type === 'object') {
let _jsonBody = buildEmptyJsonBody(bodySchema);
brunoRequestItem.request.body.json = JSON.stringify(_jsonBody, null, 2);
}
} else if (mimeType === 'application/x-www-form-urlencoded') {
brunoRequestItem.request.body.mode = 'formUrlEncoded';
if (body.schema && body.schema.type === 'object') {
each(body.schema.properties || {}, (prop, name) => {
if (bodySchema && bodySchema.type === 'object') {
each(bodySchema.properties || {}, (prop, name) => {
brunoRequestItem.request.body.formUrlEncoded.push({
uid: uuid(),
name: name,
@ -127,8 +141,8 @@ const transformOpenapiRequestItem = (request) => {
}
} else if (mimeType === 'multipart/form-data') {
brunoRequestItem.request.body.mode = 'multipartForm';
if (body.schema && body.schema.type === 'object') {
each(body.schema.properties || {}, (prop, name) => {
if (bodySchema && bodySchema.type === 'object') {
each(bodySchema.properties || {}, (prop, name) => {
brunoRequestItem.request.body.multipartForm.push({
uid: uuid(),
name: name,
@ -145,7 +159,6 @@ const transformOpenapiRequestItem = (request) => {
brunoRequestItem.request.body.mode = 'xml';
brunoRequestItem.request.body.xml = '';
}
});
}
return brunoRequestItem;