[CppRest] Add Object and fix modelbase includes (#6905)

* Objects are represented by `ModelBase.h`, not `Object.h` which does not exist.

* Use correct relative imports for files in the `model` and `api` folders.

* [CppRest] Update cpprest petstore client sample.

* Implement `Object` class to support arbitrary types.

* [CppRest] Update cpprest petstore client sample.

* [CppRest] Add newly generated files to petstore client sample.

* [CppRest] Add `Object` to CMakeLists.
This commit is contained in:
François Rosé
2017-11-22 05:19:13 +01:00
committed by William Cheng
parent d9cea0f97e
commit acefe3f385
22 changed files with 293 additions and 13 deletions
@@ -113,6 +113,8 @@ public class CppRestClientCodegen extends AbstractCppCodegen {
supportingFiles.add(new SupportingFile("modelbase-header.mustache", "", "ModelBase.h"));
supportingFiles.add(new SupportingFile("modelbase-source.mustache", "", "ModelBase.cpp"));
supportingFiles.add(new SupportingFile("object-header.mustache", "", "Object.h"));
supportingFiles.add(new SupportingFile("object-source.mustache", "", "Object.cpp"));
supportingFiles.add(new SupportingFile("apiclient-header.mustache", "", "ApiClient.h"));
supportingFiles.add(new SupportingFile("apiclient-source.mustache", "", "ApiClient.cpp"));
supportingFiles.add(new SupportingFile("apiconfiguration-header.mustache", "", "ApiConfiguration.h"));
@@ -9,7 +9,7 @@
#define {{apiHeaderGuardPrefix}}_{{classname}}_H_
{{{defaultInclude}}}
#include "ApiClient.h"
#include "../ApiClient.h"
{{#imports}}{{{import}}}
{{/imports}}
@@ -42,7 +42,7 @@ endif( NOT DEFINED CPPREST_ROOT )
include_directories(${PROJECT_SOURCE_DIR} api model ${CPPREST_INCLUDE_DIR})
#SUPPORTING FILES
set(SUPPORTING_FILES "ApiClient" "ApiConfiguration" "ApiException" "HttpContent" "IHttpBody" "JsonBody" "ModelBase" "MultipartFormData")
set(SUPPORTING_FILES "ApiClient" "ApiConfiguration" "ApiException" "HttpContent" "IHttpBody" "JsonBody" "ModelBase" "MultipartFormData" "Object")
#SOURCE FILES
file(GLOB SOURCE_FILES "api/*" "model/*")
@@ -10,7 +10,7 @@
{{^parent}}
{{{defaultInclude}}}
#include "ModelBase.h"
#include "../ModelBase.h"
{{/parent}}
{{#imports}}{{{this}}}
@@ -66,6 +66,7 @@ public:
static utility::datetime dateFromHttpContent(std::shared_ptr<HttpContent> val);
static bool boolFromHttpContent(std::shared_ptr<HttpContent> val);
static double doubleFromHttpContent(std::shared_ptr<HttpContent> val);
static web::json::value valueFromHttpContent(std::shared_ptr<HttpContent> val);
static utility::string_t toBase64( utility::string_t value );
@@ -355,6 +355,12 @@ double ModelBase::doubleFromHttpContent(std::shared_ptr<HttpContent> val)
return result;
}
web::json::value ModelBase::valueFromHttpContent(std::shared_ptr<HttpContent> val)
{
utility::string_t str = ModelBase::stringFromHttpContent(val);
return web::json::value::parse(str);
}
{{#modelNamespaceDeclarations}}
}
{{/modelNamespaceDeclarations}}
@@ -0,0 +1,50 @@
{{>licenseInfo}}
/*
* Object.h
*
* This is the implementation of a JSON object.
*/
#ifndef {{modelHeaderGuardPrefix}}_Object_H_
#define {{modelHeaderGuardPrefix}}_Object_H_
{{{defaultInclude}}}
#include "ModelBase.h"
#include <cpprest/details/basic_types.h>
#include <cpprest/json.h>
{{#modelNamespaceDeclarations}}
namespace {{this}} {
{{/modelNamespaceDeclarations}}
class {{declspec}} Object : public ModelBase
{
public:
Object();
virtual ~Object();
/////////////////////////////////////////////
/// ModelBase overrides
void validate() override;
web::json::value toJson() const override;
void fromJson(web::json::value& json) override;
void toMultipart(std::shared_ptr<MultipartFormData> multipart, const utility::string_t& namePrefix) const override;
void fromMultiPart(std::shared_ptr<MultipartFormData> multipart, const utility::string_t& namePrefix) override;
/////////////////////////////////////////////
/// Object manipulation
web::json::value getValue(const utility::string_t& key) const;
void setValue(const utility::string_t& key, const web::json::value& value);
private:
web::json::value m_object;
};
{{#modelNamespaceDeclarations}}
}
{{/modelNamespaceDeclarations}}
#endif /* {{modelHeaderGuardPrefix}}_Object_H_ */
@@ -0,0 +1,69 @@
{{>licenseInfo}}
#include "Object.h"
{{#modelNamespaceDeclarations}}
namespace {{this}} {
{{/modelNamespaceDeclarations}}
Object::Object()
{
m_object = web::json::value::object();
}
Object::~Object()
{
}
void Object::validate()
{
// TODO: implement validation
}
web::json::value Object::toJson() const
{
return m_object;
}
void Object::fromJson(web::json::value& val)
{
if (val.is_object())
{
m_object = val;
}
}
void Object::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("."))
{
namePrefix += utility::conversions::to_string_t(".");
}
multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("object"), m_object));
}
void Object::fromMultiPart(std::shared_ptr<MultipartFormData> multipart, const utility::string_t& prefix)
{
utility::string_t namePrefix = prefix;
if(namePrefix.size() > 0 && namePrefix.substr(namePrefix.size() - 1) != utility::conversions::to_string_t("."))
{
namePrefix += utility::conversions::to_string_t(".");
}
m_object = ModelBase::valueFromHttpContent(multipart->getContent(namePrefix + utility::conversions::to_string_t("object")));
}
web::json::value Object::getValue(const utility::string_t& key) const
{
return m_object.at(key);
}
void Object::setValue(const utility::string_t& key, const web::json::value& value)
{
m_object[key] = value;
}
{{#modelNamespaceDeclarations}}
}
{{/modelNamespaceDeclarations}}
@@ -42,7 +42,7 @@ endif( NOT DEFINED CPPREST_ROOT )
include_directories(${PROJECT_SOURCE_DIR} api model ${CPPREST_INCLUDE_DIR})
#SUPPORTING FILES
set(SUPPORTING_FILES "ApiClient" "ApiConfiguration" "ApiException" "HttpContent" "IHttpBody" "JsonBody" "ModelBase" "MultipartFormData")
set(SUPPORTING_FILES "ApiClient" "ApiConfiguration" "ApiException" "HttpContent" "IHttpBody" "JsonBody" "ModelBase" "MultipartFormData" "Object")
#SOURCE FILES
file(GLOB SOURCE_FILES "api/*" "model/*")
@@ -367,6 +367,12 @@ double ModelBase::doubleFromHttpContent(std::shared_ptr<HttpContent> val)
return result;
}
web::json::value ModelBase::valueFromHttpContent(std::shared_ptr<HttpContent> val)
{
utility::string_t str = ModelBase::stringFromHttpContent(val);
return web::json::value::parse(str);
}
}
}
}
@@ -78,6 +78,7 @@ public:
static utility::datetime dateFromHttpContent(std::shared_ptr<HttpContent> val);
static bool boolFromHttpContent(std::shared_ptr<HttpContent> val);
static double doubleFromHttpContent(std::shared_ptr<HttpContent> val);
static web::json::value valueFromHttpContent(std::shared_ptr<HttpContent> val);
static utility::string_t toBase64( utility::string_t value );
@@ -0,0 +1,82 @@
/**
* Swagger Petstore
* This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
* Contact: apiteam@swagger.io
*
* NOTE: This class is auto generated by the swagger code generator 2.3.0-SNAPSHOT.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/
#include "Object.h"
namespace io {
namespace swagger {
namespace client {
namespace model {
Object::Object()
{
m_object = web::json::value::object();
}
Object::~Object()
{
}
void Object::validate()
{
// TODO: implement validation
}
web::json::value Object::toJson() const
{
return m_object;
}
void Object::fromJson(web::json::value& val)
{
if (val.is_object())
{
m_object = val;
}
}
void Object::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("."))
{
namePrefix += utility::conversions::to_string_t(".");
}
multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("object"), m_object));
}
void Object::fromMultiPart(std::shared_ptr<MultipartFormData> multipart, const utility::string_t& prefix)
{
utility::string_t namePrefix = prefix;
if(namePrefix.size() > 0 && namePrefix.substr(namePrefix.size() - 1) != utility::conversions::to_string_t("."))
{
namePrefix += utility::conversions::to_string_t(".");
}
m_object = ModelBase::valueFromHttpContent(multipart->getContent(namePrefix + utility::conversions::to_string_t("object")));
}
web::json::value Object::getValue(const utility::string_t& key) const
{
return m_object.at(key);
}
void Object::setValue(const utility::string_t& key, const web::json::value& value)
{
m_object[key] = value;
}
}
}
}
}
+63
View File
@@ -0,0 +1,63 @@
/**
* Swagger Petstore
* This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
* Contact: apiteam@swagger.io
*
* NOTE: This class is auto generated by the swagger code generator 2.3.0-SNAPSHOT.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/
/*
* Object.h
*
* This is the implementation of a JSON object.
*/
#ifndef _Object_H_
#define _Object_H_
#include "ModelBase.h"
#include <cpprest/details/basic_types.h>
#include <cpprest/json.h>
namespace io {
namespace swagger {
namespace client {
namespace model {
class Object : public ModelBase
{
public:
Object();
virtual ~Object();
/////////////////////////////////////////////
/// ModelBase overrides
void validate() override;
web::json::value toJson() const override;
void fromJson(web::json::value& json) override;
void toMultipart(std::shared_ptr<MultipartFormData> multipart, const utility::string_t& namePrefix) const override;
void fromMultiPart(std::shared_ptr<MultipartFormData> multipart, const utility::string_t& namePrefix) override;
/////////////////////////////////////////////
/// Object manipulation
web::json::value getValue(const utility::string_t& key) const;
void setValue(const utility::string_t& key, const web::json::value& value);
private:
web::json::value m_object;
};
}
}
}
}
#endif /* _Object_H_ */
+1 -1
View File
@@ -20,7 +20,7 @@
#define IO_SWAGGER_CLIENT_API_PetApi_H_
#include "ApiClient.h"
#include "../ApiClient.h"
#include "ApiResponse.h"
#include "HttpContent.h"
@@ -20,7 +20,7 @@
#define IO_SWAGGER_CLIENT_API_StoreApi_H_
#include "ApiClient.h"
#include "../ApiClient.h"
#include "Order.h"
#include <map>
@@ -20,7 +20,7 @@
#define IO_SWAGGER_CLIENT_API_UserApi_H_
#include "ApiClient.h"
#include "../ApiClient.h"
#include "User.h"
#include <vector>
@@ -20,7 +20,7 @@
#define IO_SWAGGER_CLIENT_MODEL_ApiResponse_H_
#include "ModelBase.h"
#include "../ModelBase.h"
#include <cpprest/details/basic_types.h>
@@ -20,7 +20,7 @@
#define IO_SWAGGER_CLIENT_MODEL_Category_H_
#include "ModelBase.h"
#include "../ModelBase.h"
#include <cpprest/details/basic_types.h>
@@ -20,7 +20,7 @@
#define IO_SWAGGER_CLIENT_MODEL_Order_H_
#include "ModelBase.h"
#include "../ModelBase.h"
#include <cpprest/details/basic_types.h>
+1 -1
View File
@@ -20,7 +20,7 @@
#define IO_SWAGGER_CLIENT_MODEL_Pet_H_
#include "ModelBase.h"
#include "../ModelBase.h"
#include "Tag.h"
#include <cpprest/details/basic_types.h>
+1 -1
View File
@@ -20,7 +20,7 @@
#define IO_SWAGGER_CLIENT_MODEL_Tag_H_
#include "ModelBase.h"
#include "../ModelBase.h"
#include <cpprest/details/basic_types.h>
+1 -1
View File
@@ -20,7 +20,7 @@
#define IO_SWAGGER_CLIENT_MODEL_User_H_
#include "ModelBase.h"
#include "../ModelBase.h"
#include <cpprest/details/basic_types.h>