diff --git a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/model-header.mustache b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/model-header.mustache index a8d71b425ff..0f9ec5bbc70 100644 --- a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/model-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/model-header.mustache @@ -92,6 +92,15 @@ class {{declspec}} {{classname}} { public: {{classname}}(); + {{classname}}(utility::string_t str); + operator utility::string_t() const { + return enumToStrMap.at(getValue()); + } + + {{! operator std::string() const { + return enumToStrMap.at(getValue()); + } }} + virtual ~{{classname}}(); ///////////////////////////////////////////// @@ -124,6 +133,21 @@ public: protected: e{{classname}} m_value; + std::map enumToStrMap = { + {{#allowableValues}} + {{#enumVars}} + { e{{classname}}::{{classname}}_{{{name}}}, "{{{name}}}" }{{^-last}},{{/-last}} + {{/enumVars}} + {{/allowableValues}} +}; + std::map strToEnumMap = { + {{#allowableValues}} + {{#enumVars}} + { "{{{name}}}", e{{classname}}::{{classname}}_{{{name}}} }{{^-last}},{{/-last}} + {{/enumVars}} + {{/allowableValues}} +}; + }; {{/isEnum}} {{^isEnum}} diff --git a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/model-source.mustache b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/model-source.mustache index fcb6637804f..66a9e42adbd 100644 --- a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/model-source.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/model-source.mustache @@ -176,6 +176,10 @@ void {{classname}}::setValue({{classname}}::e{{classname}} const value) m_value = value; } +{{classname}}::{{classname}}(utility::string_t str){ + setValue( strToEnumMap[str] ); +} + {{/isEnum}} {{^isEnum}} 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 28dc856f83e..5e29ad21a09 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 @@ -117,6 +117,34 @@ paths: security: - petstore_auth: - 'read:pets' + /pet/findByColor: + get: + tags: + - pet + summary: Finds Pets by color + description: Returns pets filtered by color. + operationId: findPetsByColor + parameters: + - name: color + in: query + description: Color of the pet to filter by + required: false + schema: + $ref: '#/components/schemas/Color' + responses: + '200': + description: successful operation + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/Pet' + '400': + description: Invalid color value + security: + - petstore_auth: + - 'read:pets' /pet/findByTags: get: tags: @@ -748,6 +776,18 @@ components: - sold xml: name: Pet + Color: + title: Pet Color + description: pet color in the store + type: string + enum: + - black + - white + - brown + - golden + - mixed + xml: + name: Pet ApiResponse: title: An uploaded response description: Describes the result of uploading an image resource diff --git a/samples/client/petstore/cpp-restsdk/client/.openapi-generator/FILES b/samples/client/petstore/cpp-restsdk/client/.openapi-generator/FILES index 0570b3e661c..3c4a10c1033 100644 --- a/samples/client/petstore/cpp-restsdk/client/.openapi-generator/FILES +++ b/samples/client/petstore/cpp-restsdk/client/.openapi-generator/FILES @@ -19,6 +19,7 @@ include/CppRestPetstoreClient/api/UserApi.h include/CppRestPetstoreClient/api/UserOrPetApi.h include/CppRestPetstoreClient/model/ApiResponse.h include/CppRestPetstoreClient/model/Category.h +include/CppRestPetstoreClient/model/Color.h include/CppRestPetstoreClient/model/CreateUserOrPet_request.h include/CppRestPetstoreClient/model/Order.h include/CppRestPetstoreClient/model/Pet.h @@ -42,6 +43,7 @@ src/api/UserApi.cpp src/api/UserOrPetApi.cpp src/model/ApiResponse.cpp src/model/Category.cpp +src/model/Color.cpp src/model/CreateUserOrPet_request.cpp src/model/Order.cpp src/model/Pet.cpp 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 123c3cd532b..33688ff50e2 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 @@ -23,6 +23,7 @@ #include "CppRestPetstoreClient/ApiClient.h" #include "CppRestPetstoreClient/model/ApiResponse.h" +#include "CppRestPetstoreClient/model/Color.h" #include "CppRestPetstoreClient/HttpContent.h" #include "CppRestPetstoreClient/model/Pet.h" #include @@ -69,6 +70,16 @@ public: boost::optional apiKey ) const; /// + /// Finds Pets by color + /// + /// + /// Returns pets filtered by color. + /// + /// Color of the pet to filter by (optional, default to new Color()) + pplx::task>> findPetsByColor( + boost::optional> color + ) const; + /// /// Finds Pets by status /// /// diff --git a/samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/model/Color.h b/samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/model/Color.h new file mode 100644 index 00000000000..c2de795da8d --- /dev/null +++ b/samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/model/Color.h @@ -0,0 +1,91 @@ +/** + * 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. + */ + +/* + * Color.h + * + * pet color in the store + */ + +#ifndef ORG_OPENAPITOOLS_CLIENT_MODEL_Color_H_ +#define ORG_OPENAPITOOLS_CLIENT_MODEL_Color_H_ + + +#include "CppRestPetstoreClient/ModelBase.h" + + +namespace org { +namespace openapitools { +namespace client { +namespace model { + + +class Color + : public ModelBase +{ +public: + Color(); + Color(utility::string_t str); + operator utility::string_t() const { + return enumToStrMap.at(getValue()); + } + + + virtual ~Color(); + + ///////////////////////////////////////////// + /// 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; + + enum class eColor + { + Color_BLACK, + Color_WHITE, + Color_BROWN, + Color_GOLDEN, + Color_MIXED, + }; + + eColor getValue() const; + void setValue(eColor const value); + + protected: + eColor m_value; + std::map enumToStrMap = { + { eColor::Color_BLACK, "BLACK" }, + { eColor::Color_WHITE, "WHITE" }, + { eColor::Color_BROWN, "BROWN" }, + { eColor::Color_GOLDEN, "GOLDEN" }, + { eColor::Color_MIXED, "MIXED" } +}; + std::map strToEnumMap = { + { "BLACK", eColor::Color_BLACK }, + { "WHITE", eColor::Color_WHITE }, + { "BROWN", eColor::Color_BROWN }, + { "GOLDEN", eColor::Color_GOLDEN }, + { "MIXED", eColor::Color_MIXED } +}; + +}; + +} +} +} +} + +#endif /* ORG_OPENAPITOOLS_CLIENT_MODEL_Color_H_ */ 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 9378f35c472..6245be53d00 100644 --- a/samples/client/petstore/cpp-restsdk/client/src/api/PetApi.cpp +++ b/samples/client/petstore/cpp-restsdk/client/src/api/PetApi.cpp @@ -294,6 +294,138 @@ pplx::task PetApi::deletePet(int64_t petId, boost::optional>> PetApi::findPetsByColor(boost::optional> color) const +{ + + + std::shared_ptr localVarApiConfiguration( m_ApiClient->getConfiguration() ); + utility::string_t localVarPath = utility::conversions::to_string_t("/pet/findByColor"); + + 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->findPetsByColor does not produce any supported media type")); + } + + localVarHeaderParams[utility::conversions::to_string_t("Accept")] = localVarResponseHttpContentType; + + std::unordered_set localVarConsumeHttpContentTypes; + + if (color && *color != nullptr) + { + localVarQueryParams[utility::conversions::to_string_t("color")] = ApiClient::parameterToString(*color); + } + + 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->findPetsByColor does not consume any supported media type")); + } + + // authentication (petstore_auth) required + // oauth2 authentication is added automatically as part of the http_client_config + + return m_ApiClient->callApi(localVarPath, utility::conversions::to_string_t("GET"), 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 findPetsByColor: ") + 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 findPetsByColor: 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 findPetsByColor: unsupported response type")); + } + + return localVarResult; + }); +} pplx::task>> PetApi::findPetsByStatus(std::vector status) const { diff --git a/samples/client/petstore/cpp-restsdk/client/src/model/Color.cpp b/samples/client/petstore/cpp-restsdk/client/src/model/Color.cpp new file mode 100644 index 00000000000..ed466db022e --- /dev/null +++ b/samples/client/petstore/cpp-restsdk/client/src/model/Color.cpp @@ -0,0 +1,137 @@ +/** + * 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/Color.h" + +namespace org { +namespace openapitools { +namespace client { +namespace model { + +namespace +{ +using EnumUnderlyingType = utility::string_t; + +Color::eColor toEnum(const EnumUnderlyingType& val) +{ + if (val == utility::conversions::to_string_t(U("black"))) + return Color::eColor::Color_BLACK; + if (val == utility::conversions::to_string_t(U("white"))) + return Color::eColor::Color_WHITE; + if (val == utility::conversions::to_string_t(U("brown"))) + return Color::eColor::Color_BROWN; + if (val == utility::conversions::to_string_t(U("golden"))) + return Color::eColor::Color_GOLDEN; + if (val == utility::conversions::to_string_t(U("mixed"))) + return Color::eColor::Color_MIXED; + return {}; +} + +EnumUnderlyingType fromEnum(Color::eColor e) +{ + switch (e) + { + case Color::eColor::Color_BLACK: + return U("black"); + case Color::eColor::Color_WHITE: + return U("white"); + case Color::eColor::Color_BROWN: + return U("brown"); + case Color::eColor::Color_GOLDEN: + return U("golden"); + case Color::eColor::Color_MIXED: + return U("mixed"); + default: + break; + } + return {}; +} +} + +Color::Color() +{ +} + +Color::~Color() +{ +} + +void Color::validate() +{ + // TODO: implement validation +} + +web::json::value Color::toJson() const +{ + auto val = fromEnum(m_value); + return web::json::value(val); +} + +bool Color::fromJson(const web::json::value& val) +{ + m_value = toEnum(val.as_string()); + return true; +} + +void Color::toMultipart(std::shared_ptr multipart, const utility::string_t& prefix) const +{ + utility::string_t namePrefix = prefix; + if (!namePrefix.empty() && namePrefix.back() != U('.')) + { + namePrefix.push_back(U('.')); + } + + auto e = fromEnum(m_value); + multipart->add(ModelBase::toHttpContent(namePrefix, e)); +} + +bool Color::fromMultiPart(std::shared_ptr multipart, const utility::string_t& prefix) +{ + bool ok = true; + utility::string_t namePrefix = prefix; + if (!namePrefix.empty() && namePrefix.back() != U('.')) + { + namePrefix.push_back(U('.')); + } + { + EnumUnderlyingType e; + ok = ModelBase::fromHttpContent(multipart->getContent(namePrefix), e); + if (ok) + { + auto v = toEnum(e); + setValue(v); + } + } + return ok; +} + +Color::eColor Color::getValue() const +{ + return m_value; +} + +void Color::setValue(Color::eColor const value) +{ + m_value = value; +} + +Color::Color(utility::string_t str){ + setValue( strToEnumMap[str] ); +} + +} +} +} +} + +