Skip to content

Commit afa135f

Browse files
rickymaleslizhang
andauthored
[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>
1 parent 2327562 commit afa135f

File tree

8 files changed

+441
-0
lines changed

8 files changed

+441
-0
lines changed

modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/model-header.mustache

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ class {{declspec}} {{classname}}
9292
{
9393
public:
9494
{{classname}}();
95+
{{classname}}(utility::string_t str);
96+
operator utility::string_t() const {
97+
return enumToStrMap.at(getValue());
98+
}
99+
100+
{{! operator std::string() const {
101+
return enumToStrMap.at(getValue());
102+
} }}
103+
95104
virtual ~{{classname}}();
96105

97106
/////////////////////////////////////////////
@@ -124,6 +133,21 @@ public:
124133

125134
protected:
126135
e{{classname}} m_value;
136+
std::map<e{{classname}},utility::string_t> enumToStrMap = {
137+
{{#allowableValues}}
138+
{{#enumVars}}
139+
{ e{{classname}}::{{classname}}_{{{name}}}, "{{{name}}}" }{{^-last}},{{/-last}}
140+
{{/enumVars}}
141+
{{/allowableValues}}
142+
};
143+
std::map<utility::string_t,e{{classname}}> strToEnumMap = {
144+
{{#allowableValues}}
145+
{{#enumVars}}
146+
{ "{{{name}}}", e{{classname}}::{{classname}}_{{{name}}} }{{^-last}},{{/-last}}
147+
{{/enumVars}}
148+
{{/allowableValues}}
149+
};
150+
127151
};
128152
{{/isEnum}}
129153
{{^isEnum}}

modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/model-source.mustache

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ void {{classname}}::setValue({{classname}}::e{{classname}} const value)
176176
m_value = value;
177177
}
178178

179+
{{classname}}::{{classname}}(utility::string_t str){
180+
setValue( strToEnumMap[str] );
181+
}
182+
179183
{{/isEnum}}
180184
{{^isEnum}}
181185

modules/openapi-generator/src/test/resources/3_0/cpp-restsdk/petstore.yaml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,34 @@ paths:
117117
security:
118118
- petstore_auth:
119119
- 'read:pets'
120+
/pet/findByColor:
121+
get:
122+
tags:
123+
- pet
124+
summary: Finds Pets by color
125+
description: Returns pets filtered by color.
126+
operationId: findPetsByColor
127+
parameters:
128+
- name: color
129+
in: query
130+
description: Color of the pet to filter by
131+
required: false
132+
schema:
133+
$ref: '#/components/schemas/Color'
134+
responses:
135+
'200':
136+
description: successful operation
137+
content:
138+
application/json:
139+
schema:
140+
type: array
141+
items:
142+
$ref: '#/components/schemas/Pet'
143+
'400':
144+
description: Invalid color value
145+
security:
146+
- petstore_auth:
147+
- 'read:pets'
120148
/pet/findByTags:
121149
get:
122150
tags:
@@ -748,6 +776,18 @@ components:
748776
- sold
749777
xml:
750778
name: Pet
779+
Color:
780+
title: Pet Color
781+
description: pet color in the store
782+
type: string
783+
enum:
784+
- black
785+
- white
786+
- brown
787+
- golden
788+
- mixed
789+
xml:
790+
name: Pet
751791
ApiResponse:
752792
title: An uploaded response
753793
description: Describes the result of uploading an image resource

samples/client/petstore/cpp-restsdk/client/.openapi-generator/FILES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ include/CppRestPetstoreClient/api/UserApi.h
1919
include/CppRestPetstoreClient/api/UserOrPetApi.h
2020
include/CppRestPetstoreClient/model/ApiResponse.h
2121
include/CppRestPetstoreClient/model/Category.h
22+
include/CppRestPetstoreClient/model/Color.h
2223
include/CppRestPetstoreClient/model/CreateUserOrPet_request.h
2324
include/CppRestPetstoreClient/model/Order.h
2425
include/CppRestPetstoreClient/model/Pet.h
@@ -42,6 +43,7 @@ src/api/UserApi.cpp
4243
src/api/UserOrPetApi.cpp
4344
src/model/ApiResponse.cpp
4445
src/model/Category.cpp
46+
src/model/Color.cpp
4547
src/model/CreateUserOrPet_request.cpp
4648
src/model/Order.cpp
4749
src/model/Pet.cpp

samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/api/PetApi.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "CppRestPetstoreClient/ApiClient.h"
2424

2525
#include "CppRestPetstoreClient/model/ApiResponse.h"
26+
#include "CppRestPetstoreClient/model/Color.h"
2627
#include "CppRestPetstoreClient/HttpContent.h"
2728
#include "CppRestPetstoreClient/model/Pet.h"
2829
#include <vector>
@@ -69,6 +70,16 @@ class PetApi
6970
boost::optional<utility::string_t> apiKey
7071
) const;
7172
/// <summary>
73+
/// Finds Pets by color
74+
/// </summary>
75+
/// <remarks>
76+
/// Returns pets filtered by color.
77+
/// </remarks>
78+
/// <param name="color">Color of the pet to filter by (optional, default to new Color())</param>
79+
pplx::task<std::vector<std::shared_ptr<Pet>>> findPetsByColor(
80+
boost::optional<std::shared_ptr<Color>> color
81+
) const;
82+
/// <summary>
7283
/// Finds Pets by status
7384
/// </summary>
7485
/// <remarks>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* OpenAPI Petstore
3+
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
4+
*
5+
* The version of the OpenAPI document: 1.0.0
6+
*
7+
* NOTE: This class is auto generated by OpenAPI-Generator 7.14.0-SNAPSHOT.
8+
* https://openapi-generator.tech
9+
* Do not edit the class manually.
10+
*/
11+
12+
/*
13+
* Color.h
14+
*
15+
* pet color in the store
16+
*/
17+
18+
#ifndef ORG_OPENAPITOOLS_CLIENT_MODEL_Color_H_
19+
#define ORG_OPENAPITOOLS_CLIENT_MODEL_Color_H_
20+
21+
22+
#include "CppRestPetstoreClient/ModelBase.h"
23+
24+
25+
namespace org {
26+
namespace openapitools {
27+
namespace client {
28+
namespace model {
29+
30+
31+
class Color
32+
: public ModelBase
33+
{
34+
public:
35+
Color();
36+
Color(utility::string_t str);
37+
operator utility::string_t() const {
38+
return enumToStrMap.at(getValue());
39+
}
40+
41+
42+
virtual ~Color();
43+
44+
/////////////////////////////////////////////
45+
/// ModelBase overrides
46+
47+
void validate() override;
48+
49+
web::json::value toJson() const override;
50+
bool fromJson(const web::json::value& json) override;
51+
52+
void toMultipart(std::shared_ptr<MultipartFormData> multipart, const utility::string_t& namePrefix) const override;
53+
bool fromMultiPart(std::shared_ptr<MultipartFormData> multipart, const utility::string_t& namePrefix) override;
54+
55+
enum class eColor
56+
{
57+
Color_BLACK,
58+
Color_WHITE,
59+
Color_BROWN,
60+
Color_GOLDEN,
61+
Color_MIXED,
62+
};
63+
64+
eColor getValue() const;
65+
void setValue(eColor const value);
66+
67+
protected:
68+
eColor m_value;
69+
std::map<eColor,utility::string_t> enumToStrMap = {
70+
{ eColor::Color_BLACK, "BLACK" },
71+
{ eColor::Color_WHITE, "WHITE" },
72+
{ eColor::Color_BROWN, "BROWN" },
73+
{ eColor::Color_GOLDEN, "GOLDEN" },
74+
{ eColor::Color_MIXED, "MIXED" }
75+
};
76+
std::map<utility::string_t,eColor> strToEnumMap = {
77+
{ "BLACK", eColor::Color_BLACK },
78+
{ "WHITE", eColor::Color_WHITE },
79+
{ "BROWN", eColor::Color_BROWN },
80+
{ "GOLDEN", eColor::Color_GOLDEN },
81+
{ "MIXED", eColor::Color_MIXED }
82+
};
83+
84+
};
85+
86+
}
87+
}
88+
}
89+
}
90+
91+
#endif /* ORG_OPENAPITOOLS_CLIENT_MODEL_Color_H_ */

samples/client/petstore/cpp-restsdk/client/src/api/PetApi.cpp

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,138 @@ pplx::task<void> PetApi::deletePet(int64_t petId, boost::optional<utility::strin
294294
return void();
295295
});
296296
}
297+
pplx::task<std::vector<std::shared_ptr<Pet>>> PetApi::findPetsByColor(boost::optional<std::shared_ptr<Color>> color) const
298+
{
299+
300+
301+
std::shared_ptr<const ApiConfiguration> localVarApiConfiguration( m_ApiClient->getConfiguration() );
302+
utility::string_t localVarPath = utility::conversions::to_string_t("/pet/findByColor");
303+
304+
std::map<utility::string_t, utility::string_t> localVarQueryParams;
305+
std::map<utility::string_t, utility::string_t> localVarHeaderParams( localVarApiConfiguration->getDefaultHeaders() );
306+
std::map<utility::string_t, utility::string_t> localVarFormParams;
307+
std::map<utility::string_t, std::shared_ptr<HttpContent>> localVarFileParams;
308+
309+
std::unordered_set<utility::string_t> localVarResponseHttpContentTypes;
310+
localVarResponseHttpContentTypes.insert( utility::conversions::to_string_t("application/json") );
311+
312+
utility::string_t localVarResponseHttpContentType;
313+
314+
// use JSON if possible
315+
if ( localVarResponseHttpContentTypes.size() == 0 )
316+
{
317+
localVarResponseHttpContentType = utility::conversions::to_string_t("application/json");
318+
}
319+
// JSON
320+
else if ( localVarResponseHttpContentTypes.find(utility::conversions::to_string_t("application/json")) != localVarResponseHttpContentTypes.end() )
321+
{
322+
localVarResponseHttpContentType = utility::conversions::to_string_t("application/json");
323+
}
324+
// multipart formdata
325+
else if( localVarResponseHttpContentTypes.find(utility::conversions::to_string_t("multipart/form-data")) != localVarResponseHttpContentTypes.end() )
326+
{
327+
localVarResponseHttpContentType = utility::conversions::to_string_t("multipart/form-data");
328+
}
329+
else
330+
{
331+
throw ApiException(400, utility::conversions::to_string_t("PetApi->findPetsByColor does not produce any supported media type"));
332+
}
333+
334+
localVarHeaderParams[utility::conversions::to_string_t("Accept")] = localVarResponseHttpContentType;
335+
336+
std::unordered_set<utility::string_t> localVarConsumeHttpContentTypes;
337+
338+
if (color && *color != nullptr)
339+
{
340+
localVarQueryParams[utility::conversions::to_string_t("color")] = ApiClient::parameterToString(*color);
341+
}
342+
343+
std::shared_ptr<IHttpBody> localVarHttpBody;
344+
utility::string_t localVarRequestHttpContentType;
345+
346+
// use JSON if possible
347+
if ( localVarConsumeHttpContentTypes.size() == 0 || localVarConsumeHttpContentTypes.find(utility::conversions::to_string_t("application/json")) != localVarConsumeHttpContentTypes.end() )
348+
{
349+
localVarRequestHttpContentType = utility::conversions::to_string_t("application/json");
350+
}
351+
// multipart formdata
352+
else if( localVarConsumeHttpContentTypes.find(utility::conversions::to_string_t("multipart/form-data")) != localVarConsumeHttpContentTypes.end() )
353+
{
354+
localVarRequestHttpContentType = utility::conversions::to_string_t("multipart/form-data");
355+
}
356+
else if (localVarConsumeHttpContentTypes.find(utility::conversions::to_string_t("application/x-www-form-urlencoded")) != localVarConsumeHttpContentTypes.end())
357+
{
358+
localVarRequestHttpContentType = utility::conversions::to_string_t("application/x-www-form-urlencoded");
359+
}
360+
else
361+
{
362+
throw ApiException(415, utility::conversions::to_string_t("PetApi->findPetsByColor does not consume any supported media type"));
363+
}
364+
365+
// authentication (petstore_auth) required
366+
// oauth2 authentication is added automatically as part of the http_client_config
367+
368+
return m_ApiClient->callApi(localVarPath, utility::conversions::to_string_t("GET"), localVarQueryParams, localVarHttpBody, localVarHeaderParams, localVarFormParams, localVarFileParams, localVarRequestHttpContentType)
369+
.then([=, this](web::http::http_response localVarResponse)
370+
{
371+
if (m_ApiClient->getResponseHandler())
372+
{
373+
m_ApiClient->getResponseHandler()(localVarResponse.status_code(), localVarResponse.headers());
374+
}
375+
376+
// 1xx - informational : OK
377+
// 2xx - successful : OK
378+
// 3xx - redirection : OK
379+
// 4xx - client error : not OK
380+
// 5xx - client error : not OK
381+
if (localVarResponse.status_code() >= 400)
382+
{
383+
throw ApiException(localVarResponse.status_code()
384+
, utility::conversions::to_string_t("error calling findPetsByColor: ") + localVarResponse.reason_phrase()
385+
, std::make_shared<std::stringstream>(localVarResponse.extract_utf8string(true).get()));
386+
}
387+
388+
// check response content type
389+
if(localVarResponse.headers().has(utility::conversions::to_string_t("Content-Type")))
390+
{
391+
utility::string_t localVarContentType = localVarResponse.headers()[utility::conversions::to_string_t("Content-Type")];
392+
if( localVarContentType.find(localVarResponseHttpContentType) == std::string::npos )
393+
{
394+
throw ApiException(500
395+
, utility::conversions::to_string_t("error calling findPetsByColor: unexpected response type: ") + localVarContentType
396+
, std::make_shared<std::stringstream>(localVarResponse.extract_utf8string(true).get()));
397+
}
398+
}
399+
400+
return localVarResponse.extract_string();
401+
})
402+
.then([=, this](utility::string_t localVarResponse)
403+
{
404+
std::vector<std::shared_ptr<Pet>> localVarResult;
405+
406+
if(localVarResponseHttpContentType == utility::conversions::to_string_t("application/json"))
407+
{
408+
web::json::value localVarJson = web::json::value::parse(localVarResponse);
409+
for( auto& localVarItem : localVarJson.as_array() )
410+
{
411+
std::shared_ptr<Pet> localVarItemObj;
412+
ModelBase::fromJson(localVarItem, localVarItemObj);
413+
localVarResult.push_back(localVarItemObj);
414+
}
415+
}
416+
// else if(localVarResponseHttpContentType == utility::conversions::to_string_t("multipart/form-data"))
417+
// {
418+
// TODO multipart response parsing
419+
// }
420+
else
421+
{
422+
throw ApiException(500
423+
, utility::conversions::to_string_t("error calling findPetsByColor: unsupported response type"));
424+
}
425+
426+
return localVarResult;
427+
});
428+
}
297429
pplx::task<std::vector<std::shared_ptr<Pet>>> PetApi::findPetsByStatus(std::vector<utility::string_t> status) const
298430
{
299431

0 commit comments

Comments
 (0)