mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-05-12 12:40:53 +00:00
[cpp-rest-sdk] support serializing model base as parameters (#21235)
* [cpp-rest-sdk] support serializing models as parameters This fixes the code generation for the cases where the model is referenced as a parameter * [cpprest-sdk] avoid newlines when no model import
This commit is contained in:
parent
d6c4634269
commit
66e8c932c3
@ -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
|
||||
@ -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>
|
||||
|
@ -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;
|
||||
|
@ -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:
|
||||
@ -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
|
||||
|
||||
|
@ -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
|
||||
@ -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
|
||||
|
@ -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
|
||||
@ -63,6 +63,7 @@ public:
|
||||
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>
|
||||
|
@ -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>
|
||||
@ -110,6 +111,16 @@ public:
|
||||
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>
|
||||
|
@ -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_ */
|
@ -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;
|
||||
|
@ -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
|
||||
{
|
||||
|
||||
|
171
samples/client/petstore/cpp-restsdk/client/src/model/Page.cpp
Normal file
171
samples/client/petstore/cpp-restsdk/client/src/model/Page.cpp
Normal file
@ -0,0 +1,171 @@
|
||||
/**
|
||||
* 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/Page.h"
|
||||
|
||||
namespace org {
|
||||
namespace openapitools {
|
||||
namespace client {
|
||||
namespace model {
|
||||
|
||||
Page::Page()
|
||||
{
|
||||
m_Page = 0;
|
||||
m_PageIsSet = false;
|
||||
m_PerPage = 0;
|
||||
m_PerPageIsSet = false;
|
||||
}
|
||||
|
||||
Page::~Page()
|
||||
{
|
||||
}
|
||||
|
||||
void Page::validate()
|
||||
{
|
||||
// TODO: implement validation
|
||||
}
|
||||
|
||||
web::json::value Page::toJson() const
|
||||
{
|
||||
web::json::value val = web::json::value::object();
|
||||
if(m_PageIsSet)
|
||||
{
|
||||
|
||||
val[utility::conversions::to_string_t(_XPLATSTR("page"))] = ModelBase::toJson(m_Page);
|
||||
}
|
||||
if(m_PerPageIsSet)
|
||||
{
|
||||
|
||||
val[utility::conversions::to_string_t(_XPLATSTR("perPage"))] = ModelBase::toJson(m_PerPage);
|
||||
}
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
bool Page::fromJson(const web::json::value& val)
|
||||
{
|
||||
bool ok = true;
|
||||
if(val.has_field(utility::conversions::to_string_t(_XPLATSTR("page"))))
|
||||
{
|
||||
const web::json::value& fieldValue = val.at(utility::conversions::to_string_t(_XPLATSTR("page")));
|
||||
if(!fieldValue.is_null())
|
||||
{
|
||||
int32_t refVal_setPage;
|
||||
ok &= ModelBase::fromJson(fieldValue, refVal_setPage);
|
||||
setPage(refVal_setPage);
|
||||
|
||||
}
|
||||
}
|
||||
if(val.has_field(utility::conversions::to_string_t(_XPLATSTR("perPage"))))
|
||||
{
|
||||
const web::json::value& fieldValue = val.at(utility::conversions::to_string_t(_XPLATSTR("perPage")));
|
||||
if(!fieldValue.is_null())
|
||||
{
|
||||
int32_t refVal_setPerPage;
|
||||
ok &= ModelBase::fromJson(fieldValue, refVal_setPerPage);
|
||||
setPerPage(refVal_setPerPage);
|
||||
|
||||
}
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
void Page::toMultipart(std::shared_ptr<MultipartFormData> multipart, const utility::string_t& prefix) const
|
||||
{
|
||||
utility::string_t namePrefix = prefix;
|
||||
if(namePrefix.size() > 0 && namePrefix.substr(namePrefix.size() - 1) != utility::conversions::to_string_t(_XPLATSTR(".")))
|
||||
{
|
||||
namePrefix += utility::conversions::to_string_t(_XPLATSTR("."));
|
||||
}
|
||||
if(m_PageIsSet)
|
||||
{
|
||||
multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t(_XPLATSTR("page")), m_Page));
|
||||
}
|
||||
if(m_PerPageIsSet)
|
||||
{
|
||||
multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t(_XPLATSTR("perPage")), m_PerPage));
|
||||
}
|
||||
}
|
||||
|
||||
bool Page::fromMultiPart(std::shared_ptr<MultipartFormData> multipart, const utility::string_t& prefix)
|
||||
{
|
||||
bool ok = true;
|
||||
utility::string_t namePrefix = prefix;
|
||||
if(namePrefix.size() > 0 && namePrefix.substr(namePrefix.size() - 1) != utility::conversions::to_string_t(_XPLATSTR(".")))
|
||||
{
|
||||
namePrefix += utility::conversions::to_string_t(_XPLATSTR("."));
|
||||
}
|
||||
|
||||
if(multipart->hasContent(utility::conversions::to_string_t(_XPLATSTR("page"))))
|
||||
{
|
||||
int32_t refVal_setPage;
|
||||
ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t(_XPLATSTR("page"))), refVal_setPage );
|
||||
setPage(refVal_setPage);
|
||||
}
|
||||
if(multipart->hasContent(utility::conversions::to_string_t(_XPLATSTR("perPage"))))
|
||||
{
|
||||
int32_t refVal_setPerPage;
|
||||
ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t(_XPLATSTR("perPage"))), refVal_setPerPage );
|
||||
setPerPage(refVal_setPerPage);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
|
||||
int32_t Page::getPage() const
|
||||
{
|
||||
return m_Page;
|
||||
}
|
||||
|
||||
void Page::setPage(int32_t value)
|
||||
{
|
||||
m_Page = value;
|
||||
m_PageIsSet = true;
|
||||
}
|
||||
|
||||
bool Page::pageIsSet() const
|
||||
{
|
||||
return m_PageIsSet;
|
||||
}
|
||||
|
||||
void Page::unsetPage()
|
||||
{
|
||||
m_PageIsSet = false;
|
||||
}
|
||||
int32_t Page::getPerPage() const
|
||||
{
|
||||
return m_PerPage;
|
||||
}
|
||||
|
||||
void Page::setPerPage(int32_t value)
|
||||
{
|
||||
m_PerPage = value;
|
||||
m_PerPageIsSet = true;
|
||||
}
|
||||
|
||||
bool Page::perPageIsSet() const
|
||||
{
|
||||
return m_PerPageIsSet;
|
||||
}
|
||||
|
||||
void Page::unsetPerPage()
|
||||
{
|
||||
m_PerPageIsSet = false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user