[BUG] [C++][Pistache] cpp-pistache-server response 400 when request joson field was missing
Created by: haibinzero
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
What's the version of OpenAPI Generator used? -
Have you search for related issues/PRs? -
What's the actual output vs expected output? - [] [Optional] Bounty to sponsor the fix (example)
Description
The response for missing manadatory request body is 500 (internal server error), but what we wanted is 400 (bad request)
openapi-generator version
4.0.0-SNAPSHOT
OpenAPI declaration file content or url
swagger: '2.0'
info:
title: REST API
version: v1
basePath: /api/hardware/v1
schemes:
- http
paths:
/vnf/{vnfId}/vm/{vmId}/interface:
parameters:
- in: path
name: vnfId
description: VNF instance Id
required: true
type: string
- in: path
name: vmId
description: VM instance Id
required: true
type: string
post:
summary: Create interface instance under specified VM
description: Create interface
consumes:
- application/json
produces:
- application/json
parameters:
- in: body
name: interface
description: json containing interface configuration
schema:
$ref: "#/definitions/Interface"
responses:
'200':
description: OK
schema:
properties:
link:
$ref: "#/definitions/Link"
examples:
application/json:
{
"link": {
"rel": "self",
"href": "/vnf/1/vm/1/interface/1"
}
}
'400':
description: Bad request
'404':
description: Not found
default:
description: Unexpected error
definitions:
Link:
title: HATEOAS link
properties:
rel:
description: relation with the link
type: string
example: "self"
href:
description: URIs relative to base URI
type: string
example: "/vnf/1/vm/100/interface/1"
required:
- href
Interface:
title: Interface.
required:
- ipAddress
- name
- type
properties:
ipAddress:
type: string
example: 169.254.0.4
name:
type: string
enum:
- eth0
- eth1
example: eth0
type:
type: string
enum:
- physical
- logical
example: physical
Command line used for generation
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \
-i swagger.yaml \
-g cpp-pistache-server \
-o cpp
Steps to reproduce
[root@kongfu ~]# curl -i -d '{"ipAddress": "192.168.1.1","name": "eth0","type": "logical"}' -H "Content-Type: application/json" -X POST http://localhost:8080/api/hardware/v1/vnf/1/vm/1/interface
HTTP/1.1 200 OK
Connection: Close
Content-Length: 14
Do some magic
[root@kongfu ~]# curl -i -d '{"ipAddress": "192.168.1.1","name": "eth0"}' -H "Content-Type: application/json" -X POST http://localhost:8080/api/hardware/v1/vnf/1/vm/1/interface
HTTP/1.1 500 Internal Server Error
Content-Length: 54
[json.exception.out_of_range.403] key 'type' not found[root@kongfu ~]# curl -i -d '{"ipAddress": "192.168.1.1","name": "eth0"}' -H "Content-Type: application/json" /1/interface://localhost:8080/api/hardware/v1/vnf/1/vm/
HTTP/1.1 500 Internal Server Error
Content-Length: 54
Related issues/PRs
Suggest a fix
Response 400 (Bad request) when the request body missing mandatoy fields.
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::parse(request.body()).get_to(interface);
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;
}
}
Changes to
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::parse(request.body()).get_to(interface);
} catch (nlohmann::detail::exception & e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
}
// the std::runtime_error was caught outside and responsed 500
this->vnf_vnf_id_vm_vm_id_interface_post(vnfId, vmId, interface, response);
}