Skip to content

Commit

Permalink
Add helpers for creating empty message-body responses
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyGuinn committed Nov 26, 2021
1 parent 349d961 commit ecb8ec3
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/oatpp/web/protocol/http/outgoing/ResponseFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@
#include "./BufferBody.hpp"

namespace oatpp { namespace web { namespace protocol { namespace http { namespace outgoing {


std::shared_ptr<Response>
ResponseFactory::createResponse(const Status& status) {
return Response::createShared(status, nullptr);
}

std::shared_ptr<Response>
ResponseFactory::createResponse(const Status& status, const oatpp::String& text) {
return Response::createShared(status, BufferBody::createShared(text));
Expand Down
7 changes: 7 additions & 0 deletions src/oatpp/web/protocol/http/outgoing/ResponseFactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ namespace oatpp { namespace web { namespace protocol { namespace http { namespac
*/
class ResponseFactory {
public:

/**
* Create &id:oatpp::web::protocol::http::outgoing::Response; without a &id:oatpp::web::protocol::http::outgoing::Body;.
* @param status - &id:oatpp::web::protocol::http::Status;.
* @return - `std::shared_ptr` to &id:oatpp::web::protocol::http::outgoing::Response;.
*/
static std::shared_ptr<Response> createResponse(const Status &status);

/**
* Create &id:oatpp::web::protocol::http::outgoing::Response; with &id:oatpp::web::protocol::http::outgoing::BufferBody;.
Expand Down
4 changes: 4 additions & 0 deletions src/oatpp/web/server/api/ApiController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ std::shared_ptr<ApiController::OutgoingResponse> ApiController::createResponse(c
return ResponseFactory::createResponse(status, str);
}

std::shared_ptr<ApiController::OutgoingResponse> ApiController::createResponse(const ApiController::Status &status) const {
return ResponseFactory::createResponse(status);
}

std::shared_ptr<ApiController::OutgoingResponse> ApiController::createDtoResponse(const Status& status,
const oatpp::Void& dto,
const std::shared_ptr<oatpp::data::mapping::ObjectMapper>& objectMapper) const {
Expand Down
2 changes: 2 additions & 0 deletions src/oatpp/web/server/api/ApiController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,8 @@ class ApiController : public oatpp::base::Countable {
std::shared_ptr<OutgoingResponse> createResponse(const Status& status,
const oatpp::String& str) const;

std::shared_ptr<OutgoingResponse> createResponse(const Status& status) const;

std::shared_ptr<OutgoingResponse> createDtoResponse(const Status& status,
const oatpp::Void& dto,
const std::shared_ptr<oatpp::data::mapping::ObjectMapper>& objectMapper) const;
Expand Down
11 changes: 11 additions & 0 deletions src/oatpp/web/server/api/Endpoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,17 @@ class Endpoint : public oatpp::base::Countable {
return hint;
}

/**
* Add response info with no message-body to endpoint
* @param status
* @param responseDescription
*/
ContentHints& addResponse(const oatpp::web::protocol::http::Status& status, const oatpp::String& responseDescription = oatpp::String()) {
auto& hint = responses[status];
hint.description = responseDescription.get() == nullptr ? status.description : responseDescription;
return hint;
}

/**
* Add security requirement.
* @param requirement
Expand Down
28 changes: 28 additions & 0 deletions test/oatpp/web/server/api/ApiControllerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ class Controller : public oatpp::web::server::api::ApiController {
return createResponse(Status::CODE_200, "test3");
}

ENDPOINT_INFO(noContent) {
info->addResponse(Status::CODE_204, "No Content");
}
ENDPOINT("GET", "noContent", noContent) {
return createResponse(Status::CODE_204);
}

#include OATPP_CODEGEN_END(ApiController)

};
Expand Down Expand Up @@ -162,6 +169,27 @@ void ApiControllerTest::onRun() {

}

{
auto endpoint = controller.Z__ENDPOINT_noContent;
OATPP_ASSERT(endpoint);
OATPP_ASSERT(!endpoint->info()->summary);

auto r204 = endpoint->info()->responses[Status::CODE_204];
OATPP_ASSERT(!r204.contentType);
OATPP_ASSERT(!r204.schema);
OATPP_ASSERT(r204.description == "No Content");

auto response = controller.noContent();
OATPP_ASSERT(response->getStatus().code == 204);
OATPP_ASSERT(!response->getBody());

oatpp::data::stream::BufferOutputStream stream;
response->send(&stream, &headersOutBuffer, nullptr);

OATPP_LOGD(TAG, "response:\n---\n%s\n---\n", stream.toString()->c_str());

}

}

}}}}}

0 comments on commit ecb8ec3

Please sign in to comment.