Skip to content

Commit 6264f73

Browse files
committed
[cpp-rest-sdk] support serializing model base as parameters
This fixes the code generation for the cases where the model is referenced as a parameter
1 parent f281371 commit 6264f73

File tree

10 files changed

+445
-2
lines changed

10 files changed

+445
-2
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "{{packageName}}/ApiException.h"
1414
#include "{{packageName}}/IHttpBody.h"
1515
#include "{{packageName}}/HttpContent.h"
16-
16+
{{^hasModelImport}}#include "{{packageName}}/ModelBase.h"{{/hasModelImport}}
1717
#if defined (_WIN32) || defined (_WIN64)
1818
#undef U
1919
#endif
@@ -52,6 +52,7 @@ public:
5252
static utility::string_t parameterToString(double value);
5353
static utility::string_t parameterToString(const utility::datetime &value);
5454
static utility::string_t parameterToString(bool value);
55+
{{^hasModelImport}}static utility::string_t parameterToString(const ModelBase& value);{{/hasModelImport}}
5556
template<class T>
5657
static utility::string_t parameterToString(const std::vector<T>& value);
5758
template<class T>

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

+7
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ utility::string_t ApiClient::parameterToString(const utility::datetime &value)
7979
return utility::conversions::to_string_t(value.to_string(utility::datetime::ISO_8601));
8080
}
8181

82+
{{^hasModelImport}}
83+
utility::string_t ApiClient::parameterToString(const ModelBase& value)
84+
{
85+
return value.toJson().serialize();
86+
}
87+
{{/hasModelImport}}
88+
8289
utility::string_t ApiClient::parameterToString(bool value)
8390
{
8491
std::stringstream valueAsStringStream;

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

+34
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,30 @@ tags:
1818
- name: user
1919
description: Operations about user
2020
paths:
21+
/pets:
22+
post:
23+
tags:
24+
- pet
25+
summary: List all pets
26+
description: Returns a list of pets
27+
parameters:
28+
- name: page
29+
in: query
30+
description: The page number
31+
required: false
32+
schema:
33+
$ref: '#/components/schemas/Page'
34+
responses:
35+
'200':
36+
description: A list of pets
37+
content:
38+
application/json:
39+
schema:
40+
type: array
41+
items:
42+
$ref: '#/components/schemas/Pet'
43+
'400':
44+
description: Invalid page number
2145
/pet:
2246
post:
2347
tags:
@@ -832,3 +856,13 @@ components:
832856
boosterRequired:
833857
type: boolean
834858
description: true if a booster is still needed to complete the vaccination
859+
Page:
860+
type: object
861+
properties:
862+
page:
863+
type: integer
864+
format: int32
865+
perPage:
866+
type: integer
867+
format: int32
868+

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

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ include/CppRestPetstoreClient/model/Category.h
2222
include/CppRestPetstoreClient/model/Color.h
2323
include/CppRestPetstoreClient/model/CreateUserOrPet_request.h
2424
include/CppRestPetstoreClient/model/Order.h
25+
include/CppRestPetstoreClient/model/Page.h
2526
include/CppRestPetstoreClient/model/Pet.h
2627
include/CppRestPetstoreClient/model/SchemaWithSet.h
2728
include/CppRestPetstoreClient/model/SchemaWithSet_vaccinationBook.h
@@ -46,6 +47,7 @@ src/model/Category.cpp
4647
src/model/Color.cpp
4748
src/model/CreateUserOrPet_request.cpp
4849
src/model/Order.cpp
50+
src/model/Page.cpp
4951
src/model/Pet.cpp
5052
src/model/SchemaWithSet.cpp
5153
src/model/SchemaWithSet_vaccinationBook.cpp

samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/ApiClient.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "CppRestPetstoreClient/ApiException.h"
2424
#include "CppRestPetstoreClient/IHttpBody.h"
2525
#include "CppRestPetstoreClient/HttpContent.h"
26-
26+
#include "CppRestPetstoreClient/ModelBase.h"
2727
#if defined (_WIN32) || defined (_WIN64)
2828
#undef U
2929
#endif
@@ -63,6 +63,7 @@ class ApiClient
6363
static utility::string_t parameterToString(double value);
6464
static utility::string_t parameterToString(const utility::datetime &value);
6565
static utility::string_t parameterToString(bool value);
66+
static utility::string_t parameterToString(const ModelBase& value);
6667
template<class T>
6768
static utility::string_t parameterToString(const std::vector<T>& value);
6869
template<class T>

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

+11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "CppRestPetstoreClient/model/ApiResponse.h"
2626
#include "CppRestPetstoreClient/model/Color.h"
2727
#include "CppRestPetstoreClient/HttpContent.h"
28+
#include "CppRestPetstoreClient/model/Page.h"
2829
#include "CppRestPetstoreClient/model/Pet.h"
2930
#include <vector>
3031
#include <cpprest/details/basic_types.h>
@@ -110,6 +111,16 @@ class PetApi
110111
int64_t petId
111112
) const;
112113
/// <summary>
114+
/// List all pets
115+
/// </summary>
116+
/// <remarks>
117+
/// Returns a list of pets
118+
/// </remarks>
119+
/// <param name="page">The page number (optional, default to nullptr)</param>
120+
pplx::task<std::vector<std::shared_ptr<Pet>>> petsPost(
121+
boost::optional<std::shared_ptr<Page>> page
122+
) const;
123+
/// <summary>
113124
/// Update an existing pet
114125
/// </summary>
115126
/// <remarks>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
* Page.h
14+
*
15+
*
16+
*/
17+
18+
#ifndef ORG_OPENAPITOOLS_CLIENT_MODEL_Page_H_
19+
#define ORG_OPENAPITOOLS_CLIENT_MODEL_Page_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+
32+
class Page
33+
: public ModelBase
34+
{
35+
public:
36+
Page();
37+
virtual ~Page();
38+
39+
/////////////////////////////////////////////
40+
/// ModelBase overrides
41+
42+
void validate() override;
43+
44+
web::json::value toJson() const override;
45+
bool fromJson(const web::json::value& json) override;
46+
47+
void toMultipart(std::shared_ptr<MultipartFormData> multipart, const utility::string_t& namePrefix) const override;
48+
bool fromMultiPart(std::shared_ptr<MultipartFormData> multipart, const utility::string_t& namePrefix) override;
49+
50+
51+
/////////////////////////////////////////////
52+
/// Page members
53+
54+
55+
int32_t getPage() const;
56+
bool pageIsSet() const;
57+
void unsetPage();
58+
void setPage(int32_t value);
59+
60+
int32_t getPerPage() const;
61+
bool perPageIsSet() const;
62+
void unsetPerPage();
63+
void setPerPage(int32_t value);
64+
65+
66+
protected:
67+
int32_t m_Page;
68+
bool m_PageIsSet;
69+
70+
int32_t m_PerPage;
71+
bool m_PerPageIsSet;
72+
73+
};
74+
75+
76+
}
77+
}
78+
}
79+
}
80+
81+
#endif /* ORG_OPENAPITOOLS_CLIENT_MODEL_Page_H_ */

