swagger-api / swagger-api/swagger-codegen

[C++][Pistache-server] Unable to return 400 Error when request Json body parsing fail or request paramters error

Open
#9,396 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description
  • Define a request data model with mandatory fields then request the API with missing the mandatory field. The expected response is 400 Bad request, but returns 500 internal error.
  • The same issue with the required parameters in the request URI.

The generated server code in "DefaultApi.h"

  1. without check request paramters
  2. the std::runtime_error catch maybe means to check the request body errors. but the nlohmann json parser will throw std::exception but not std::runting_error. If the request body don't align with the parameter definitions in the yaml file, it will response 500 instead of 400.
void DefaultApi::vnf_vnf_id_vm_vm_id_interface_post_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
    // Getting the path params
    auto vnfId = request.param(":vnfId").as<std::string>();
    auto vmId = request.param(":vmId").as<std::string>();

    // Getting the body param
    Interface interface;

    try {
      nlohmann::json request_body = nlohmann::json::parse(request.body());
      interface.fromJson(request_body);
      this->vnf_vnf_id_vm_vm_id_interface_post(vnfId, vmId, interface, response);
    } catch (std::runtime_error & e) {
      //send a 400 error
      response.send(Pistache::Http::Code::Bad_Request, e.what());
      return;
    }

}
Swagger-codegen version

2.4.5

Swagger declaration file content or url

yaml file from the link
https://gist.github.com/haibinzero/ccca4fb23c30f13d601cb987b1139526

Command line used for generation
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
-i wagger.yaml \
-l pistache-server \
-o auto_code/cpp
Suggest a fix/enhancement

Try to catch the request errors includes the request parameter and request body.
Both nlohmann ::detail::exception and std::runtime_error should be checked.

This is the simple way to catch, but it have some limits.
The requst parameter check can't return the missing parameter name.
Maybe other std::runtime_error which really was internal error but reports 400.
Can you help to inmplement it in api_source.mustache?

void DefaultApi::vnf_vnf_id_vm_vm_id_interface_post_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
    try
    {
        // Getting the path params
        auto vnfId = request.param(":vnfId").as<std::string>();
        auto vmId = request.param(":vmId").as<std::string>();

        // Getting the body param
        Interface interface;

        nlohmann::json request_body = nlohmann::json::parse(request.body());
        interface.fromJson(request_body);
        this->vnf_vnf_id_vm_vm_id_interface_post(vnfId, vmId, interface, response);
    } catch (std::runtime_error & e) {
        //send a 400 error
        response.send(Pistache::Http::Code::Bad_Request, e.what());
        return;
    } catch (nlohmann::detail::exception & e) {
        //send a 400 error
        response.send(Pistache::Http::Code::Bad_Request, e.what());
        return;
    }
}

https://gist.github.com/haibinzero/8534e174a5f46bd637026f9c38ce00a3

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 the C++ server template api_source.mustache and compare it with the generated DefaultApi.h shown in the issue. Reproduce the malformed JSON and missing path or request parameters using the provided YAML and generation command. Done means generated handlers return HTTP 400 for request validation and parsing failures instead of 500.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.