diff --git a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/apiclient-header.mustache b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/apiclient-header.mustache index 33addd7a397f..73eee1dc322d 100644 --- a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/apiclient-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/apiclient-header.mustache @@ -13,7 +13,9 @@ #include "{{packageName}}/ApiException.h" #include "{{packageName}}/IHttpBody.h" #include "{{packageName}}/HttpContent.h" - +{{^hasModelImport}} +#include "{{packageName}}/ModelBase.h" +{{/hasModelImport}} #if defined (_WIN32) || defined (_WIN64) #undef U #endif @@ -52,6 +54,9 @@ public: static utility::string_t parameterToString(double value); static utility::string_t parameterToString(const utility::datetime &value); static utility::string_t parameterToString(bool value); + {{^hasModelImport}} + static utility::string_t parameterToString(const ModelBase& value); + {{/hasModelImport}} template static utility::string_t parameterToString(const std::vector& value); template diff --git a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/apiclient-source.mustache b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/apiclient-source.mustache index 08407320b086..c69b3886876e 100644 --- a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/apiclient-source.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/apiclient-source.mustache @@ -79,6 +79,13 @@ utility::string_t ApiClient::parameterToString(const utility::datetime &value) return utility::conversions::to_string_t(value.to_string(utility::datetime::ISO_8601)); } +{{^hasModelImport}} +utility::string_t ApiClient::parameterToString(const ModelBase& value) +{ + return value.toJson().serialize(); +} +{{/hasModelImport}} + utility::string_t ApiClient::parameterToString(bool value) { std::stringstream valueAsStringStream; diff --git a/modules/openapi-generator/src/test/resources/3_0/cpp-restsdk/petstore.yaml b/modules/openapi-generator/src/test/resources/3_0/cpp-restsdk/petstore.yaml index 5e29ad21a09a..5394e75e5cee 100644 --- a/modules/openapi-generator/src/test/resources/3_0/cpp-restsdk/petstore.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/cpp-restsdk/petstore.yaml @@ -18,6 +18,30 @@ tags: - name: user description: Operations about user paths: + /pets: + post: + tags: + - pet + summary: List all pets + description: Returns a list of pets + parameters: + - name: page + in: query + description: The page number + required: false + schema: + $ref: '#/components/schemas/Page' + responses: + '200': + description: A list of pets + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/Pet' + '400': + description: Invalid page number /pet: post: tags: @@ -832,3 +856,13 @@ components: boosterRequired: type: boolean description: true if a booster is still needed to complete the vaccination + Page: + type: object + properties: + page: + type: integer + format: int32 + perPage: + type: integer + format: int32 + diff --git a/samples/client/petstore/cpp-restsdk/client/.openapi-generator/FILES b/samples/client/petstore/cpp-restsdk/client/.openapi-generator/FILES index 3c4a10c10335..2408edf0ae03 100644 --- a/samples/client/petstore/cpp-restsdk/client/.openapi-generator/FILES +++ b/samples/client/petstore/cpp-restsdk/client/.openapi-generator/FILES @@ -22,6 +22,7 @@ include/CppRestPetstoreClient/model/Category.h include/CppRestPetstoreClient/model/Color.h include/CppRestPetstoreClient/model/CreateUserOrPet_request.h include/CppRestPetstoreClient/model/Order.h +include/CppRestPetstoreClient/model/Page.h include/CppRestPetstoreClient/model/Pet.h include/CppRestPetstoreClient/model/SchemaWithSet.h include/CppRestPetstoreClient/model/SchemaWithSet_vaccinationBook.h @@ -46,6 +47,7 @@ src/model/Category.cpp src/model/Color.cpp src/model/CreateUserOrPet_request.cpp src/model/Order.cpp +src/model/Page.cpp src/model/Pet.cpp src/model/SchemaWithSet.cpp src/model/SchemaWithSet_vaccinationBook.cpp diff --git a/samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/ApiClient.h b/samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/ApiClient.h index b4d26fb69560..9b3fa59fa062 100644 --- a/samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/ApiClient.h +++ b/samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/ApiClient.h @@ -23,7 +23,7 @@ #include "CppRestPetstoreClient/ApiException.h" #include "CppRestPetstoreClient/IHttpBody.h" #include "CppRestPetstoreClient/HttpContent.h" - +#include "CppRestPetstoreClient/ModelBase.h" #if defined (_WIN32) || defined (_WIN64) #undef U #endif @@ -63,6 +63,7 @@ class ApiClient static utility::string_t parameterToString(double value); static utility::string_t parameterToString(const utility::datetime &value); static utility::string_t parameterToString(bool value); + static utility::string_t parameterToString(const ModelBase& value); template static utility::string_t parameterToString(const std::vector& value); template diff --git a/samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/api/PetApi.h b/samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/api/PetApi.h index 33688ff50e23..9a27c1bc1b2a 100644 --- a/samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/api/PetApi.h +++ b/samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/api/PetApi.h @@ -25,6 +25,7 @@ #include "CppRestPetstoreClient/model/ApiResponse.h" #include "CppRestPetstoreClient/model/Color.h" #include "CppRestPetstoreClient/HttpContent.h" +#include "CppRestPetstoreClient/model/Page.h" #include "CppRestPetstoreClient/model/Pet.h" #include #include @@ -110,6 +111,16 @@ class PetApi int64_t petId ) const; /// + /// List all pets + /// + /// + /// Returns a list of pets + /// + /// The page number (optional, default to nullptr) + pplx::task>> petsPost( + boost::optional> page + ) const; + /// /// Update an existing pet /// /// diff --git a/samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/model/Page.h b/samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/model/Page.h new file mode 100644 index 000000000000..7aeb0858b8b4 --- /dev/null +++ b/samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/model/Page.h @@ -0,0 +1,81 @@ +/** + * OpenAPI Petstore + * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. + * + * The version of the OpenAPI document: 1.0.0 + * + * NOTE: This class is auto generated by OpenAPI-Generator 7.14.0-SNAPSHOT. + * https://openapi-generator.tech + * Do not edit the class manually. + */ + +/* + * Page.h + * + * + */ + +#ifndef ORG_OPENAPITOOLS_CLIENT_MODEL_Page_H_ +#define ORG_OPENAPITOOLS_CLIENT_MODEL_Page_H_ + + +#include "CppRestPetstoreClient/ModelBase.h" + + +namespace org { +namespace openapitools { +namespace client { +namespace model { + + + +class Page + : public ModelBase +{ +public: + Page(); + virtual ~Page(); + + ///////////////////////////////////////////// + /// ModelBase overrides + + void validate() override; + + web::json::value toJson() const override; + bool fromJson(const web::json::value& json) override; + + void toMultipart(std::shared_ptr multipart, const utility::string_t& namePrefix) const override; + bool fromMultiPart(std::shared_ptr multipart, const utility::string_t& namePrefix) override; + + + ///////////////////////////////////////////// + /// Page members + + + int32_t getPage() const; + bool pageIsSet() const; + void unsetPage(); + void setPage(int32_t value); + + int32_t getPerPage() const; + bool perPageIsSet() const; + void unsetPerPage(); + void setPerPage(int32_t value); + + +protected: + int32_t m_Page; + bool m_PageIsSet; + + int32_t m_PerPage; + bool m_PerPageIsSet; + +}; + + +} +} +} +} + +#endif /* ORG_OPENAPITOOLS_CLIENT_MODEL_Page_H_ */ diff --git a/samples/client/petstore/cpp-restsdk/client/src/ApiClient.cpp b/samples/client/petstore/cpp-restsdk/client/src/ApiClient.cpp index 037298ba4c2c..9a0b4f0f8202 100644 --- a/samples/client/petstore/cpp-restsdk/client/src/ApiClient.cpp +++ b/samples/client/petstore/cpp-restsdk/client/src/ApiClient.cpp @@ -90,6 +90,11 @@ utility::string_t ApiClient::parameterToString(const utility::datetime &value) return utility::conversions::to_string_t(value.to_string(utility::datetime::ISO_8601)); } +utility::string_t ApiClient::parameterToString(const ModelBase& value) +{ + return value.toJson().serialize(); +} + utility::string_t ApiClient::parameterToString(bool value) { std::stringstream valueAsStringStream; diff --git a/samples/client/petstore/cpp-restsdk/client/src/api/PetApi.cpp b/samples/client/petstore/cpp-restsdk/client/src/api/PetApi.cpp index 6245be53d004..b6106d528584 100644 --- a/samples/client/petstore/cpp-restsdk/client/src/api/PetApi.cpp +++ b/samples/client/petstore/cpp-restsdk/client/src/api/PetApi.cpp @@ -822,6 +822,136 @@ pplx::task> PetApi::getPetById(int64_t petId) const return localVarResult; }); } +pplx::task>> PetApi::petsPost(boost::optional> page) const +{ + + + std::shared_ptr localVarApiConfiguration( m_ApiClient->getConfiguration() ); + utility::string_t localVarPath = utility::conversions::to_string_t("/pets"); + + std::map localVarQueryParams; + std::map localVarHeaderParams( localVarApiConfiguration->getDefaultHeaders() ); + std::map localVarFormParams; + std::map> localVarFileParams; + + std::unordered_set localVarResponseHttpContentTypes; + localVarResponseHttpContentTypes.insert( utility::conversions::to_string_t("application/json") ); + + utility::string_t localVarResponseHttpContentType; + + // use JSON if possible + if ( localVarResponseHttpContentTypes.size() == 0 ) + { + localVarResponseHttpContentType = utility::conversions::to_string_t("application/json"); + } + // JSON + else if ( localVarResponseHttpContentTypes.find(utility::conversions::to_string_t("application/json")) != localVarResponseHttpContentTypes.end() ) + { + localVarResponseHttpContentType = utility::conversions::to_string_t("application/json"); + } + // multipart formdata + else if( localVarResponseHttpContentTypes.find(utility::conversions::to_string_t("multipart/form-data")) != localVarResponseHttpContentTypes.end() ) + { + localVarResponseHttpContentType = utility::conversions::to_string_t("multipart/form-data"); + } + else + { + throw ApiException(400, utility::conversions::to_string_t("PetApi->petsPost does not produce any supported media type")); + } + + localVarHeaderParams[utility::conversions::to_string_t("Accept")] = localVarResponseHttpContentType; + + std::unordered_set localVarConsumeHttpContentTypes; + + if (page && *page != nullptr) + { + localVarQueryParams[utility::conversions::to_string_t("page")] = ApiClient::parameterToString(*page); + } + + std::shared_ptr localVarHttpBody; + utility::string_t localVarRequestHttpContentType; + + // use JSON if possible + if ( localVarConsumeHttpContentTypes.size() == 0 || localVarConsumeHttpContentTypes.find(utility::conversions::to_string_t("application/json")) != localVarConsumeHttpContentTypes.end() ) + { + localVarRequestHttpContentType = utility::conversions::to_string_t("application/json"); + } + // multipart formdata + else if( localVarConsumeHttpContentTypes.find(utility::conversions::to_string_t("multipart/form-data")) != localVarConsumeHttpContentTypes.end() ) + { + localVarRequestHttpContentType = utility::conversions::to_string_t("multipart/form-data"); + } + else if (localVarConsumeHttpContentTypes.find(utility::conversions::to_string_t("application/x-www-form-urlencoded")) != localVarConsumeHttpContentTypes.end()) + { + localVarRequestHttpContentType = utility::conversions::to_string_t("application/x-www-form-urlencoded"); + } + else + { + throw ApiException(415, utility::conversions::to_string_t("PetApi->petsPost does not consume any supported media type")); + } + + + return m_ApiClient->callApi(localVarPath, utility::conversions::to_string_t("POST"), localVarQueryParams, localVarHttpBody, localVarHeaderParams, localVarFormParams, localVarFileParams, localVarRequestHttpContentType) + .then([=, this](web::http::http_response localVarResponse) + { + if (m_ApiClient->getResponseHandler()) + { + m_ApiClient->getResponseHandler()(localVarResponse.status_code(), localVarResponse.headers()); + } + + // 1xx - informational : OK + // 2xx - successful : OK + // 3xx - redirection : OK + // 4xx - client error : not OK + // 5xx - client error : not OK + if (localVarResponse.status_code() >= 400) + { + throw ApiException(localVarResponse.status_code() + , utility::conversions::to_string_t("error calling petsPost: ") + localVarResponse.reason_phrase() + , std::make_shared(localVarResponse.extract_utf8string(true).get())); + } + + // check response content type + if(localVarResponse.headers().has(utility::conversions::to_string_t("Content-Type"))) + { + utility::string_t localVarContentType = localVarResponse.headers()[utility::conversions::to_string_t("Content-Type")]; + if( localVarContentType.find(localVarResponseHttpContentType) == std::string::npos ) + { + throw ApiException(500 + , utility::conversions::to_string_t("error calling petsPost: unexpected response type: ") + localVarContentType + , std::make_shared(localVarResponse.extract_utf8string(true).get())); + } + } + + return localVarResponse.extract_string(); + }) + .then([=, this](utility::string_t localVarResponse) + { + std::vector> localVarResult; + + if(localVarResponseHttpContentType == utility::conversions::to_string_t("application/json")) + { + web::json::value localVarJson = web::json::value::parse(localVarResponse); + for( auto& localVarItem : localVarJson.as_array() ) + { + std::shared_ptr localVarItemObj; + ModelBase::fromJson(localVarItem, localVarItemObj); + localVarResult.push_back(localVarItemObj); + } + } + // else if(localVarResponseHttpContentType == utility::conversions::to_string_t("multipart/form-data")) + // { + // TODO multipart response parsing + // } + else + { + throw ApiException(500 + , utility::conversions::to_string_t("error calling petsPost: unsupported response type")); + } + + return localVarResult; + }); +} pplx::task> PetApi::updatePet(std::shared_ptr pet) const { diff --git a/samples/client/petstore/cpp-restsdk/client/src/model/Page.cpp b/samples/client/petstore/cpp-restsdk/client/src/model/Page.cpp new file mode 100644 index 000000000000..71fccb0e12ac --- /dev/null +++ b/samples/client/petstore/cpp-restsdk/client/src/model/Page.cpp @@ -0,0 +1,171 @@ +/** + * OpenAPI Petstore + * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. + * + * The version of the OpenAPI document: 1.0.0 + * + * NOTE: This class is auto generated by OpenAPI-Generator 7.14.0-SNAPSHOT. + * https://openapi-generator.tech + * Do not edit the class manually. + */ + + + +#include "CppRestPetstoreClient/model/Page.h" + +namespace org { +namespace openapitools { +namespace client { +namespace model { + +Page::Page() +{ + m_Page = 0; + m_PageIsSet = false; + m_PerPage = 0; + m_PerPageIsSet = false; +} + +Page::~Page() +{ +} + +void Page::validate() +{ + // TODO: implement validation +} + +web::json::value Page::toJson() const +{ + web::json::value val = web::json::value::object(); + if(m_PageIsSet) + { + + val[utility::conversions::to_string_t(_XPLATSTR("page"))] = ModelBase::toJson(m_Page); + } + if(m_PerPageIsSet) + { + + val[utility::conversions::to_string_t(_XPLATSTR("perPage"))] = ModelBase::toJson(m_PerPage); + } + + return val; +} + +bool Page::fromJson(const web::json::value& val) +{ + bool ok = true; + if(val.has_field(utility::conversions::to_string_t(_XPLATSTR("page")))) + { + const web::json::value& fieldValue = val.at(utility::conversions::to_string_t(_XPLATSTR("page"))); + if(!fieldValue.is_null()) + { + int32_t refVal_setPage; + ok &= ModelBase::fromJson(fieldValue, refVal_setPage); + setPage(refVal_setPage); + + } + } + if(val.has_field(utility::conversions::to_string_t(_XPLATSTR("perPage")))) + { + const web::json::value& fieldValue = val.at(utility::conversions::to_string_t(_XPLATSTR("perPage"))); + if(!fieldValue.is_null()) + { + int32_t refVal_setPerPage; + ok &= ModelBase::fromJson(fieldValue, refVal_setPerPage); + setPerPage(refVal_setPerPage); + + } + } + return ok; +} + +void Page::toMultipart(std::shared_ptr multipart, const utility::string_t& prefix) const +{ + utility::string_t namePrefix = prefix; + if(namePrefix.size() > 0 && namePrefix.substr(namePrefix.size() - 1) != utility::conversions::to_string_t(_XPLATSTR("."))) + { + namePrefix += utility::conversions::to_string_t(_XPLATSTR(".")); + } + if(m_PageIsSet) + { + multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t(_XPLATSTR("page")), m_Page)); + } + if(m_PerPageIsSet) + { + multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t(_XPLATSTR("perPage")), m_PerPage)); + } +} + +bool Page::fromMultiPart(std::shared_ptr multipart, const utility::string_t& prefix) +{ + bool ok = true; + utility::string_t namePrefix = prefix; + if(namePrefix.size() > 0 && namePrefix.substr(namePrefix.size() - 1) != utility::conversions::to_string_t(_XPLATSTR("."))) + { + namePrefix += utility::conversions::to_string_t(_XPLATSTR(".")); + } + + if(multipart->hasContent(utility::conversions::to_string_t(_XPLATSTR("page")))) + { + int32_t refVal_setPage; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t(_XPLATSTR("page"))), refVal_setPage ); + setPage(refVal_setPage); + } + if(multipart->hasContent(utility::conversions::to_string_t(_XPLATSTR("perPage")))) + { + int32_t refVal_setPerPage; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t(_XPLATSTR("perPage"))), refVal_setPerPage ); + setPerPage(refVal_setPerPage); + } + return ok; +} + + +int32_t Page::getPage() const +{ + return m_Page; +} + +void Page::setPage(int32_t value) +{ + m_Page = value; + m_PageIsSet = true; +} + +bool Page::pageIsSet() const +{ + return m_PageIsSet; +} + +void Page::unsetPage() +{ + m_PageIsSet = false; +} +int32_t Page::getPerPage() const +{ + return m_PerPage; +} + +void Page::setPerPage(int32_t value) +{ + m_PerPage = value; + m_PerPageIsSet = true; +} + +bool Page::perPageIsSet() const +{ + return m_PerPageIsSet; +} + +void Page::unsetPerPage() +{ + m_PerPageIsSet = false; +} + +} +} +} +} + +