Skip to content

[cpp-rest-sdk] support serializing model base as parameters #21235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<class T>
static utility::string_t parameterToString(const std::vector<T>& value);
template<class T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<class T>
static utility::string_t parameterToString(const std::vector<T>& value);
template<class T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <vector>
#include <cpprest/details/basic_types.h>
Expand Down Expand Up @@ -110,6 +111,16 @@ class PetApi
int64_t petId
) const;
/// <summary>
/// List all pets
/// </summary>
/// <remarks>
/// Returns a list of pets
/// </remarks>
/// <param name="page">The page number (optional, default to nullptr)</param>
pplx::task<std::vector<std::shared_ptr<Pet>>> petsPost(
boost::optional<std::shared_ptr<Page>> page
) const;
/// <summary>
/// Update an existing pet
/// </summary>
/// <remarks>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<MultipartFormData> multipart, const utility::string_t& namePrefix) const override;
bool fromMultiPart(std::shared_ptr<MultipartFormData> 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_ */
5 changes: 5 additions & 0 deletions samples/client/petstore/cpp-restsdk/client/src/ApiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
130 changes: 130 additions & 0 deletions samples/client/petstore/cpp-restsdk/client/src/api/PetApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,136 @@ pplx::task<std::shared_ptr<Pet>> PetApi::getPetById(int64_t petId) const
return localVarResult;
});
}
pplx::task<std::vector<std::shared_ptr<Pet>>> PetApi::petsPost(boost::optional<std::shared_ptr<Page>> page) const
{


std::shared_ptr<const ApiConfiguration> localVarApiConfiguration( m_ApiClient->getConfiguration() );
utility::string_t localVarPath = utility::conversions::to_string_t("/pets");

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->petsPost does not produce any supported media type"));
}

localVarHeaderParams[utility::conversions::to_string_t("Accept")] = localVarResponseHttpContentType;

std::unordered_set<utility::string_t> localVarConsumeHttpContentTypes;

if (page && *page != nullptr)
{
localVarQueryParams[utility::conversions::to_string_t("page")] = ApiClient::parameterToString(*page);
}

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->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<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 petsPost: 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 petsPost: unsupported response type"));
}

return localVarResult;
});
}
pplx::task<std::shared_ptr<Pet>> PetApi::updatePet(std::shared_ptr<Pet> pet) const
{

Expand Down
Loading
Loading