mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-05-12 04:30:51 +00:00
[cpprestsdk] Support passing enum request parameters (#20836)
* [cpprestsdk] Support passing enum request parameters * Add a fake endpoint to test enum request parameters * Add auto-generated sample codes * Update to date with master branch --------- Co-authored-by: leslizhang <453688819@qq.com>
This commit is contained in:
parent
2327562af4
commit
afa135f93d
@ -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<e{{classname}},utility::string_t> enumToStrMap = {
|
||||
{{#allowableValues}}
|
||||
{{#enumVars}}
|
||||
{ e{{classname}}::{{classname}}_{{{name}}}, "{{{name}}}" }{{^-last}},{{/-last}}
|
||||
{{/enumVars}}
|
||||
{{/allowableValues}}
|
||||
};
|
||||
std::map<utility::string_t,e{{classname}}> strToEnumMap = {
|
||||
{{#allowableValues}}
|
||||
{{#enumVars}}
|
||||
{ "{{{name}}}", e{{classname}}::{{classname}}_{{{name}}} }{{^-last}},{{/-last}}
|
||||
{{/enumVars}}
|
||||
{{/allowableValues}}
|
||||
};
|
||||
|
||||
};
|
||||
{{/isEnum}}
|
||||
{{^isEnum}}
|
||||
|
@ -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}}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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 <vector>
|
||||
@ -69,6 +70,16 @@ public:
|
||||
boost::optional<utility::string_t> apiKey
|
||||
) const;
|
||||
/// <summary>
|
||||
/// Finds Pets by color
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Returns pets filtered by color.
|
||||
/// </remarks>
|
||||
/// <param name="color">Color of the pet to filter by (optional, default to new Color())</param>
|
||||
pplx::task<std::vector<std::shared_ptr<Pet>>> findPetsByColor(
|
||||
boost::optional<std::shared_ptr<Color>> color
|
||||
) const;
|
||||
/// <summary>
|
||||
/// Finds Pets by status
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
|
@ -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<MultipartFormData> multipart, const utility::string_t& namePrefix) const override;
|
||||
bool fromMultiPart(std::shared_ptr<MultipartFormData> 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<eColor,utility::string_t> enumToStrMap = {
|
||||
{ eColor::Color_BLACK, "BLACK" },
|
||||
{ eColor::Color_WHITE, "WHITE" },
|
||||
{ eColor::Color_BROWN, "BROWN" },
|
||||
{ eColor::Color_GOLDEN, "GOLDEN" },
|
||||
{ eColor::Color_MIXED, "MIXED" }
|
||||
};
|
||||
std::map<utility::string_t,eColor> 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_ */
|
@ -294,6 +294,138 @@ pplx::task<void> PetApi::deletePet(int64_t petId, boost::optional<utility::strin
|
||||
return void();
|
||||
});
|
||||
}
|
||||
pplx::task<std::vector<std::shared_ptr<Pet>>> PetApi::findPetsByColor(boost::optional<std::shared_ptr<Color>> color) const
|
||||
{
|
||||
|
||||
|
||||
std::shared_ptr<const ApiConfiguration> localVarApiConfiguration( m_ApiClient->getConfiguration() );
|
||||
utility::string_t localVarPath = utility::conversions::to_string_t("/pet/findByColor");
|
||||
|
||||
std::map<utility::string_t, utility::string_t> localVarQueryParams;
|
||||
std::map<utility::string_t, utility::string_t> localVarHeaderParams( localVarApiConfiguration->getDefaultHeaders() );
|
||||
std::map<utility::string_t, utility::string_t> localVarFormParams;
|
||||
std::map<utility::string_t, std::shared_ptr<HttpContent>> localVarFileParams;
|
||||
|
||||
std::unordered_set<utility::string_t> 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<utility::string_t> localVarConsumeHttpContentTypes;
|
||||
|
||||
if (color && *color != nullptr)
|
||||
{
|
||||
localVarQueryParams[utility::conversions::to_string_t("color")] = ApiClient::parameterToString(*color);
|
||||
}
|
||||
|
||||
std::shared_ptr<IHttpBody> 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<std::stringstream>(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<std::stringstream>(localVarResponse.extract_utf8string(true).get()));
|
||||
}
|
||||
}
|
||||
|
||||
return localVarResponse.extract_string();
|
||||
})
|
||||
.then([=, this](utility::string_t localVarResponse)
|
||||
{
|
||||
std::vector<std::shared_ptr<Pet>> 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<Pet> 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<std::vector<std::shared_ptr<Pet>>> PetApi::findPetsByStatus(std::vector<utility::string_t> status) const
|
||||
{
|
||||
|
||||
|
137
samples/client/petstore/cpp-restsdk/client/src/model/Color.cpp
Normal file
137
samples/client/petstore/cpp-restsdk/client/src/model/Color.cpp
Normal file
@ -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<MultipartFormData> 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<MultipartFormData> 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] );
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user