OpenAPITools / OpenAPITools/openapi-generator

[BUG] [BASH] Bash generator does not propagate curl exit code to clients

Open
#21,944 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
Description

Curl exit code is not propagated to cli callers when body content provided with - argument

I use the example code from BASH pet store where I specify --fail flag in curl and do the request that triggers 400 error on the server

./petstore-cli --host http://localhost:8081 --fail --content-type json addPet --body="{\"wrong\":\"hello\", \"notId\":\"1\"}"
curl: (22) The requested URL returned error: 400

I check exit code

echo $?
22

This is expected behaviour. But when I provide request body using here doc

./petstore-cli --host http://localhost:8081 --fail --content-type json addPet -<<JSON
{"wrong": "1", "notId":"name"}
JSON
curl: (22) The requested URL returned error: 400

The exit code will be success, despite the fact the request failed


echo $?
0

I expect the error code to be returned because the request failed

The root cause

The problem is client.mustache contains code that does not return curl exit code when used with body_content_temp_file

There is a place where curl exit code is ignored, which blocks users from relying on cli exit code

    if [[ -n $body_content_temp_file ]]; then
        if [[ "$print_curl" = true ]]; then
            echo "cat ${body_content_temp_file} | curl ${basic_auth_option} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\" -d @-"
        else
            eval "cat ${body_content_temp_file} | curl ${basic_auth_option} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\" -d @-"
        fi
        rm "${body_content_temp_file}"

in order to propagate the exit code we need to do something like that

retval=$?
rm "${body_content_temp_file}"
return $retval

It can be reproduced in pet store tests

  1. specify --fail parameter in curl
  2. Make a request that triggers error on the server side
  3. Use here doc for providing the body
  4. Expect exit code to be not 0
@test "addPet from parameters" {
    run --2 bash $PETSTORE_CLI --fail --content-type json addPet -<<JSON
{"wrong": "1", "notId":"name"}
JSON

Actual result exit code 0

Expected result: code should not be 22

openapi-generator version

7.15.0

OpenAPI declaration file content or url

https://github.com/OpenAPITools/openapi-generator/blob/6825d9ccaadea00471fc1a84fece37e50204cccb/samples/yaml/pet.yml

Steps to reproduce
  1. Provide request with incorrect body using here doc
  2. Expect CLI exit code not to be 0
Suggest a fix

add exit code

retval=$?
rm "${body_content_temp_file}"
return $retval

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in client.mustache at the body_content_temp_file branch, then inspect the petstore Bash test for the here-doc addPet request. Reproduce the --fail case against the petstore server and verify that the generated CLI returns curl's nonzero exit code while still removing the temporary body file.

Written by the indexing model from the issue text.

Assessment

Tech stack
bash
Domain
cli
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.