Skip to content

fix(bruno-lang): Fix multiline text blocks in multipart form #4619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions packages/bruno-lang/v2/src/bruToJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const { safeParseJson, outdentString } = require('./utils');
* body:json {
* {
* "username": "John Nash",
* "password": "governingdynamics
* "password": "governingdynamics"
* }
*
*/
Expand All @@ -38,7 +38,7 @@ const grammar = ohm.grammar(`Bru {

// Multiline text block surrounded by '''
multilinetextblockdelimiter = "'''"
multilinetextblock = multilinetextblockdelimiter (~multilinetextblockdelimiter any)* multilinetextblockdelimiter
multilinetextblock = multilinetextblockdelimiter (~multilinetextblockdelimiter any)* multilinetextblockdelimiter (st* (~nl any)*)?

// Dictionary Blocks
dictionary = st* "{" pairlist? tagend
Expand Down Expand Up @@ -164,12 +164,45 @@ const mapRequestParams = (pairList = [], type) => {

const multipartExtractContentType = (pair) => {
if (_.isString(pair.value)) {
const match = pair.value.match(/^(.*?)\s*@contentType\((.*?)\)\s*$/);
if (match != null && match.length > 2) {
pair.value = match[1];
pair.contentType = match[2];
if (pair.value.startsWith("'''") && pair.value.includes("'''", 3)) {
const closingQuotesPos = pair.value.indexOf("'''", 3);
let multilineContent = pair.value.substring(3, closingQuotesPos);

if (multilineContent) {
const lines = multilineContent.split('\n');
let minIndent = Infinity;
lines.forEach(line => {
if (line.trim()) {
const indent = line.match(/^\s*/)[0].length;
if (indent < minIndent) minIndent = indent;
}
});

if (minIndent < Infinity) {
multilineContent = lines.map(line =>
line.trim() ? line.substring(minIndent) : line
).join('\n');
}
}

const remaining = pair.value.substring(closingQuotesPos + 3);
const contentTypeMatch = remaining.match(/@contentType\((.*?)\)/);

if (contentTypeMatch && contentTypeMatch[1]) {
pair.contentType = contentTypeMatch[1].trim();
pair.value = multilineContent.trim();
} else {
pair.contentType = '';
pair.value = multilineContent.trim();
}
} else {
pair.contentType = '';
const match = pair.value.match(/^(.*?)\s*@contentType\((.*?)\)\s*$/);
if (match != null && match.length > 2) {
pair.value = match[1];
pair.contentType = match[2];
} else {
pair.contentType = '';
}
}
}
};
Expand Down
11 changes: 8 additions & 3 deletions packages/bruno-lang/v2/tests/fixtures/request.bru
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,14 @@ body:form-urlencoded {
}

body:multipart-form {
apikey: secret
numbers: +91998877665
~message: hello
apikey: secret @contentType(application/text)
numbers: +91998877665 @contentType(application/text)
~message: '''
{
"type": "text",
"value": "hello"
}
''' @contentType(application/text)
}

body:file {
Expand Down
8 changes: 4 additions & 4 deletions packages/bruno-lang/v2/tests/fixtures/request.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,23 +125,23 @@
],
"multipartForm": [
{
"contentType": "",
"contentType": "application/text",
"name": "apikey",
"value": "secret",
"enabled": true,
"type": "text"
},
{
"contentType": "",
"contentType": "application/text",
"name": "numbers",
"value": "+91998877665",
"enabled": true,
"type": "text"
},
{
"contentType": "",
"contentType": "application/text",
"name": "message",
"value": "hello",
"value": "{\n\"type\": \"text\",\n\"value\": \"hello\"\n}",
"enabled": false,
"type": "text"
}
Expand Down