diff --git a/include/pistache/description.h b/include/pistache/description.h index 775b3d831..dda9f8604 100644 --- a/include/pistache/description.h +++ b/include/pistache/description.h @@ -172,8 +172,18 @@ template std::unique_ptr makeDataType() { return std::unique_ptr(new DataTypeT()); } +#define PARAMETER_LOCATIONS \ + LOC(Path, "path") \ + LOC(Query, "query") + +enum class Location { +#define LOC(name, _) name, + PARAMETER_LOCATIONS +#undef LOC +}; + struct Parameter { - Parameter(std::string name, std::string description); + Parameter(std::string name, std::string description, Location location, bool required); template static Parameter create(Args &&... args) { @@ -184,6 +194,7 @@ struct Parameter { std::string name; std::string description; + Location location; bool required; std::shared_ptr type; }; @@ -298,10 +309,17 @@ struct PathBuilder { template PathBuilder ¶meter(std::string name, std::string description) { path_->parameters.push_back( - Parameter::create(std::move(name), std::move(description))); + Parameter::create(std::move(name), std::move(description), Location::Path, true)); return *this; } + template + PathBuilder &query(std::string name, std::string description, bool required) { + path_->parameters.push_back( + Parameter::create(std::move(name), std::move(description), Location::Query, required)); + return *this; + } + PathBuilder &response(Http::Code statusCode, std::string description) { path_->responses.push_back(Response(statusCode, std::move(description))); return *this; @@ -363,8 +381,14 @@ struct SubPath { template void parameter(std::string name, std::string description) { - parameters.push_back( - Parameter::create(std::move(name), std::move(description))); + parameters.push_back( + Parameter::create(std::move(name), std::move(description), Location::Path, true)); + } + + template + void query(std::string name, std::string description, bool required) { + parameters.push_back( + Parameter::create(std::move(name), std::move(description), Location::Query, required)); } std::string prefix; diff --git a/include/pistache/thirdparty/serializer/rapidjson.h b/include/pistache/thirdparty/serializer/rapidjson.h index 02bfbd4e2..98eb89e75 100644 --- a/include/pistache/thirdparty/serializer/rapidjson.h +++ b/include/pistache/thirdparty/serializer/rapidjson.h @@ -62,13 +62,18 @@ void serializePC(Writer &writer, const Schema::ProduceConsume &pc) { template void serializeParameter(Writer &writer, const Schema::Parameter ¶meter) { +#define LOC(_, name) name, + static char* locations[] = { + PARAMETER_LOCATIONS + }; +#undef LOC writer.StartObject(); { writer.String("name"); writer.String(parameter.name.c_str()); writer.String("in"); // @Feature: support other types of parameters - writer.String("path"); + writer.String(locations[static_cast(parameter.location)]); writer.String("description"); writer.String(parameter.description.c_str()); writer.String("required"); diff --git a/src/common/description.cc b/src/common/description.cc index fa2a251c5..bf15aa43b 100644 --- a/src/common/description.cc +++ b/src/common/description.cc @@ -184,9 +184,9 @@ SubPath SubPath::path(const std::string &prefix) { return SubPath(this->prefix + prefix, paths); } -Parameter::Parameter(std::string name, std::string description) - : name(std::move(name)), description(std::move(description)), - required(true), type() {} +Parameter::Parameter(std::string name, std::string description, Location location, bool required) + : name(std::move(name)), description(std::move(description)), location(location), + required(required), type() {} Response::Response(Http::Code statusCode, std::string description) : statusCode(statusCode), description(std::move(description)) {}