Commit 65df3deb authored by octal's avatar octal

Added produce and consume fields to the root description

parent 6db3a102
...@@ -61,8 +61,11 @@ private: ...@@ -61,8 +61,11 @@ private:
auto backendErrorResponse = auto backendErrorResponse =
desc.response(Http::Code::Internal_Server_Error, "An error occured with the backend"); desc.response(Http::Code::Internal_Server_Error, "An error occured with the backend");
desc.schemes(Rest::Scheme::Http); desc
desc.basePath("/v1"); .schemes(Rest::Scheme::Http)
.basePath("/v1")
.produces(MIME(Application, Json))
.consumes(MIME(Application, Json));
desc desc
.route(desc.get("/ready")) .route(desc.get("/ready"))
...@@ -77,7 +80,7 @@ private: ...@@ -77,7 +80,7 @@ private:
accountsPath accountsPath
.route(desc.get("/all")) .route(desc.get("/all"))
.bind(&BankerService::retrieveAllAccounts, this) .bind(&BankerService::retrieveAllAccounts, this)
.produces(MIME(Application, Json)) .produces(MIME(Application, Json), MIME(Application, Xml))
.response(Http::Code::Ok, "The list of all account"); .response(Http::Code::Ok, "The list of all account");
accountsPath accountsPath
......
...@@ -104,6 +104,32 @@ template<typename DT> struct DataTypeValidation { ...@@ -104,6 +104,32 @@ template<typename DT> struct DataTypeValidation {
} // namespace Traits } // namespace Traits
struct ProduceConsume {
std::vector<Http::Mime::MediaType> produce;
std::vector<Http::Mime::MediaType> consume;
template<typename Writer>
void serialize(Writer& writer) const {
auto serializeMimes = [&](const char* name, const std::vector<Http::Mime::MediaType>& mimes) {
if (!mimes.empty()) {
writer.String(name);
writer.StartArray();
{
for (const auto& mime: mimes) {
auto str = mime.toString();
writer.String(str.c_str());
}
}
writer.EndArray();
}
};
serializeMimes("consumes", consume);
serializeMimes("produces", produce);
}
};
struct Contact { struct Contact {
Contact(std::string name, std::string url, std::string email); Contact(std::string name, std::string url, std::string email);
...@@ -267,8 +293,7 @@ struct Path { ...@@ -267,8 +293,7 @@ struct Path {
std::string description; std::string description;
bool hidden; bool hidden;
std::vector<Http::Mime::MediaType> produceMimes; ProduceConsume pc;
std::vector<Http::Mime::MediaType> consumeMimes;
std::vector<Parameter> parameters; std::vector<Parameter> parameters;
std::vector<Response> responses; std::vector<Response> responses;
...@@ -282,19 +307,6 @@ struct Path { ...@@ -282,19 +307,6 @@ struct Path {
template<typename Writer> template<typename Writer>
void serialize(Writer& writer) const { void serialize(Writer& writer) const {
auto serializeMimes = [&](const char* name, const std::vector<Http::Mime::MediaType>& mimes) {
if (!mimes.empty()) {
writer.String(name);
writer.StartArray();
{
for (const auto& mime: mimes) {
auto str = mime.toString();
writer.String(str.c_str());
}
}
writer.EndArray();
}
};
std::string methodStr(methodString(method)); std::string methodStr(methodString(method));
// So it looks like Swagger requires method to be in lowercase // So it looks like Swagger requires method to be in lowercase
...@@ -305,8 +317,7 @@ struct Path { ...@@ -305,8 +317,7 @@ struct Path {
{ {
writer.String("description"); writer.String("description");
writer.String(description.c_str()); writer.String(description.c_str());
serializeMimes("consumes", consumeMimes); pc.serialize(writer);
serializeMimes("produces", produceMimes);
if (!parameters.empty()) { if (!parameters.empty()) {
writer.String("parameters"); writer.String("parameters");
writer.StartArray(); writer.StartArray();
...@@ -418,14 +429,14 @@ struct PathBuilder { ...@@ -418,14 +429,14 @@ struct PathBuilder {
template<typename... Mimes> template<typename... Mimes>
PathBuilder& produces(Mimes... mimes) { PathBuilder& produces(Mimes... mimes) {
const Http::Mime::MediaType m[] = { mimes... }; const Http::Mime::MediaType m[] = { mimes... };
std::copy(std::begin(m), std::end(m), std::back_inserter(path_->produceMimes)); std::copy(std::begin(m), std::end(m), std::back_inserter(path_->pc.produce));
return *this; return *this;
} }
template<typename... Mimes> template<typename... Mimes>
PathBuilder& consumes(Mimes... mimes) { PathBuilder& consumes(Mimes... mimes) {
const Http::Mime::MediaType m[] = { mimes... }; const Http::Mime::MediaType m[] = { mimes... };
std::copy(std::begin(m), std::end(m), std::back_inserter(path_->consumeMimes)); std::copy(std::begin(m), std::end(m), std::back_inserter(path_->pc.consume));
return *this; return *this;
} }
...@@ -511,6 +522,7 @@ public: ...@@ -511,6 +522,7 @@ public:
Description& host(std::string value); Description& host(std::string value);
Description& basePath(std::string value); Description& basePath(std::string value);
template<typename... Schemes> template<typename... Schemes>
Description& schemes(Schemes... schemes) { Description& schemes(Schemes... schemes) {
...@@ -519,6 +531,20 @@ public: ...@@ -519,6 +531,20 @@ public:
return *this; return *this;
} }
template<typename... Mimes>
Description& produces(Mimes... mimes) {
const Http::Mime::MediaType m[] = { mimes... };
std::copy(std::begin(m), std::end(m), std::back_inserter(pc.produce));
return *this;
}
template<typename... Mimes>
Description& consumes(Mimes... mimes) {
const Http::Mime::MediaType m[] = { mimes... };
std::copy(std::begin(m), std::end(m), std::back_inserter(pc.consume));
return *this;
}
Schema::PathFragment get(std::string name); Schema::PathFragment get(std::string name);
Schema::PathFragment post(std::string name); Schema::PathFragment post(std::string name);
Schema::PathFragment put(std::string name); Schema::PathFragment put(std::string name);
...@@ -556,6 +582,7 @@ public: ...@@ -556,6 +582,7 @@ public:
} }
writer.EndArray(); writer.EndArray();
} }
pc.serialize(writer);
paths_.serialize(writer, basePath_, Schema::PathGroup::Format::Swagger); paths_.serialize(writer, basePath_, Schema::PathGroup::Format::Swagger);
} }
...@@ -567,6 +594,7 @@ private: ...@@ -567,6 +594,7 @@ private:
std::string host_; std::string host_;
std::string basePath_; std::string basePath_;
std::vector<Scheme> schemes_; std::vector<Scheme> schemes_;
Schema::ProduceConsume pc;
Schema::PathGroup paths_; Schema::PathGroup paths_;
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment