Skip to content

Commit

Permalink
[Bash] Allow non-JSON request body payloads (#17641)
Browse files Browse the repository at this point in the history
* Added option to mustache template that allows passing of non JSON request bodies

* Used linux terminal to run the generator scripts

---------

Co-authored-by: mHejlesen <[email protected]>
  • Loading branch information
mHejlesen and mHejlesen committed Jan 22, 2024
1 parent e6161cc commit 256b279
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 26 deletions.
40 changes: 27 additions & 13 deletions modules/openapi-generator/src/main/resources/bash/client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -330,20 +330,24 @@ header_arguments_to_curl() {
#
##############################################################################
body_parameters_to_json() {
local body_json="-d '{"
local count=0
for key in "${!body_parameters[@]}"; do
if [[ $((count++)) -gt 0 ]]; then
body_json+=", "
fi
body_json+="\"${key}\": ${body_parameters[${key}]}"
done
body_json+="}'"

if [[ "${#body_parameters[@]}" -eq 0 ]]; then
echo ""
if [[ $RAW_BODY == "1" ]]; then
echo "-d '${body_parameters["RAW_BODY"]}'"
else
echo "${body_json}"
local body_json="-d '{"
local count=0
for key in "${!body_parameters[@]}"; do
if [[ $((count++)) -gt 0 ]]; then
body_json+=", "
fi
body_json+="\"${key}\": ${body_parameters[${key}]}"
done
body_json+="}'"

if [[ "${#body_parameters[@]}" -eq 0 ]]; then
echo ""
else
echo "${body_json}"
fi
fi
}

Expand Down Expand Up @@ -1002,6 +1006,16 @@ case $key in
body_parameters[${body_key}]="\"${body_value}\""
fi
;;
--body=*)
# Parse value of body as argument and convert it into only
# the raw body content
if [[ "$operation" ]]; then
IFS='--body=' read -r body_value <<< "$key"
body_value=${body_value##--body=}
body_parameters["RAW_BODY"]="${body_value}"
RAW_BODY=1
fi
;;
*:=*)
# Parse body arguments and convert them into top level
# JSON properties passed in the body content without quotes
Expand Down
40 changes: 27 additions & 13 deletions samples/client/petstore/bash/petstore-cli
Original file line number Diff line number Diff line change
Expand Up @@ -492,20 +492,24 @@ header_arguments_to_curl() {
#
##############################################################################
body_parameters_to_json() {
local body_json="-d '{"
local count=0
for key in "${!body_parameters[@]}"; do
if [[ $((count++)) -gt 0 ]]; then
body_json+=", "
fi
body_json+="\"${key}\": ${body_parameters[${key}]}"
done
body_json+="}'"

if [[ "${#body_parameters[@]}" -eq 0 ]]; then
echo ""
if [[ $RAW_BODY == "1" ]]; then
echo "-d '${body_parameters["RAW_BODY"]}'"
else
echo "${body_json}"
local body_json="-d '{"
local count=0
for key in "${!body_parameters[@]}"; do
if [[ $((count++)) -gt 0 ]]; then
body_json+=", "
fi
body_json+="\"${key}\": ${body_parameters[${key}]}"
done
body_json+="}'"

if [[ "${#body_parameters[@]}" -eq 0 ]]; then
echo ""
else
echo "${body_json}"
fi
fi
}

Expand Down Expand Up @@ -3843,6 +3847,16 @@ case $key in
body_parameters[${body_key}]="\"${body_value}\""
fi
;;
--body=*)
# Parse value of body as argument and convert it into only
# the raw body content
if [[ "$operation" ]]; then
IFS='--body=' read -r body_value <<< "$key"
body_value=${body_value##--body=}
body_parameters["RAW_BODY"]="${body_value}"
RAW_BODY=1
fi
;;
*:=*)
# Parse body arguments and convert them into top level
# JSON properties passed in the body content without quotes
Expand Down

0 comments on commit 256b279

Please sign in to comment.