From 3c7b17e9833269026e7fb47c2846ec6127fafde2 Mon Sep 17 00:00:00 2001 From: Lennart Sauerbeck Date: Fri, 1 Sep 2017 16:16:50 +0200 Subject: [PATCH 1/2] Swagger: Implement `responses` object for endpoints For easier documentation each response code can now be documented by itself. For now the only supported field is `description` which is also the only required field as per the specification. Signed-off-by: Lennart Sauerbeck --- src/action.cpp | 5 +++++ src/action.h | 5 +++++ src/swagger.cpp | 11 ++++++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/action.cpp b/src/action.cpp index 7c7fb3cb..33b0a8b0 100644 --- a/src/action.cpp +++ b/src/action.cpp @@ -139,6 +139,11 @@ std::vector Action::getInputs() const return EMPTY_INPUTS; } +std::map Action::getResponses() const +{ + return {}; +} + std::vector Action::getHeaders() const { return Global::getDefaultHeaders(); diff --git a/src/action.h b/src/action.h index 7630c6c6..204b398a 100644 --- a/src/action.h +++ b/src/action.h @@ -1,6 +1,8 @@ #ifndef QTTPACTION_H #define QTTPACTION_H +#include + #include "qttp_global.h" #include "httproute.h" #include "httpdata.h" @@ -60,6 +62,9 @@ class QTTPSHARED_EXPORT Action //! The inputs help SwaggerUI include parameters. virtual std::vector getInputs() const; + //! The possible responses of this action - used for SwaggerUI. + virtual std::map getResponses() const; + bool registerRoute(HttpMethod method, const QString& path, Visibility visibility = Visibility::Show); bool registerRoute(const qttp::HttpPath& path, Visibility visibility = Visibility::Show); void registerRoute(const std::vector& routes, Visibility visibility = Visibility::Show); diff --git a/src/swagger.cpp b/src/swagger.cpp index f409577f..a2c6fcce 100644 --- a/src/swagger.cpp +++ b/src/swagger.cpp @@ -84,6 +84,14 @@ void Swagger::initialize() QJsonArray required; QJsonArray tags = QJsonArray::fromStringList(action->getTags()); + QJsonObject responses; + for(auto response : action->getResponses()) + { + responses.insert(QString::number(static_cast(response.first)), QJsonObject { + { "description", response.second } + }); + } + QString actionName = action->getName(); const vector & inputs = action->getInputs(); @@ -216,7 +224,8 @@ void Swagger::initialize() { "description", action->getDescription() }, { "operationId", routePath }, { "parameters", routeParameters }, - { "tags", tags } + { "tags", tags }, + { "responses", responses } }); paths[routePath] = pathRoute; } From b545f040d7be9eb5e945d446035027611f807597 Mon Sep 17 00:00:00 2001 From: Lennart Sauerbeck Date: Mon, 11 Sep 2017 17:06:38 +0200 Subject: [PATCH 2/2] SimpleAction: Add responses as well This should be squashed with the previous commit before merging. Signed-off-by: Lennart Sauerbeck --- src/action.cpp | 10 ++++++++++ src/action.h | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/src/action.cpp b/src/action.cpp index 33b0a8b0..f5cf00c1 100644 --- a/src/action.cpp +++ b/src/action.cpp @@ -256,6 +256,16 @@ std::vector SimpleAction::getInputs() const return m_Inputs; } +void SimpleAction::setResponses(const std::map& responses) +{ + m_Responses.insert(responses.begin(), responses.end()); +} + +std::map SimpleAction::getResponses() const +{ + return m_Responses; +} + std::vector SimpleAction::getHeaders() const { return m_Headers; diff --git a/src/action.h b/src/action.h index 204b398a..ccb1a419 100644 --- a/src/action.h +++ b/src/action.h @@ -123,6 +123,9 @@ class QTTPSHARED_EXPORT SimpleAction : public Action void setInputs(const std::vector& inputs); std::vector getInputs() const; + void setResponses(const std::map& responses); + std::map getResponses() const; + QTTP_PROTECTED: std::vector getHeaders() const; @@ -136,6 +139,7 @@ class QTTPSHARED_EXPORT SimpleAction : public Action QByteArray m_Description; QStringList m_Tags; std::vector m_Inputs; + std::map m_Responses; std::vector m_Headers; };