samples/client/petstore/cpp-restsdk/client/src/ApiClient.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ utility::string_t ApiClient::parameterToString(const utility::datetime &value)
9090
return utility::conversions::to_string_t(value.to_string(utility::datetime::ISO_8601));
9191
}
9292

93+
utility::string_t ApiClient::parameterToString(const ModelBase& value)
94+
{
95+
return value.toJson().serialize();
96+
}
97+
9398
utility::string_t ApiClient::parameterToString(bool value)
9499
{
95100
std::stringstream valueAsStringStream;

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

+130
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,136 @@ pplx::task<std::shared_ptr<Pet>> PetApi::getPetById(int64_t petId) const
822822
return localVarResult;
823823
});
824824
}
825+
pplx::task<std::vector<std::shared_ptr<Pet>>> PetApi::petsPost(boost::optional<std::shared_ptr<Page>> page) const
826+
{
827+
828+
829+
std::shared_ptr<const ApiConfiguration> localVarApiConfiguration( m_ApiClient->getConfiguration() );
830+
utility::string_t localVarPath = utility::conversions::to_string_t("/pets");
831+
832+
std::map<utility::string_t, utility::string_t> localVarQueryParams;
833+
std::map<utility::string_t, utility::string_t> localVarHeaderParams( localVarApiConfiguration->getDefaultHeaders() );
834+
std::map<utility::string_t, utility::string_t> localVarFormParams;
835+
std::map<utility::string_t, std::shared_ptr<HttpContent>> localVarFileParams;
836+
837+
std::unordered_set<utility::string_t> localVarResponseHttpContentTypes;
838+
localVarResponseHttpContentTypes.insert( utility::conversions::to_string_t("application/json") );
839+
840+
utility::string_t localVarResponseHttpContentType;
841+
842+
// use JSON if possible
843+
if ( localVarResponseHttpContentTypes.size() == 0 )
844+
{
845+
localVarResponseHttpContentType = utility::conversions::to_string_t("application/json");
846+
}
847+
// JSON
848+
else if ( localVarResponseHttpContentTypes.find(utility::conversions::to_string_t("application/json")) != localVarResponseHttpContentTypes.end() )
849+
{
850+
localVarResponseHttpContentType = utility::conversions::to_string_t("application/json");
851+
}
852+
// multipart formdata
853+
else if( localVarResponseHttpContentTypes.find(utility::conversions::to_string_t("multipart/form-data")) != localVarResponseHttpContentTypes.end() )
854+
{
855+
localVarResponseHttpContentType = utility::conversions::to_string_t("multipart/form-data");
856+
}
857+
else
858+
{
859+
throw ApiException(400, utility::conversions::to_string_t("PetApi->petsPost does not produce any supported media type"));
860+
}
861+
862+
localVarHeaderParams[utility::conversions::to_string_t("Accept")] = localVarResponseHttpContentType;
863+
864+
std::unordered_set<utility::string_t> localVarConsumeHttpContentTypes;
865+
866+
if (page && *page != nullptr)
867+
{
868+
localVarQueryParams[utility::conversions::to_string_t("page")] = ApiClient::parameterToString(*page);
869+
}
870+
871+
std::shared_ptr<IHttpBody> localVarHttpBody;
872+
utility::string_t localVarRequestHttpContentType;
873+
874+
// use JSON if possible
875+
if ( localVarConsumeHttpContentTypes.size() == 0 || localVarConsumeHttpContentTypes.find(utility::conversions::to_string_t("application/json")) != localVarConsumeHttpContentTypes.end() )
876+
{
877+
localVarRequestHttpContentType = utility::conversions::to_string_t("application/json");
878+
}
879+
// multipart formdata
880+
else if( localVarConsumeHttpContentTypes.find(utility::conversions::to_string_t("multipart/form-data")) != localVarConsumeHttpContentTypes.end() )
881+
{
882+
localVarRequestHttpContentType = utility::conversions::to_string_t("multipart/form-data");
883+
}
884+
else if (localVarConsumeHttpContentTypes.find(utility::conversions::to_string_t("application/x-www-form-urlencoded")) != localVarConsumeHttpContentTypes.end())
885+
{
886+
localVarRequestHttpContentType = utility::conversions::to_string_t("application/x-www-form-urlencoded");
887+
}
888+
else
889+
{
890+
throw ApiException(415, utility::conversions::to_string_t("PetApi->petsPost does not consume any supported media type"));
891+
}
892+
893+
894+
return m_ApiClient->callApi(localVarPath, utility::conversions::to_string_t("POST"), localVarQueryParams, localVarHttpBody, localVarHeaderParams, localVarFormParams, localVarFileParams, localVarRequestHttpContentType)
895+
.then([=, this](web::http::http_response localVarResponse)
896+
{
897+
if (m_ApiClient->getResponseHandler())
898+
{
899+
m_ApiClient->getResponseHandler()(localVarResponse.status_code(), localVarResponse.headers());
900+
}
901+
902+
// 1xx - informational : OK
903+
// 2xx - successful : OK
904+
// 3xx - redirection : OK
905+
// 4xx - client error : not OK
906+
// 5xx - client error : not OK
907+
if (localVarResponse.status_code() >= 400)
908+
{
909+
throw ApiException(localVarResponse.status_code()
910+
, utility::conversions::to_string_t("error calling petsPost: ") + localVarResponse.reason_phrase()
911+
, std::make_shared<std::stringstream>(localVarResponse.extract_utf8string(true).get()));
912+
}
913+
914+
// check response content type
915+
if(localVarResponse.headers().has(utility::conversions::to_string_t("Content-Type")))
916+
{
917+
utility::string_t localVarContentType = localVarResponse.headers()[utility::conversions::to_string_t("Content-Type")];
918+
if( localVarContentType.find(localVarResponseHttpContentType) == std::string::npos )
919+
{
920+
throw ApiException(500
921+
, utility::conversions::to_string_t("error calling petsPost: unexpected response type: ") + localVarContentType
922+
, std::make_shared<std::stringstream>(localVarResponse.extract_utf8string(true).get()));
923+
}
924+
}
925+
926+
return localVarResponse.extract_string();
927+
})
928+
.then([=, this](utility::string_t localVarResponse)
929+
{
930+
std::vector<std::shared_ptr<Pet>> localVarResult;
931+
932+
if(localVarResponseHttpContentType == utility::conversions::to_string_t("application/json"))
933+
{
934+
web::json::value localVarJson = web::json::value::parse(localVarResponse);
935+
for( auto& localVarItem : localVarJson.as_array() )
936+
{
937+
std::shared_ptr<Pet> localVarItemObj;
938+
ModelBase::fromJson(localVarItem, localVarItemObj);
939+
localVarResult.push_back(localVarItemObj);
940+
}
941+
}
942+
// else if(localVarResponseHttpContentType == utility::conversions::to_string_t("multipart/form-data"))
943+
// {
944+
// TODO multipart response parsing
945+
// }
946+
else
947+
{
948+
throw ApiException(500
949+
, utility::conversions::to_string_t("error calling petsPost: unsupported response type"));
950+
}
951+
952+
return localVarResult;
953+
});
954+
}
825955
pplx::task<std::shared_ptr<Pet>> PetApi::updatePet(std::shared_ptr<Pet> pet) const
826956
{
827957

0 commit comments

Comments
 (0)