From 150b469fbc6195bd40f41b28b05130aeb630a99a Mon Sep 17 00:00:00 2001 From: Jeremy Guinn Date: Fri, 26 Nov 2021 15:11:50 -0700 Subject: [PATCH] Add support for responses with no message-body --- README.md | 1 + src/oatpp-swagger/Generator.cpp | 25 ++++++++++--------- .../test-controllers/TestController.hpp | 1 + 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 6bb2015..d4169d2 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ ENDPOINT_INFO(createUser) { info->summary = "Create new User"; info->addConsumes("application/json"); info->addResponse(Status::CODE_200, "application/json"); + info->addResponse(Status::CODE_500); } ENDPOINT("POST", "demo/api/users", createUser, BODY_DTO(UserDto, userDto)) { diff --git a/src/oatpp-swagger/Generator.cpp b/src/oatpp-swagger/Generator.cpp index a16cf76..f117d86 100644 --- a/src/oatpp-swagger/Generator.cpp +++ b/src/oatpp-swagger/Generator.cpp @@ -340,30 +340,31 @@ oatpp::Fields> Generator::generateResponses(cons for(auto& hint : endpointInfo.responses) { - auto mediaType = oas3::MediaTypeObject::createShared(); - mediaType->schema = generateSchemaForType(hint.second.schema, linkSchema, usedTypes); + auto response = oas3::OperationResponse::createShared(); + + if(hint.second.schema != nullptr) { + + auto mediaType = oas3::MediaTypeObject::createShared(); + mediaType->schema = generateSchemaForType(hint.second.schema, linkSchema, usedTypes); + + for(auto& ex : hint.second.examples) { + mediaType->addExample(ex.first, ex.second); + } + + response->content = {}; + response->content[hint.second.contentType] = mediaType; - for(auto& ex : hint.second.examples) { - mediaType->addExample(ex.first, ex.second); } - auto response = oas3::OperationResponse::createShared(); response->description = hint.second.description.get() == nullptr ? hint.first.description : hint.second.description; - response->content = {}; - response->content[hint.second.contentType] = mediaType; responses[oatpp::utils::conversion::int32ToStr(hint.first.code)] = response; } } else { - auto mediaType = oas3::MediaTypeObject::createShared(); - mediaType->schema = generateSchemaForType(oatpp::String::Class::getType(), linkSchema, usedTypes); - auto response = oas3::OperationResponse::createShared(); response->description = "success"; - response->content = {}; - response->content["text/plain"] = mediaType; responses["200"] = response; } diff --git a/test/oatpp-swagger/test-controllers/TestController.hpp b/test/oatpp-swagger/test-controllers/TestController.hpp index a37c2f3..33ee1aa 100644 --- a/test/oatpp-swagger/test-controllers/TestController.hpp +++ b/test/oatpp-swagger/test-controllers/TestController.hpp @@ -252,6 +252,7 @@ public: info->summary = "Delete User by userId"; info->addResponse(Status::CODE_200, "text/plain"); info->addResponse(Status::CODE_404, "text/plain"); + info->addResponse(Status::CODE_500); } ENDPOINT("DELETE", "demo/api/users/{userId}", deleteUser, PATH(Int32, userId)) {