From d9cea0f97ea12d69df70078b86e3b208c0312340 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Ros=C3=A9?= Date: Wed, 22 Nov 2017 05:13:05 +0100 Subject: [PATCH] [CppRest] Support optional parameters (#6959) * Use boost::optional for parameters that are not required. * Update sample petstore client. --- .../resources/cpprest/api-header.mustache | 12 ++++- .../resources/cpprest/api-source.mustache | 37 ++++++++++---- .../client/petstore/cpprest/api/PetApi.cpp | 28 +++++------ samples/client/petstore/cpprest/api/PetApi.h | 50 +++++++++++++++---- .../client/petstore/cpprest/api/StoreApi.h | 18 +++++-- .../client/petstore/cpprest/api/UserApi.cpp | 2 - samples/client/petstore/cpprest/api/UserApi.h | 42 ++++++++++++---- 7 files changed, 134 insertions(+), 55 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/cpprest/api-header.mustache b/modules/swagger-codegen/src/main/resources/cpprest/api-header.mustache index 4299c808378..31d44de7c46 100644 --- a/modules/swagger-codegen/src/main/resources/cpprest/api-header.mustache +++ b/modules/swagger-codegen/src/main/resources/cpprest/api-header.mustache @@ -14,6 +14,8 @@ {{#imports}}{{{import}}} {{/imports}} +#include + {{#apiNamespaceDeclarations}} namespace {{this}} { {{/apiNamespaceDeclarations}} @@ -32,8 +34,14 @@ public: /// /// {{notes}} /// - {{#allParams}}/// {{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}}{{/allParams}} - pplx::task<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> {{operationId}}({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); + {{#allParams}} + /// {{description}}{{^required}} (optional{{#defaultValue}}, default to {{.}}{{/defaultValue}}){{/required}} + {{/allParams}} + pplx::task<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> {{operationId}}( + {{#allParams}} + {{^required}}boost::optional<{{/required}}{{{dataType}}}{{^required}}>{{/required}} {{paramName}}{{#hasMore}},{{/hasMore}} + {{/allParams}} + ); {{/operation}} protected: diff --git a/modules/swagger-codegen/src/main/resources/cpprest/api-source.mustache b/modules/swagger-codegen/src/main/resources/cpprest/api-source.mustache index 51884fa1f8a..2e3ee4cd24c 100644 --- a/modules/swagger-codegen/src/main/resources/cpprest/api-source.mustache +++ b/modules/swagger-codegen/src/main/resources/cpprest/api-source.mustache @@ -26,7 +26,7 @@ using namespace {{modelNamespace}}; } {{#operation}} -pplx::task<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> {{classname}}::{{operationId}}({{#allParams}}{{{dataType}}} {{paramName}}{{^required}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) +pplx::task<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> {{classname}}::{{operationId}}({{#allParams}}{{^required}}boost::optional<{{/required}}{{{dataType}}}{{^required}}>{{/required}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) { {{#allParams}}{{#required}}{{^isPrimitiveType}}{{^isContainer}} // verify the required parameter '{{paramName}}' is set @@ -104,34 +104,53 @@ pplx::task<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/r {{#allParams}} {{^isBodyParam}} {{^isPathParam}} - {{^isPrimitiveType}}{{^isContainer}}if ({{paramName}} != nullptr){{/isContainer}}{{/isPrimitiveType}} + {{#required}} + {{^isPrimitiveType}} + {{^isContainer}} + if ({{paramName}} != nullptr) + {{/isContainer}} + {{/isPrimitiveType}} + {{/required}} + {{^required}} + {{^isPrimitiveType}} + {{^isContainer}} + if ({{paramName}} && *{{paramName}} != nullptr) + {{/isContainer}} + {{/isPrimitiveType}} + {{#isPrimitiveType}} + if ({{paramName}}) + {{/isPrimitiveType}} + {{#isContainer}} + if ({{paramName}}) + {{/isContainer}} + {{/required}} { {{#isContainer}} {{#isQueryParam}} - queryParams[utility::conversions::to_string_t("{{baseName}}")] = ApiClient::parameterToArrayString<{{items.datatype}}>({{paramName}}); + queryParams[utility::conversions::to_string_t("{{baseName}}")] = ApiClient::parameterToArrayString<{{items.datatype}}>({{^required}}*{{/required}}{{paramName}}); {{/isQueryParam}} {{#isHeaderParam}} - headerParams[utility::conversions::to_string_t("{{baseName}}")] = ApiClient::parameterToArrayString<{{items.datatype}}>({{paramName}}); + headerParams[utility::conversions::to_string_t("{{baseName}}")] = ApiClient::parameterToArrayString<{{items.datatype}}>({{^required}}*{{/required}}{{paramName}}); {{/isHeaderParam}} {{#isFormParam}} {{^isFile}} - formParams[ utility::conversions::to_string_t("{{baseName}}") ] = ApiClient::parameterToArrayString<{{items.datatype}}>({{paramName}}); + formParams[ utility::conversions::to_string_t("{{baseName}}") ] = ApiClient::parameterToArrayString<{{items.datatype}}>({{^required}}*{{/required}}{{paramName}}); {{/isFile}} {{/isFormParam}} {{/isContainer}} {{^isContainer}} {{#isQueryParam}} - queryParams[utility::conversions::to_string_t("{{baseName}}")] = ApiClient::parameterToString({{paramName}}); + queryParams[utility::conversions::to_string_t("{{baseName}}")] = ApiClient::parameterToString({{^required}}*{{/required}}{{paramName}}); {{/isQueryParam}} {{#isHeaderParam}} - headerParams[utility::conversions::to_string_t("{{baseName}}")] = ApiClient::parameterToString({{paramName}}); + headerParams[utility::conversions::to_string_t("{{baseName}}")] = ApiClient::parameterToString({{^required}}*{{/required}}{{paramName}}); {{/isHeaderParam}} {{#isFormParam}} {{#isFile}} - fileParams[ utility::conversions::to_string_t("{{baseName}}") ] = {{paramName}}; + fileParams[ utility::conversions::to_string_t("{{baseName}}") ] = {{^required}}*{{/required}}{{paramName}}; {{/isFile}} {{^isFile}} - formParams[ utility::conversions::to_string_t("{{baseName}}") ] = ApiClient::parameterToString({{paramName}}); + formParams[ utility::conversions::to_string_t("{{baseName}}") ] = ApiClient::parameterToString({{^required}}*{{/required}}{{paramName}}); {{/isFile}} {{/isFormParam}} {{/isContainer}} diff --git a/samples/client/petstore/cpprest/api/PetApi.cpp b/samples/client/petstore/cpprest/api/PetApi.cpp index e709c13d8f2..966907bb680 100644 --- a/samples/client/petstore/cpprest/api/PetApi.cpp +++ b/samples/client/petstore/cpprest/api/PetApi.cpp @@ -155,7 +155,7 @@ pplx::task PetApi::addPet(std::shared_ptr body) return void(); }); } -pplx::task PetApi::deletePet(int64_t petId, utility::string_t apiKey) +pplx::task PetApi::deletePet(int64_t petId, boost::optional apiKey) { @@ -198,9 +198,9 @@ pplx::task PetApi::deletePet(int64_t petId, utility::string_t apiKey) std::unordered_set consumeHttpContentTypes; - + if (apiKey) { - headerParams[utility::conversions::to_string_t("api_key")] = ApiClient::parameterToString(apiKey); + headerParams[utility::conversions::to_string_t("api_key")] = ApiClient::parameterToString(*apiKey); } std::shared_ptr httpBody; @@ -300,7 +300,6 @@ pplx::task>> PetApi::findPetsByStatus(std::vect std::unordered_set consumeHttpContentTypes; - { queryParams[utility::conversions::to_string_t("status")] = ApiClient::parameterToArrayString(status); } @@ -427,7 +426,6 @@ pplx::task>> PetApi::findPetsByTags(std::vector std::unordered_set consumeHttpContentTypes; - { queryParams[utility::conversions::to_string_t("tags")] = ApiClient::parameterToArrayString(tags); } @@ -754,7 +752,7 @@ pplx::task PetApi::updatePet(std::shared_ptr body) return void(); }); } -pplx::task PetApi::updatePetWithForm(int64_t petId, utility::string_t name, utility::string_t status) +pplx::task PetApi::updatePetWithForm(int64_t petId, boost::optional name, boost::optional status) { @@ -798,13 +796,13 @@ pplx::task PetApi::updatePetWithForm(int64_t petId, utility::string_t name std::unordered_set consumeHttpContentTypes; consumeHttpContentTypes.insert( utility::conversions::to_string_t("application/x-www-form-urlencoded") ); - + if (name) { - formParams[ utility::conversions::to_string_t("name") ] = ApiClient::parameterToString(name); + formParams[ utility::conversions::to_string_t("name") ] = ApiClient::parameterToString(*name); } - + if (status) { - formParams[ utility::conversions::to_string_t("status") ] = ApiClient::parameterToString(status); + formParams[ utility::conversions::to_string_t("status") ] = ApiClient::parameterToString(*status); } std::shared_ptr httpBody; @@ -862,7 +860,7 @@ pplx::task PetApi::updatePetWithForm(int64_t petId, utility::string_t name return void(); }); } -pplx::task> PetApi::uploadFile(int64_t petId, utility::string_t additionalMetadata, std::shared_ptr file) +pplx::task> PetApi::uploadFile(int64_t petId, boost::optional additionalMetadata, boost::optional> file) { @@ -905,13 +903,13 @@ pplx::task> PetApi::uploadFile(int64_t petId, utili std::unordered_set consumeHttpContentTypes; consumeHttpContentTypes.insert( utility::conversions::to_string_t("multipart/form-data") ); - + if (additionalMetadata) { - formParams[ utility::conversions::to_string_t("additionalMetadata") ] = ApiClient::parameterToString(additionalMetadata); + formParams[ utility::conversions::to_string_t("additionalMetadata") ] = ApiClient::parameterToString(*additionalMetadata); } - if (file != nullptr) + if (file && *file != nullptr) { - fileParams[ utility::conversions::to_string_t("file") ] = file; + fileParams[ utility::conversions::to_string_t("file") ] = *file; } std::shared_ptr httpBody; diff --git a/samples/client/petstore/cpprest/api/PetApi.h b/samples/client/petstore/cpprest/api/PetApi.h index f76d7f93e2c..adf96821916 100644 --- a/samples/client/petstore/cpprest/api/PetApi.h +++ b/samples/client/petstore/cpprest/api/PetApi.h @@ -27,6 +27,8 @@ #include "Pet.h" #include +#include + namespace io { namespace swagger { namespace client { @@ -46,15 +48,21 @@ public: /// /// /// Pet object that needs to be added to the store - pplx::task addPet(std::shared_ptr body); + pplx::task addPet( + std::shared_ptr body + ); /// /// Deletes a pet /// /// /// /// - /// Pet id to delete/// (optional) - pplx::task deletePet(int64_t petId, utility::string_t apiKey); + /// Pet id to delete + /// (optional) + pplx::task deletePet( + int64_t petId, + boost::optional apiKey + ); /// /// Finds Pets by status /// @@ -62,7 +70,9 @@ public: /// Multiple status values can be provided with comma separated strings /// /// Status values that need to be considered for filter - pplx::task>> findPetsByStatus(std::vector status); + pplx::task>> findPetsByStatus( + std::vector status + ); /// /// Finds Pets by tags /// @@ -70,7 +80,9 @@ public: /// Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. /// /// Tags to filter by - pplx::task>> findPetsByTags(std::vector tags); + pplx::task>> findPetsByTags( + std::vector tags + ); /// /// Find pet by ID /// @@ -78,7 +90,9 @@ public: /// Returns a single pet /// /// ID of pet to return - pplx::task> getPetById(int64_t petId); + pplx::task> getPetById( + int64_t petId + ); /// /// Update an existing pet /// @@ -86,23 +100,37 @@ public: /// /// /// Pet object that needs to be added to the store - pplx::task updatePet(std::shared_ptr body); + pplx::task updatePet( + std::shared_ptr body + ); /// /// Updates a pet in the store with form data /// /// /// /// - /// ID of pet that needs to be updated/// Updated name of the pet (optional)/// Updated status of the pet (optional) - pplx::task updatePetWithForm(int64_t petId, utility::string_t name, utility::string_t status); + /// ID of pet that needs to be updated + /// Updated name of the pet (optional) + /// Updated status of the pet (optional) + pplx::task updatePetWithForm( + int64_t petId, + boost::optional name, + boost::optional status + ); /// /// uploads an image /// /// /// /// - /// ID of pet to update/// Additional data to pass to server (optional)/// file to upload (optional) - pplx::task> uploadFile(int64_t petId, utility::string_t additionalMetadata, std::shared_ptr file); + /// ID of pet to update + /// Additional data to pass to server (optional) + /// file to upload (optional) + pplx::task> uploadFile( + int64_t petId, + boost::optional additionalMetadata, + boost::optional> file + ); protected: std::shared_ptr m_ApiClient; diff --git a/samples/client/petstore/cpprest/api/StoreApi.h b/samples/client/petstore/cpprest/api/StoreApi.h index 1f283e34a33..8140477461f 100644 --- a/samples/client/petstore/cpprest/api/StoreApi.h +++ b/samples/client/petstore/cpprest/api/StoreApi.h @@ -26,6 +26,8 @@ #include #include +#include + namespace io { namespace swagger { namespace client { @@ -45,15 +47,17 @@ public: /// For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors /// /// ID of the order that needs to be deleted - pplx::task deleteOrder(utility::string_t orderId); + pplx::task deleteOrder( + utility::string_t orderId + ); /// /// Returns pet inventories by status /// /// /// Returns a map of status codes to quantities /// - - pplx::task> getInventory(); + pplx::task> getInventory( + ); /// /// Find purchase order by ID /// @@ -61,7 +65,9 @@ public: /// For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions /// /// ID of pet that needs to be fetched - pplx::task> getOrderById(int64_t orderId); + pplx::task> getOrderById( + int64_t orderId + ); /// /// Place an order for a pet /// @@ -69,7 +75,9 @@ public: /// /// /// order placed for purchasing the pet - pplx::task> placeOrder(std::shared_ptr body); + pplx::task> placeOrder( + std::shared_ptr body + ); protected: std::shared_ptr m_ApiClient; diff --git a/samples/client/petstore/cpprest/api/UserApi.cpp b/samples/client/petstore/cpprest/api/UserApi.cpp index 8a820c52ae4..681d2ad2a16 100644 --- a/samples/client/petstore/cpprest/api/UserApi.cpp +++ b/samples/client/petstore/cpprest/api/UserApi.cpp @@ -654,11 +654,9 @@ pplx::task UserApi::loginUser(utility::string_t username, uti std::unordered_set consumeHttpContentTypes; - { queryParams[utility::conversions::to_string_t("username")] = ApiClient::parameterToString(username); } - { queryParams[utility::conversions::to_string_t("password")] = ApiClient::parameterToString(password); } diff --git a/samples/client/petstore/cpprest/api/UserApi.h b/samples/client/petstore/cpprest/api/UserApi.h index 4b694b8626b..41da01a5c82 100644 --- a/samples/client/petstore/cpprest/api/UserApi.h +++ b/samples/client/petstore/cpprest/api/UserApi.h @@ -26,6 +26,8 @@ #include #include +#include + namespace io { namespace swagger { namespace client { @@ -45,7 +47,9 @@ public: /// This can only be done by the logged in user. /// /// Created user object - pplx::task createUser(std::shared_ptr body); + pplx::task createUser( + std::shared_ptr body + ); /// /// Creates list of users with given input array /// @@ -53,7 +57,9 @@ public: /// /// /// List of user object - pplx::task createUsersWithArrayInput(std::vector> body); + pplx::task createUsersWithArrayInput( + std::vector> body + ); /// /// Creates list of users with given input array /// @@ -61,7 +67,9 @@ public: /// /// /// List of user object - pplx::task createUsersWithListInput(std::vector> body); + pplx::task createUsersWithListInput( + std::vector> body + ); /// /// Delete user /// @@ -69,7 +77,9 @@ public: /// This can only be done by the logged in user. /// /// The name that needs to be deleted - pplx::task deleteUser(utility::string_t username); + pplx::task deleteUser( + utility::string_t username + ); /// /// Get user by user name /// @@ -77,31 +87,41 @@ public: /// /// /// The name that needs to be fetched. Use user1 for testing. - pplx::task> getUserByName(utility::string_t username); + pplx::task> getUserByName( + utility::string_t username + ); /// /// Logs user into the system /// /// /// /// - /// The user name for login/// The password for login in clear text - pplx::task loginUser(utility::string_t username, utility::string_t password); + /// The user name for login + /// The password for login in clear text + pplx::task loginUser( + utility::string_t username, + utility::string_t password + ); /// /// Logs out current logged in user session /// /// /// /// - - pplx::task logoutUser(); + pplx::task logoutUser( + ); /// /// Updated user /// /// /// This can only be done by the logged in user. /// - /// name that need to be deleted/// Updated user object - pplx::task updateUser(utility::string_t username, std::shared_ptr body); + /// name that need to be deleted + /// Updated user object + pplx::task updateUser( + utility::string_t username, + std::shared_ptr body + ); protected: std::shared_ptr m_ApiClient;