Skip to content

Commit

Permalink
Bugfix: Resolve lint errors for generated client (#6563)
Browse files Browse the repository at this point in the history
Updates the bash client template to allow the generated client code
to be free of lint errors.

Resolves: #6562
  • Loading branch information
kenjones-cisco authored and wing328 committed Sep 26, 2017
1 parent 451218b commit 5e9d977
Show file tree
Hide file tree
Showing 2 changed files with 326 additions and 180 deletions.
109 changes: 66 additions & 43 deletions modules/swagger-codegen/src/main/resources/bash/client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
}

##############################################################################
Expand All @@ -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
}

Expand All @@ -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}}
Expand All @@ -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}}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -502,7 +519,7 @@ build_request_path() {
path_template+="?${query_request_part}"
fi

echo $path_template
echo "$path_template"
}


Expand Down Expand Up @@ -576,7 +593,7 @@ EOF
{{#apis}}
echo ""
echo -e "${BOLD}${WHITE}[{{classVarName}}]${OFF}"
read -d '' ops <<EOF
read -r -d '' ops <<EOF
{{#operations}}
{{#operation}}
${CYAN}{{operationId}}${OFF};{{{summary}}}{{#authMethods}} (AUTH){{/authMethods}}
Expand All @@ -588,22 +605,22 @@ echo " $ops" | column -t -s ';'
{{/apiInfo}}
echo ""
echo -e "${BOLD}${WHITE}Options${OFF}"
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}<url>${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}<url>${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}<mime-type>${OFF}\t\tSet the 'Accept' header in the request"
echo -e " -ct,--content-type ${YELLOW}<mime-type>${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}<mime-type>${OFF}\\t\\tSet the 'Accept' header in the request"
echo -e " -ct,--content-type ${YELLOW}<mime-type>${OFF}\\tSet the 'Content-type' header in "
echo -e " \\tthe request"
echo ""
}

Expand All @@ -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 <<EOF
read -r -d '' appdescription <<EOF
{{#x-bash-codegen-app-description}}{{{x-bash-codegen-app-description}}}{{/x-bash-codegen-app-description}}
{{^x-bash-codegen-app-description}}{{{appDescription}}}{{/x-bash-codegen-app-description}}
EOF
Expand Down Expand Up @@ -678,7 +695,7 @@ print_{{operationId}}_help() {
echo -e " * ${GREEN}{{baseName}}${OFF} ${BLUE}[{{dataType}}]${OFF}{{#required}} ${RED}(required)${OFF}{{/required}}{{#defaultValue}} ${CYAN}(default: {{defaultValue}}){{/defaultValue}}${OFF} - {{{description}}} ${YELLOW}Specify as: {{baseName}}=value${OFF}" | paste -sd' ' | fold -sw 80 | sed '2,$s/^/ /'
{{/isPathParam}}
{{#isQueryParam}}
echo -e " * ${GREEN}{{baseName}}${OFF} ${BLUE}[{{dataType}}]${OFF}{{#required}} ${RED}(required)${OFF}{{/required}}{{#defaultValue}} ${CYAN}(default: {{defaultValue}}){{/defaultValue}}${OFF} - {{{description}}}${YELLOW}{{#isContainer}} Specify as: {{#vendorExtensions}}{{#x-codegen-collection-multi}}{{baseName}}=value1 {{baseName}}=value2 {{baseName}}=...{{/x-codegen-collection-multi}}{{#x-codegen-collection-csv}}{{baseName}}="value1,value2,..."{{/x-codegen-collection-csv}}{{#x-codegen-collection-pipes}}{{baseName}}="value1|value2|..."{{/x-codegen-collection-pipes}}{{#x-codegen-collection-ssv}}{{baseName}}="value1 value2 ..."{{/x-codegen-collection-ssv}}{{#x-codegen-collection-tsv}}{{baseName}}="value1\tvalue2\t..."{{/x-codegen-collection-tsv}}{{/vendorExtensions}}{{/isContainer}}{{^isContainer}} Specify as: {{baseName}}=value{{/isContainer}}${OFF}" \
echo -e " * ${GREEN}{{baseName}}${OFF} ${BLUE}[{{dataType}}]${OFF}{{#required}} ${RED}(required)${OFF}{{/required}}{{#defaultValue}} ${CYAN}(default: {{defaultValue}}){{/defaultValue}}${OFF} - {{{description}}}${YELLOW}{{#isContainer}} Specify as: {{#vendorExtensions}}{{#x-codegen-collection-multi}}{{baseName}}=value1 {{baseName}}=value2 {{baseName}}=...{{/x-codegen-collection-multi}}{{#x-codegen-collection-csv}}{{baseName}}="value1,value2,..."{{/x-codegen-collection-csv}}{{#x-codegen-collection-pipes}}{{baseName}}="value1|value2|..."{{/x-codegen-collection-pipes}}{{#x-codegen-collection-ssv}}{{baseName}}="value1 value2 ..."{{/x-codegen-collection-ssv}}{{#x-codegen-collection-tsv}}{{baseName}}="value1\\tvalue2\\t..."{{/x-codegen-collection-tsv}}{{/vendorExtensions}}{{/isContainer}}{{^isContainer}} Specify as: {{baseName}}=value{{/isContainer}}${OFF}" \
| paste -sd' ' | fold -sw 80 | sed '2,$s/^/ /'
{{/isQueryParam}}
{{#isHeaderParam}}
Expand Down Expand Up @@ -736,17 +753,21 @@ print_{{operationId}}_help() {
#
##############################################################################
call_{{operationId}}() {
# ignore error about 'path_parameter_names' being unused; passed by reference
# shellcheck disable=SC2034
local path_parameter_names=({{#pathParams}}{{baseName}}{{#hasMore}} {{/hasMore}}{{/pathParams}})
# ignore error about 'query_parameter_names' being unused; passed by reference
# shellcheck disable=SC2034
local query_parameter_names=({{#queryParams}}{{baseName}}{{#hasMore}} {{/hasMore}}{{/queryParams}}{{#authMethods}} {{#isApiKey}}{{#isKeyInQuery}}{{keyParamName}}{{/isKeyInQuery}}{{/isApiKey}} {{/authMethods}})
local path

path=$(build_request_path "{{basePathWithoutHost}}{{{path}}}" path_parameter_names query_parameter_names)
if [ $? -ne 0 ]; then
if ! path=$(build_request_path "{{basePathWithoutHost}}{{{path}}}" path_parameter_names query_parameter_names); then
ERROR_MSG=$path
exit 1
fi
local method="{{httpMethod}}"
local headers_curl=$(header_arguments_to_curl)
local headers_curl
headers_curl=$(header_arguments_to_curl)
if [[ -n $header_accept ]]; then
headers_curl="${headers_curl} -H 'Accept: ${header_accept}'"
fi
Expand Down Expand Up @@ -782,7 +803,7 @@ call_{{operationId}}() {
echo "ERROR: Request's content-type not specified!!!"
echo "This operation expects content-type in one of the following formats:"
{{#consumes}}
echo -e "\t- {{mediaType}}"
echo -e "\\t- {{mediaType}}"
{{/consumes}}
echo ""
echo "Use '--content-type' to set proper content type"
Expand Down Expand Up @@ -941,23 +962,25 @@ case $key in
# Parse body arguments and convert them into top level
# JSON properties passed in the body content as strings
if [[ "$operation" ]]; then
IFS='==' read body_key sep body_value <<< "$key"
IFS='==' read -r body_key sep body_value <<< "$key"
body_parameters[${body_key}]="\"${body_value}\""
fi
;;
*:=*)
# Parse body arguments and convert them into top level
# JSON properties passed in the body content without qoutes
if [[ "$operation" ]]; then
IFS=':=' read body_key sep body_value <<< "$key"
# ignore error about 'sep' being unused
# shellcheck disable=SC2034
IFS=':=' read -r body_key sep body_value <<< "$key"
body_parameters[${body_key}]=${body_value}
fi
;;
+([^=]):*)
# Parse header arguments and convert them into curl
# only after the operation argument
if [[ "$operation" ]]; then
IFS=':' read header_name header_value <<< "$key"
IFS=':' read -r header_name header_value <<< "$key"
{{#hasAuthMethods}}
{{#authMethods}}
{{#isApiKey}}
Expand All @@ -980,13 +1003,13 @@ case $key in
;;
-)
body_content_temp_file=$(mktemp)
cat - > $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
Expand Down
Loading

0 comments on commit 5e9d977

Please sign in to comment.