diff --git a/modules/swagger-codegen/src/main/resources/bash/client.mustache b/modules/swagger-codegen/src/main/resources/bash/client.mustache index d351fbd213d..0922d286b83 100644 --- a/modules/swagger-codegen/src/main/resources/bash/client.mustache +++ b/modules/swagger-codegen/src/main/resources/bash/client.mustache @@ -51,7 +51,7 @@ fi ## # The filename of this script for help messages -script_name=`basename "$0"` +script_name=$(basename "$0") ## # Map for headers passed after operation as KEY:VALUE @@ -162,9 +162,17 @@ host="{{#x-codegen-host-env}}${{x-codegen-host-env}}{{/x-codegen-host-env}}" # The user credentials for basic authentication basic_auth_credential="{{#x-codegen-basicauth-env}}${{x-codegen-basicauth-env}}{{/x-codegen-basicauth-env}}" +{{#hasAuthMethods}} +{{#authMethods}} +{{#isApiKey}} +{{#isKeyInHeader}} ## # The user API key apikey_auth_credential="{{#x-codegen-apikey-env}}${{x-codegen-apikey-env}}{{/x-codegen-apikey-env}}" +{{/isKeyInHeader}} +{{/isApiKey}} +{{/authMethods}} +{{/hasAuthMethods}} ## # If true, the script will only output the actual cURL command that would be @@ -238,9 +246,9 @@ url_escape() { -e 's/)/%29/g' \ -e 's/:/%3A/g' \ -e 's/\t/%09/g' \ - -e 's/?/%3F/g' <<<$raw_url); + -e 's/?/%3F/g' <<<"$raw_url"); - echo $value + echo "$value" } ############################################################################## @@ -250,12 +258,12 @@ url_escape() { # ############################################################################## lookup_mime_type() { - local mime_type=$1 + local mime_type="$1" if [[ ${mime_type_abbreviations[$mime_type]} ]]; then - echo ${mime_type_abbreviations[$mime_type]} + echo "${mime_type_abbreviations[$mime_type]}" else - echo $1 + echo "$mime_type" fi } @@ -267,12 +275,12 @@ lookup_mime_type() { ############################################################################## header_arguments_to_curl() { local headers_curl="" - local api_key_header="" - local api_key_header_in_cli="" {{#hasAuthMethods}} {{#authMethods}} {{#isApiKey}} {{#isKeyInHeader}} + local api_key_header="" + local api_key_header_in_cli="" api_key_header="{{keyParamName}}" {{/isKeyInHeader}} {{/isApiKey}} @@ -281,9 +289,17 @@ header_arguments_to_curl() { for key in "${!header_arguments[@]}"; do headers_curl+="-H \"${key}: ${header_arguments[${key}]}\" " +{{#hasAuthMethods}} +{{#authMethods}} +{{#isApiKey}} +{{#isKeyInHeader}} if [[ "${key}XX" == "${api_key_header}XX" ]]; then api_key_header_in_cli="YES" fi +{{/isKeyInHeader}} +{{/isApiKey}} +{{/authMethods}} +{{/hasAuthMethods}} done {{#hasAuthMethods}} {{#authMethods}} @@ -315,7 +331,6 @@ header_arguments_to_curl() { ############################################################################## body_parameters_to_json() { local body_json="-d '{" - local body_parameter_count=${#body_parameters[@]} local count=0 for key in "${!body_parameters[@]}"; do if [[ $((count++)) -gt 0 ]]; then @@ -366,7 +381,8 @@ build_request_path() { if [[ "$force" = false ]]; then local was_error="" for qparam in "${query_params[@]}" "${path_params[@]}"; do - local parameter_values=($(sed -e 's/'":::"'/\n/g' <<<"${operation_parameters[$qparam]}")) + local parameter_values + mapfile -t parameter_values < <(sed -e 's/'":::"'/\n/g' <<<"${operation_parameters[$qparam]}") # # Check if the number of provided values is not less than minimum required @@ -391,29 +407,30 @@ build_request_path() { fi # First replace all path parameters in the path + local path_regex="(.*)(\\{$pparam\\})(.*)" for pparam in "${path_params[@]}"; do - if [[ $path_template =~ (.*)(\{$pparam\})(.*) ]]; then + if [[ $path_template =~ $path_regex ]]; then path_template=${BASH_REMATCH[1]}${operation_parameters[$pparam]}${BASH_REMATCH[3]} fi done local query_request_part="" - local query_parameter_count=${#query_params[@]} local count=0 for qparam in "${query_params[@]}"; do # Get the array of parameter values - local parameter_values=($(sed -e 's/'":::"'/\n/g' <<<"${operation_parameters[$qparam]}")) local parameter_value="" + local parameter_values + mapfile -t parameter_values < <(sed -e 's/'":::"'/\n/g' <<<"${operation_parameters[$qparam]}") - if [[ -n "${parameter_values[@]}" ]]; then + if [[ -n "${parameter_values[*]}" ]]; then if [[ $((count++)) -gt 0 ]]; then query_request_part+="&" fi fi {{#hasAuthMethods}}{{#authMethods}}{{#isApiKey}}{{#isKeyInQuery}} if [[ ${qparam} == "{{keyParamName}}" ]]; then - if [[ -n "${parameter_values[@]}" ]]; then + if [[ -n "${parameter_values[*]}" ]]; then parameter_value+="${qparam}=${parameter_values}" {{#x-codegen-apikey-env}} elif [[ -n "$MATRIX_API_KEY" ]]; then @@ -481,7 +498,7 @@ build_request_path() { local vcount=0 for qvalue in "${parameter_values[@]}"; do if [[ $((vcount++)) -gt 0 ]]; then - parameter_value+="\t" + parameter_value+="\\t" fi parameter_value+="${qvalue}" done @@ -502,7 +519,7 @@ build_request_path() { path_template+="?${query_request_part}" fi - echo $path_template + echo "$path_template" } @@ -576,7 +593,7 @@ EOF {{#apis}} echo "" echo -e "${BOLD}${WHITE}[{{classVarName}}]${OFF}" -read -d '' ops <${OFF}\t\t\t\tSpecify the host URL " + echo -e " -h,--help\\t\\t\\t\\tPrint this help" + echo -e " -V,--version\\t\\t\\t\\tPrint API version" + echo -e " --about\\t\\t\\t\\tPrint the information about service" + echo -e " --host ${CYAN}${OFF}\\t\\t\\t\\tSpecify the host URL " {{#swagger}} -{{#host}}echo -e " \t\t\t\t(e.g. 'https://{{host}}')"{{/host}} -{{^host}}echo -e " \t\t\t\t(e.g. 'https://127.0.0.1:8080')"{{/host}} +{{#host}}echo -e " \\t\\t\\t\\t(e.g. 'https://{{host}}')"{{/host}} +{{^host}}echo -e " \\t\\t\\t\\t(e.g. 'https://127.0.0.1:8080')"{{/host}} {{/swagger}} - echo -e " --force\t\t\t\tForce command invocation in spite of missing" - echo -e " \t\t\t\trequired parameters or wrong content type" - echo -e " --dry-run\t\t\t\tPrint out the cURL command without" - echo -e " \t\t\t\texecuting it" - echo -e " -nc,--no-colors\t\t\tEnforce print without colors, otherwise autodected" - echo -e " -ac,--accept ${YELLOW}${OFF}\t\tSet the 'Accept' header in the request" - echo -e " -ct,--content-type ${YELLOW}${OFF}\tSet the 'Content-type' header in " - echo -e " \tthe request" + echo -e " --force\\t\\t\\t\\tForce command invocation in spite of missing" + echo -e " \\t\\t\\t\\trequired parameters or wrong content type" + echo -e " --dry-run\\t\\t\\t\\tPrint out the cURL command without" + echo -e " \\t\\t\\t\\texecuting it" + echo -e " -nc,--no-colors\\t\\t\\tEnforce print without colors, otherwise autodected" + echo -e " -ac,--accept ${YELLOW}${OFF}\\t\\tSet the 'Accept' header in the request" + echo -e " -ct,--content-type ${YELLOW}${OFF}\\tSet the 'Content-type' header in " + echo -e " \\tthe request" echo "" } @@ -620,7 +637,7 @@ print_about() { echo -e "License: {{#swagger}}{{#info}}{{#license}}{{name}}{{/license}}{{/info}}{{/swagger}}" echo -e "Contact: {{#swagger}}{{#info}}{{#contact}}{{email}}{{/contact}}{{/info}}{{/swagger}}" echo "" -read -d '' appdescription < $body_content_temp_file + cat - > "$body_content_temp_file" ;; *=*) # Parse operation arguments and convert them into curl # only after the operation argument if [[ "$operation" ]]; then - IFS='=' read parameter_name parameter_value <<< "$key" + IFS='=' read -r parameter_name parameter_value <<< "$key" if [[ -z "${operation_parameters[$parameter_name]+foo}" ]]; then operation_parameters[$parameter_name]=$(url_escape "${parameter_value}") else diff --git a/samples/client/petstore/bash/petstore-cli b/samples/client/petstore/bash/petstore-cli index d3e6720e1fc..04b0fd2f634 100755 --- a/samples/client/petstore/bash/petstore-cli +++ b/samples/client/petstore/bash/petstore-cli @@ -50,7 +50,7 @@ fi ## # The filename of this script for help messages -script_name=`basename "$0"` +script_name=$(basename "$0") ## # Map for headers passed after operation as KEY:VALUE @@ -372,9 +372,9 @@ url_escape() { -e 's/)/%29/g' \ -e 's/:/%3A/g' \ -e 's/\t/%09/g' \ - -e 's/?/%3F/g' <<<$raw_url); + -e 's/?/%3F/g' <<<"$raw_url"); - echo $value + echo "$value" } ############################################################################## @@ -384,12 +384,12 @@ url_escape() { # ############################################################################## lookup_mime_type() { - local mime_type=$1 + local mime_type="$1" if [[ ${mime_type_abbreviations[$mime_type]} ]]; then - echo ${mime_type_abbreviations[$mime_type]} + echo "${mime_type_abbreviations[$mime_type]}" else - echo $1 + echo "$mime_type" fi } @@ -433,7 +433,6 @@ header_arguments_to_curl() { ############################################################################## body_parameters_to_json() { local body_json="-d '{" - local body_parameter_count=${#body_parameters[@]} local count=0 for key in "${!body_parameters[@]}"; do if [[ $((count++)) -gt 0 ]]; then @@ -484,7 +483,8 @@ build_request_path() { if [[ "$force" = false ]]; then local was_error="" for qparam in "${query_params[@]}" "${path_params[@]}"; do - local parameter_values=($(sed -e 's/'":::"'/\n/g' <<<"${operation_parameters[$qparam]}")) + local parameter_values + mapfile -t parameter_values < <(sed -e 's/'":::"'/\n/g' <<<"${operation_parameters[$qparam]}") # # Check if the number of provided values is not less than minimum required @@ -509,29 +509,30 @@ build_request_path() { fi # First replace all path parameters in the path + local path_regex="(.*)(\\{$pparam\\})(.*)" for pparam in "${path_params[@]}"; do - if [[ $path_template =~ (.*)(\{$pparam\})(.*) ]]; then + if [[ $path_template =~ $path_regex ]]; then path_template=${BASH_REMATCH[1]}${operation_parameters[$pparam]}${BASH_REMATCH[3]} fi done local query_request_part="" - local query_parameter_count=${#query_params[@]} local count=0 for qparam in "${query_params[@]}"; do # Get the array of parameter values - local parameter_values=($(sed -e 's/'":::"'/\n/g' <<<"${operation_parameters[$qparam]}")) local parameter_value="" + local parameter_values + mapfile -t parameter_values < <(sed -e 's/'":::"'/\n/g' <<<"${operation_parameters[$qparam]}") - if [[ -n "${parameter_values[@]}" ]]; then + if [[ -n "${parameter_values[*]}" ]]; then if [[ $((count++)) -gt 0 ]]; then query_request_part+="&" fi fi if [[ ${qparam} == "api_key_query" ]]; then - if [[ -n "${parameter_values[@]}" ]]; then + if [[ -n "${parameter_values[*]}" ]]; then parameter_value+="${qparam}=${parameter_values}" elif [[ -n "$MATRIX_API_KEY" ]]; then parameter_value+="${qparam}=$PETSTORE_API_KEY" @@ -597,7 +598,7 @@ build_request_path() { local vcount=0 for qvalue in "${parameter_values[@]}"; do if [[ $((vcount++)) -gt 0 ]]; then - parameter_value+="\t" + parameter_value+="\\t" fi parameter_value+="${qvalue}" done @@ -618,7 +619,7 @@ build_request_path() { path_template+="?${query_request_part}" fi - echo $path_template + echo "$path_template" } @@ -676,13 +677,13 @@ EOF echo -e "${BOLD}${WHITE}Operations (grouped by tags)${OFF}" echo "" echo -e "${BOLD}${WHITE}[anotherFake]${OFF}" -read -d '' ops <${OFF}\t\t\t\tSpecify the host URL " -echo -e " \t\t\t\t(e.g. 'https://petstore.swagger.io:80')" + echo -e " -h,--help\\t\\t\\t\\tPrint this help" + echo -e " -V,--version\\t\\t\\t\\tPrint API version" + echo -e " --about\\t\\t\\t\\tPrint the information about service" + echo -e " --host ${CYAN}${OFF}\\t\\t\\t\\tSpecify the host URL " +echo -e " \\t\\t\\t\\t(e.g. 'https://petstore.swagger.io:80')" - echo -e " --force\t\t\t\tForce command invocation in spite of missing" - echo -e " \t\t\t\trequired parameters or wrong content type" - echo -e " --dry-run\t\t\t\tPrint out the cURL command without" - echo -e " \t\t\t\texecuting it" - echo -e " -nc,--no-colors\t\t\tEnforce print without colors, otherwise autodected" - echo -e " -ac,--accept ${YELLOW}${OFF}\t\tSet the 'Accept' header in the request" - echo -e " -ct,--content-type ${YELLOW}${OFF}\tSet the 'Content-type' header in " - echo -e " \tthe request" + echo -e " --force\\t\\t\\t\\tForce command invocation in spite of missing" + echo -e " \\t\\t\\t\\trequired parameters or wrong content type" + echo -e " --dry-run\\t\\t\\t\\tPrint out the cURL command without" + echo -e " \\t\\t\\t\\texecuting it" + echo -e " -nc,--no-colors\\t\\t\\tEnforce print without colors, otherwise autodected" + echo -e " -ac,--accept ${YELLOW}${OFF}\\t\\tSet the 'Accept' header in the request" + echo -e " -ct,--content-type ${YELLOW}${OFF}\\tSet the 'Content-type' header in " + echo -e " \\tthe request" echo "" } @@ -769,7 +770,7 @@ print_about() { echo -e "License: Apache 2.0" echo -e "Contact: apiteam@swagger.io" echo "" -read -d '' appdescription < $body_content_temp_file + cat - > "$body_content_temp_file" ;; *=*) # Parse operation arguments and convert them into curl # only after the operation argument if [[ "$operation" ]]; then - IFS='=' read parameter_name parameter_value <<< "$key" + IFS='=' read -r parameter_name parameter_value <<< "$key" if [[ -z "${operation_parameters[$parameter_name]+foo}" ]]; then operation_parameters[$parameter_name]=$(url_escape "${parameter_value}") else