Skip to content

Commit

Permalink
fix: query parameters containing a colon (:) are not parsed correctly…
Browse files Browse the repository at this point in the history
… when changed

Adjust Bruno grammar so that dictionary key may optionally be quoted. Automatically quote query parameter keys if they contain ":".
  • Loading branch information
pietrygamat committed Sep 5, 2024
1 parent c85d7b0 commit 9c718da
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
12 changes: 10 additions & 2 deletions packages/bruno-lang/v2/src/bruToJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ const grammar = ohm.grammar(`Bru {
dictionary = st* "{" pairlist? tagend
pairlist = optionalnl* pair (~tagend stnl* pair)* (~tagend space)*
pair = st* key st* ":" st* value st*
key = keychar*
quoted_key = "\\"" (~"\\"" any)* "\\""
key = quoted_key | keychar*
value = multilinetextblock | valuechar*
// Dictionary for Assert Block
Expand Down Expand Up @@ -212,7 +213,14 @@ const sem = grammar.createSemantics().addAttribute('ast', {
return res;
},
key(chars) {
return chars.sourceString ? chars.sourceString.trim() : '';
const unquote = (string) => {
if(string.length >= 2 && string[0] === '\"' && string[string.length-1] === '\"') {
return string.slice(1, -1);
} else {
return string;
}
}
return chars.sourceString ? unquote(chars.sourceString.trim()) : '';
},
value(chars) {
try {
Expand Down
7 changes: 5 additions & 2 deletions packages/bruno-lang/v2/src/jsonToBru.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const { indentString } = require('../../v1/src/utils');

const enabled = (items = []) => items.filter((item) => item.enabled);
const disabled = (items = []) => items.filter((item) => !item.enabled);
const quoteKey = (key) => {
return key?.includes(":") ? ('"' + key + '"') : key;
}

// remove the last line if two new lines are found
const stripLastLine = (text) => {
Expand Down Expand Up @@ -71,15 +74,15 @@ const jsonToBru = (json) => {
if (enabled(queryParams).length) {
bru += `\n${indentString(
enabled(queryParams)
.map((item) => `${item.name}: ${item.value}`)
.map((item) => `${quoteKey(item.name)}: ${item.value}`)
.join('\n')
)}`;
}

if (disabled(queryParams).length) {
bru += `\n${indentString(
disabled(queryParams)
.map((item) => `~${item.name}: ${item.value}`)
.map((item) => `~${quoteKey(item.name)}: ${item.value}`)
.join('\n')
)}`;
}
Expand Down
1 change: 1 addition & 0 deletions packages/bruno-lang/v2/tests/fixtures/request.bru
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ get {
params:query {
apiKey: secret
numbers: 998877665
"colon:parameter": is:allowed
~message: hello
}

Expand Down
6 changes: 6 additions & 0 deletions packages/bruno-lang/v2/tests/fixtures/request.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
"type": "query",
"enabled": true
},
{
"name" : "colon:parameter",
"value" : "is:allowed",
"type": "query",
"enabled": true
},
{
"name": "message",
"value": "hello",
Expand Down

0 comments on commit 9c718da

Please sign in to comment.