diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCppCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCppCodegen.java index de170293667..5f79d1228ab 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCppCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCppCodegen.java @@ -24,6 +24,7 @@ import io.swagger.v3.oas.models.media.Schema; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; import org.openapitools.codegen.CodegenConfig; +import org.openapitools.codegen.CodegenModel; import org.openapitools.codegen.CodegenProperty; import org.openapitools.codegen.DefaultCodegen; import org.openapitools.codegen.templating.mustache.IndentedLambda; @@ -34,6 +35,7 @@ import org.slf4j.LoggerFactory; import java.io.File; import java.net.URL; import java.util.Arrays; +import java.util.List; import java.util.Map; abstract public class AbstractCppCodegen extends DefaultCodegen implements CodegenConfig { @@ -327,7 +329,17 @@ abstract public class AbstractCppCodegen extends DefaultCodegen implements Codeg } @Override + @SuppressWarnings("unchecked") public Map postProcessModels(Map objs) { + List models = (List) objs.get("models"); + for (Object _mo : models) { + Map mo = (Map) _mo; + CodegenModel cm = (CodegenModel) mo.get("model"); + // cannot handle inheritance from maps and arrays in C++ + if((cm.isArrayModel || cm.isMapModel ) && (cm.parentModel == null)) { + cm.parent = null; + } + } return postProcessModelsEnum(objs); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppRestSdkClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppRestSdkClientCodegen.java index 5475472046c..68065389d47 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppRestSdkClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppRestSdkClientCodegen.java @@ -169,7 +169,7 @@ public class CppRestSdkClientCodegen extends AbstractCppCodegen { typeMapping.put("map", "std::map"); typeMapping.put("file", "HttpContent"); typeMapping.put("object", "Object"); - typeMapping.put("binary", "utility::string_t"); + typeMapping.put("binary", "HttpContent"); typeMapping.put("number", "double"); typeMapping.put("UUID", "utility::string_t"); typeMapping.put("URI", "utility::string_t"); @@ -348,6 +348,8 @@ public class CppRestSdkClientCodegen extends AbstractCppCodegen { } else if (ModelUtils.isMapSchema(p)) { Schema inner = ModelUtils.getAdditionalProperties(p); return getSchemaType(p) + ""; + } else if (ModelUtils.isFileSchema(p) || ModelUtils.isBinarySchema(p)) { + return "std::shared_ptr<" + openAPIType + ">"; } else if (ModelUtils.isStringSchema(p) || ModelUtils.isDateSchema(p) || ModelUtils.isDateTimeSchema(p) || ModelUtils.isFileSchema(p) || ModelUtils.isUUIDSchema(p) @@ -403,9 +405,10 @@ public class CppRestSdkClientCodegen extends AbstractCppCodegen { boolean isPrimitiveType = parameter.isPrimitiveType == Boolean.TRUE; boolean isListContainer = parameter.isListContainer == Boolean.TRUE; + boolean isMapContainer = parameter.isMapContainer == Boolean.TRUE; boolean isString = parameter.isString == Boolean.TRUE; - if (!isPrimitiveType && !isListContainer && !isString && !parameter.dataType.startsWith("std::shared_ptr")) { + if (!isPrimitiveType && !isListContainer && !isMapContainer && !isString && !parameter.dataType.startsWith("std::shared_ptr")) { parameter.dataType = "std::shared_ptr<" + parameter.dataType + ">"; } } diff --git a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/README.mustache b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/README.mustache index 950193f45c8..5f2c07c256f 100644 --- a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/README.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/README.mustache @@ -44,10 +44,10 @@ make - Once visual studio opens, CMake should show up in top menu bar. - Select CMake > Build All. -*Note: If the CMake menu item doesn't show up in Visual Studio, CMake -for Visual Studio must be installed. In this case, open the 'Visual Studio -Installer' application. Select 'modify' Visual Studio 2017. Make sure -'Desktop Development with C++' is installed, and specifically that 'Visual +*Note: If the CMake menu item doesn't show up in Visual Studio, CMake +for Visual Studio must be installed. In this case, open the 'Visual Studio +Installer' application. Select 'modify' Visual Studio 2017. Make sure +'Desktop Development with C++' is installed, and specifically that 'Visual C++ tools for CMake' is selected in the 'Installation Details' section. Also be sure to review the CMakeLists.txt file. Edits are likely required.* diff --git a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/api-header.mustache b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/api-header.mustache index 0b95b6e25aa..76f1bff0955 100644 --- a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/api-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/api-header.mustache @@ -33,7 +33,7 @@ public: {{#operation}} virtual pplx::task<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> {{operationId}}( {{#allParams}} - {{^required}}boost::optional<{{/required}}{{#isFile}}std::shared_ptr<{{/isFile}}{{{dataType}}}{{#isFile}}>{{/isFile}}{{^required}}>{{/required}} {{paramName}}{{#hasMore}},{{/hasMore}} + {{^required}}boost::optional<{{/required}}{{{dataType}}}{{^required}}>{{/required}} {{paramName}}{{#hasMore}},{{/hasMore}} {{/allParams}} ) const = 0; {{/operation}} @@ -67,7 +67,7 @@ public: {{/allParams}} pplx::task<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> {{operationId}}( {{#allParams}} - {{^required}}boost::optional<{{/required}}{{#isFile}}std::shared_ptr<{{/isFile}}{{{dataType}}}{{#isFile}}>{{/isFile}}{{^required}}>{{/required}} {{paramName}}{{#hasMore}},{{/hasMore}} + {{^required}}boost::optional<{{/required}}{{{dataType}}}{{^required}}>{{/required}} {{paramName}}{{#hasMore}},{{/hasMore}} {{/allParams}} ) const{{#gmockApis}} override{{/gmockApis}}; {{/operation}} diff --git a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/api-source.mustache b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/api-source.mustache index 30d2fd7222a..cbd91c2d3a6 100644 --- a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/api-source.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/api-source.mustache @@ -26,7 +26,7 @@ using namespace {{modelNamespace}}; } {{#operation}} -pplx::task<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> {{classname}}::{{operationId}}({{#allParams}}{{^required}}boost::optional<{{/required}}{{#isFile}}std::shared_ptr<{{/isFile}}{{{dataType}}}{{#isFile}}>{{/isFile}}{{^required}}>{{/required}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) const +pplx::task<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> {{classname}}::{{operationId}}({{#allParams}}{{^required}}boost::optional<{{/required}}{{{dataType}}}{{^required}}>{{/required}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) const { {{#allParams}}{{#required}}{{^isPrimitiveType}}{{^isContainer}} // verify the required parameter '{{paramName}}' is set @@ -160,13 +160,13 @@ pplx::task<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/r web::json::value localVarJson; {{#isPrimitiveType}} - localVarJson = ModelBase::toJson({{paramName}}); + localVarJson = ModelBase::toJson({{paramName}}{{^required}}.get(){{/required}}); {{/isPrimitiveType}} {{^isPrimitiveType}} {{#isListContainer}} { std::vector localVarJsonArray; - for( auto& localVarItem : {{paramName}} ) + for( auto& localVarItem : {{paramName}}{{^required}}.get(){{/required}} ) { {{#items.isPrimitiveType}}localVarJsonArray.push_back(ModelBase::toJson(localVarItem)); {{/items.isPrimitiveType}}{{^items.isPrimitiveType}}{{#items.isString}}localVarJsonArray.push_back(ModelBase::toJson(localVarItem)); @@ -193,30 +193,32 @@ pplx::task<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/r {{#bodyParam}} std::shared_ptr localVarMultipart(new MultipartFormData); {{#isPrimitiveType}} - localVarMultipart->add(ModelBase::toHttpContent(utility::conversions::to_string_t("{{paramName}}"), {{paramName}})); - {{/isPrimitiveType}} - {{^isPrimitiveType}} - {{#isListContainer}} + localVarMultipart->add(ModelBase::toHttpContent(utility::conversions::to_string_t("{{paramName}}"), {{paramName}}{{^required}}.get(){{/required}})); + {{/isPrimitiveType}}{{^isPrimitiveType}}{{#isListContainer}} { std::vector localVarJsonArray; - for( auto& localVarItem : {{paramName}} ) + for( auto& localVarItem : {{paramName}}{{^required}}.get(){{/required}} ) { - {{#items.isPrimitiveType}}localVarJsonArray.push_back(ModelBase::toJson(localVarItem)); - {{/items.isPrimitiveType}}{{^items.isPrimitiveType}}{{#items.isString}}localVarJsonArray.push_back(ModelBase::toJson(localVarItem)); - {{/items.isString}}{{^items.isString}}{{#items.isDateTime}}localVarJsonArray.push_back(ModelBase::toJson(localVarItem)); - {{/items.isDateTime}}{{^items.isDateTime}}localVarJsonArray.push_back( localVarItem.get() ? localVarItem->toJson() : web::json::value::null() ); - {{/items.isDateTime}}{{/items.isString}}{{/items.isPrimitiveType}} + localVarJsonArray.push_back(ModelBase::toJson(localVarItem)); } - localVarMultipart->add(ModelBase::toHttpContent(utility::conversions::to_string_t("{{paramName}}"), web::json::value::array(localVarJsonArray), utility::conversions::to_string_t("application/json"))); - }{{/isListContainer}} - {{^isListContainer}}{{#isString}}localVarMultipart->add(ModelBase::toHttpContent(utility::conversions::to_string_t("{{paramName}}"), {{paramName}})); + localVarMultipart->add(ModelBase::toHttpContent(utility::conversions::to_string_t("{{paramName}}"), localVarJsonArray, utility::conversions::to_string_t("application/json"))); + }{{/isListContainer}}{{#isMapContainer}} + { + std::map localVarJsonMap; + for( auto& localVarItem : {{paramName}}{{^required}}.get(){{/required}} ) + { + web::json::value jval; + localVarJsonMap.insert( std::pair(localVarItem.first, ModelBase::toJson(localVarItem.second) )); + } + localVarMultipart->add(ModelBase::toHttpContent(utility::conversions::to_string_t("{{paramName}}"), localVarJsonMap, utility::conversions::to_string_t("application/json"))); + }{{/isMapContainer}} + {{^isListContainer}}{{^isMapContainer}}{{#isString}}localVarMultipart->add(ModelBase::toHttpContent(utility::conversions::to_string_t("{{paramName}}"), {{paramName}})); {{/isString}}{{^isString}}if({{^required}}{{paramName}} && (*{{paramName}}){{/required}}{{#required}}{{paramName}}{{/required}}.get()) { {{^required}}(*{{/required}}{{paramName}}{{^required}}){{/required}}->toMultipart(localVarMultipart, utility::conversions::to_string_t("{{paramName}}")); } {{/isString}} - {{/isListContainer}} - {{/isPrimitiveType}} + {{/isMapContainer}}{{/isListContainer}}{{/isPrimitiveType}} localVarHttpBody = localVarMultipart; localVarRequestHttpContentType += utility::conversions::to_string_t("; boundary=") + localVarMultipart->getBoundary(); @@ -318,29 +320,20 @@ pplx::task<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/r if(localVarResponseHttpContentType == utility::conversions::to_string_t("application/json")) { web::json::value localVarJson = web::json::value::parse(localVarResponse); - - {{#isListContainer}}for( auto& localVarItem : localVarJson.as_array() ) + {{#isListContainer}} + for( auto& localVarItem : localVarJson.as_array() ) { - {{#vendorExtensions.x-codegen-response.items.isPrimitiveType}}localVarResult.push_back(ModelBase::{{vendorExtensions.x-codegen-response.items.datatype}}FromJson(localVarItem)); - {{/vendorExtensions.x-codegen-response.items.isPrimitiveType}}{{^vendorExtensions.x-codegen-response.items.isPrimitiveType}}{{#vendorExtensions.x-codegen-response.items.isString}}localVarResult.push_back(ModelBase::stringFromJson(localVarItem)); - {{/vendorExtensions.x-codegen-response.items.isString}}{{^vendorExtensions.x-codegen-response.items.isString}}{{{vendorExtensions.x-codegen-response.items.datatype}}} localVarItemObj({{{vendorExtensions.x-codegen-response.items.defaultValue}}}); - localVarItemObj->fromJson(localVarItem); + {{{vendorExtensions.x-codegen-response.items.datatype}}} localVarItemObj; + ModelBase::fromJson(localVarItem, localVarItemObj); localVarResult.push_back(localVarItemObj); - {{/vendorExtensions.x-codegen-response.items.isString}}{{/vendorExtensions.x-codegen-response.items.isPrimitiveType}} - } - {{/isListContainer}}{{^isListContainer}}{{#isMapContainer}}for( auto& localVarItem : localVarJson.as_object() ) + }{{/isListContainer}}{{#isMapContainer}} + for( auto& localVarItem : localVarJson.as_object() ) { - {{#vendorExtensions.x-codegen-response.items.isPrimitiveType}}localVarResult[localVarItem.first] = ModelBase::{{vendorExtensions.x-codegen-response.items.datatype}}FromJson(localVarItem.second); - {{/vendorExtensions.x-codegen-response.items.isPrimitiveType}}{{^vendorExtensions.x-codegen-response.items.isPrimitiveType}}{{#vendorExtensions.x-codegen-response.items.isString}}localVarResult[localVarItem.first] = ModelBase::stringFromJson(localVarItem.second); - {{/vendorExtensions.x-codegen-response.items.isString}}{{^vendorExtensions.x-codegen-response.items.isString}}{{{vendorExtensions.x-codegen-response.items.datatype}}} localVarItemObj({{{vendorExtensions.x-codegen-response.items.defaultValue}}}); - localVarItemObj->fromJson(localVarItem.second); + {{{vendorExtensions.x-codegen-response.items.datatype}}} localVarItemObj; + ModelBase::fromJson(localVarItem.second, localVarItemObj); localVarResult[localVarItem.first] = localVarItemObj; - {{/vendorExtensions.x-codegen-response.items.isString}}{{/vendorExtensions.x-codegen-response.items.isPrimitiveType}} - } - {{/isMapContainer}}{{^isMapContainer}}{{#vendorExtensions.x-codegen-response.isPrimitiveType}}{{#vendorExtensions.x-codegen-response.items.datatype}}localVarResult = ModelBase::{{vendorExtensions.x-codegen-response.items.datatype}}FromJson(localVarJson); - {{/vendorExtensions.x-codegen-response.items.datatype}}{{^vendorExtensions.x-codegen-response.items.datatype}}localVarResult = ModelBase::{{vendorExtensions.x-codegen-response.datatype}}FromJson(localVarJson); - {{/vendorExtensions.x-codegen-response.items.datatype}}{{/vendorExtensions.x-codegen-response.isPrimitiveType}}{{^vendorExtensions.x-codegen-response.isPrimitiveType}}{{#vendorExtensions.x-codegen-response.isString}}localVarResult = ModelBase::stringFromJson(localVarJson); - {{/vendorExtensions.x-codegen-response.isString}}{{^vendorExtensions.x-codegen-response.isString}}localVarResult->fromJson(localVarJson);{{/vendorExtensions.x-codegen-response.isString}}{{/vendorExtensions.x-codegen-response.isPrimitiveType}}{{/isMapContainer}}{{/isListContainer}} + }{{/isMapContainer}}{{^isListContainer}}{{^isMapContainer}} + ModelBase::fromJson(localVarJson, localVarResult);{{/isMapContainer}}{{/isListContainer}} }{{#vendorExtensions.x-codegen-response.isString}} else if(localVarResponseHttpContentType == utility::conversions::to_string_t("text/plain")) { diff --git a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/httpcontent-header.mustache b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/httpcontent-header.mustache index 59cf42ae8c0..f6cfbc3dd02 100644 --- a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/httpcontent-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/httpcontent-header.mustache @@ -24,19 +24,19 @@ public: HttpContent(); virtual ~HttpContent(); - virtual utility::string_t getContentDisposition(); + virtual utility::string_t getContentDisposition() const; virtual void setContentDisposition( const utility::string_t& value ); - virtual utility::string_t getName(); + virtual utility::string_t getName() const; virtual void setName( const utility::string_t& value ); - virtual utility::string_t getFileName(); + virtual utility::string_t getFileName() const; virtual void setFileName( const utility::string_t& value ); - virtual utility::string_t getContentType(); + virtual utility::string_t getContentType() const; virtual void setContentType( const utility::string_t& value ); - virtual std::shared_ptr getData(); + virtual std::shared_ptr getData() const; virtual void setData( std::shared_ptr value ); virtual void writeTo( std::ostream& stream ); diff --git a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/httpcontent-source.mustache b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/httpcontent-source.mustache index 791515a205d..df0cb475e14 100644 --- a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/httpcontent-source.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/httpcontent-source.mustache @@ -13,7 +13,7 @@ HttpContent::~HttpContent() { } -utility::string_t HttpContent::getContentDisposition() +utility::string_t HttpContent::getContentDisposition() const { return m_ContentDisposition; } @@ -23,7 +23,7 @@ void HttpContent::setContentDisposition( const utility::string_t & value ) m_ContentDisposition = value; } -utility::string_t HttpContent::getName() +utility::string_t HttpContent::getName() const { return m_Name; } @@ -33,7 +33,7 @@ void HttpContent::setName( const utility::string_t & value ) m_Name = value; } -utility::string_t HttpContent::getFileName() +utility::string_t HttpContent::getFileName() const { return m_FileName; } @@ -43,7 +43,7 @@ void HttpContent::setFileName( const utility::string_t & value ) m_FileName = value; } -utility::string_t HttpContent::getContentType() +utility::string_t HttpContent::getContentType() const { return m_ContentType; } @@ -53,7 +53,7 @@ void HttpContent::setContentType( const utility::string_t & value ) m_ContentType = value; } -std::shared_ptr HttpContent::getData() +std::shared_ptr HttpContent::getData() const { return m_Data; } diff --git a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/model-header.mustache b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/model-header.mustache index a1141597d0e..e8321341c07 100644 --- a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/model-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/model-header.mustache @@ -34,12 +34,12 @@ public: void validate() override; web::json::value toJson() const override; - void fromJson(const web::json::value& json) override; + bool fromJson(const web::json::value& json) override; void toMultipart(std::shared_ptr multipart, const utility::string_t& namePrefix) const override; - void fromMultiPart(std::shared_ptr multipart, const utility::string_t& namePrefix) override; + bool fromMultiPart(std::shared_ptr multipart, const utility::string_t& namePrefix) override; - enum class e{{classname}} + enum class e{{classname}} { {{#allowableValues}} {{#enumVars}} @@ -48,8 +48,8 @@ public: /// {{enumDescription}} /// {{/enumDescription}} - {{classname}}_{{{name}}}{{^last}},{{/last}} - {{/enumVars}} + {{classname}}_{{{name}}}{{^last}},{{/last}} + {{/enumVars}} {{/allowableValues}} }; @@ -78,10 +78,10 @@ public: void validate() override; web::json::value toJson() const override; - void fromJson(const web::json::value& json) override; + bool fromJson(const web::json::value& json) override; void toMultipart(std::shared_ptr multipart, const utility::string_t& namePrefix) const override; - void fromMultiPart(std::shared_ptr multipart, const utility::string_t& namePrefix) override; + bool fromMultiPart(std::shared_ptr multipart, const utility::string_t& namePrefix) override; ///////////////////////////////////////////// /// {{classname}} members @@ -93,9 +93,8 @@ public: /// {{#isContainer}}{{{dataType}}}& {{getter}}(); {{/isContainer}}{{^isContainer}}{{{dataType}}} {{getter}}() const; - {{/isContainer}}{{^required}}bool {{nameInCamelCase}}IsSet() const; + {{/isContainer}}bool {{nameInCamelCase}}IsSet() const; void unset{{name}}(); - {{/required}} {{#isPrimitiveType}} void {{setter}}({{{dataType}}} value); @@ -111,8 +110,7 @@ protected: {{#vars}} {{^isInherited}} {{{dataType}}} m_{{name}}; - {{^required}}bool m_{{name}}IsSet; - {{/required}} + bool m_{{name}}IsSet; {{/isInherited}} {{/vars}} }; diff --git a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/model-source.mustache b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/model-source.mustache index 1faa870e708..41eb29a5610 100644 --- a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/model-source.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/model-source.mustache @@ -33,12 +33,13 @@ web::json::value {{classname}}::toJson() const return val; } -void {{classname}}::fromJson(const web::json::value& val) +bool {{classname}}::fromJson(const web::json::value& val) { auto s = val.as_string(); {{#allowableValues}}{{#enumVars}} if (s == utility::conversions::to_string_t({{{value}}})) m_value = e{{classname}}::{{classname}}_{{name}};{{/enumVars}}{{/allowableValues}} + return true; } void {{classname}}::toMultipart(std::shared_ptr multipart, const utility::string_t& prefix) const @@ -57,24 +58,25 @@ void {{classname}}::toMultipart(std::shared_ptr multipart, co multipart->add(ModelBase::toHttpContent(namePrefix, s)); } -void {{classname}}::fromMultiPart(std::shared_ptr multipart, const utility::string_t& prefix) +bool {{classname}}::fromMultiPart(std::shared_ptr 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(".")) { namePrefix += utility::conversions::to_string_t("."); } - { utility::string_t s; - s = ModelBase::stringFromHttpContent(multipart->getContent(namePrefix)); + ok = ModelBase::fromHttpContent(multipart->getContent(namePrefix), s); e{{classname}} v; - + {{#allowableValues}}{{#enumVars}} if (s == utility::conversions::to_string_t({{{value}}})) v = e{{classname}}::{{classname}}_{{name}};{{/enumVars}}{{/allowableValues}} setValue(v); } + return ok; } {{classname}}::e{{classname}} {{classname}}::getValue() const @@ -108,9 +110,7 @@ void {{classname}}::setValue({{classname}}::e{{classname}} const value) {{/isDateTime}} {{/isPrimitiveType}} {{/isContainer}} - {{^required}} m_{{name}}IsSet = false; - {{/required}} {{/isInherited}} {{/vars}} } @@ -127,263 +127,37 @@ void {{classname}}::validate() web::json::value {{classname}}::toJson() const { {{#parent}} - web::json::value val = this->{{{parent}}}::toJson(); - {{/parent}} + web::json::value val = this->{{{parent}}}::toJson();{{/parent}} {{^parent}} web::json::value val = web::json::value::object(); {{/parent}} - - {{#vars}} - {{^isInherited}} - {{#isPrimitiveType}} - {{^isListContainer}} - {{^isMapContainer}} - {{^required}} + {{#vars}}{{^isInherited}} if(m_{{name}}IsSet) { val[utility::conversions::to_string_t("{{baseName}}")] = ModelBase::toJson(m_{{name}}); - } - {{/required}} - {{#required}} - val[utility::conversions::to_string_t("{{baseName}}")] = ModelBase::toJson(m_{{name}}); - {{/required}} - {{/isMapContainer}} - {{/isListContainer}} - {{/isPrimitiveType}} - {{#isListContainer}} - { - std::vector jsonArray; - for( auto& item : m_{{name}} ) - { - jsonArray.push_back(ModelBase::toJson(item)); - } - {{#required}} - val[utility::conversions::to_string_t("{{baseName}}")] = web::json::value::array(jsonArray); - {{/required}} - {{^required}} - if(jsonArray.size() > 0) - { - val[utility::conversions::to_string_t("{{baseName}}")] = web::json::value::array(jsonArray); - } - {{/required}} - } - {{/isListContainer}} - {{#isMapContainer}} - { - std::vector jsonArray; - for( auto& item : m_{{name}} ) - { - web::json::value tmp = web::json::value::object(); - tmp[utility::conversions::to_string_t("key")] = ModelBase::toJson(item.first); - tmp[utility::conversions::to_string_t("value")] = ModelBase::toJson(item.second); - jsonArray.push_back(tmp); - } - {{#required}} - val[utility::conversions::to_string_t("{{baseName}}")] = web::json::value::array(jsonArray); - {{/required}} - {{^required}} - if(jsonArray.size() > 0) - { - val[utility::conversions::to_string_t("{{baseName}}")] = web::json::value::array(jsonArray); - } - {{/required}} - } - {{/isMapContainer}} - {{^isListContainer}} - {{^isMapContainer}} - {{^isPrimitiveType}} - {{^required}} - if(m_{{name}}IsSet) - { - val[utility::conversions::to_string_t("{{baseName}}")] = ModelBase::toJson(m_{{name}}); - } - {{/required}} - {{#required}} - val[utility::conversions::to_string_t("{{baseName}}")] = ModelBase::toJson(m_{{name}}); - {{/required}} - {{/isPrimitiveType}} - {{/isMapContainer}} - {{/isListContainer}} - {{/isInherited}} - {{/vars}} + }{{/isInherited}}{{/vars}} return val; } -void {{classname}}::fromJson(const web::json::value& val) +bool {{classname}}::fromJson(const web::json::value& val) { + bool ok = true; {{#parent}} - this->{{{parent}}}::fromJson(val); - + ok &= this->{{{parent}}}::fromJson(val); {{/parent}} - {{#vars}} - {{^isInherited}} - {{#isPrimitiveType}} - {{^isListContainer}} - {{^isMapContainer}} - {{^required}} + {{#vars}}{{^isInherited}} if(val.has_field(utility::conversions::to_string_t("{{baseName}}"))) { const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("{{baseName}}")); if(!fieldValue.is_null()) { - {{setter}}(ModelBase::{{baseType}}FromJson(fieldValue)); + {{{dataType}}} refVal_{{baseName}}; + ok &= ModelBase::fromJson(fieldValue, refVal_{{baseName}}); + {{setter}}(refVal_{{baseName}}); } - } - {{/required}} - {{#required}} - {{setter}}(ModelBase::{{baseType}}FromJson(val.at(utility::conversions::to_string_t("{{baseName}}")))); - {{/required}} - {{/isMapContainer}} - {{/isListContainer}} - {{/isPrimitiveType}} - {{#isListContainer}} - { - m_{{name}}.clear(); - std::vector jsonArray; - {{^required}} - if(val.has_field(utility::conversions::to_string_t("{{baseName}}"))) - { - {{/required}} - for( auto& item : val.at(utility::conversions::to_string_t("{{baseName}}")).as_array() ) - { - {{#items.isPrimitiveType}} - m_{{name}}.push_back(ModelBase::{{items.baseType}}FromJson(item)); - {{/items.isPrimitiveType}} - {{^items.isPrimitiveType}} - {{#items.isString}} - m_{{name}}.push_back(ModelBase::stringFromJson(item)); - {{/items.isString}} - {{^items.isString}} - {{#items.isDateTime}} - m_{{name}}.push_back(ModelBase::dateFromJson(item)); - {{/items.isDateTime}} - {{^items.isDateTime}} - if(item.is_null()) - { - m_{{name}}.push_back( {{{items.datatype}}}(nullptr) ); - } - else - { - auto newItem = std::make_shared<{{{items.datatype}}}::element_type>(); - newItem->fromJson(item); - m_{{name}}.push_back( newItem ); - } - {{/items.isDateTime}} - {{/items.isString}} - {{/items.isPrimitiveType}} - } - {{^required}} - } - {{/required}} - } - {{/isListContainer}} - {{#isMapContainer}} - { - m_{{name}}.clear(); - std::vector jsonArray; - {{^required}} - if(val.has_field(utility::conversions::to_string_t("{{baseName}}"))) - { - {{/required}} - for( const auto& item : val.at(utility::conversions::to_string_t("{{baseName}}")).as_array() ) - { - if(item.has_field(utility::conversions::to_string_t("key"))) - { - utility::string_t key = ModelBase::stringFromJson(item.at(utility::conversions::to_string_t("key"))); - {{#items.isPrimitiveType}} - m_{{name}}.insert(std::pair( key, ModelBase::{{items.baseType}}FromJson(item.at(utility::conversions::to_string_t("value"))))); - {{/items.isPrimitiveType}} - {{^items.isPrimitiveType}} - {{#items.isString}} - m_{{name}}.insert(std::pair( key, ModelBase::stringFromJson(item.at(utility::conversions::to_string_t("value"))))); - {{/items.isString}} - {{^items.isString}} - {{#items.isDateTime}} - m_{{name}}.insert(std::pair( key, ModelBase::dateFromJson(item.at(utility::conversions::to_string_t("value"))))); - {{/items.isDateTime}} - {{^items.isDateTime}} - if(item.is_null()) - { - m_{{name}}.insert(std::pair( key, {{{items.datatype}}}(nullptr) )); - } - else - { - auto newItem = std::make_shared<{{{items.datatype}}}::element_type>(); - newItem->fromJson(item.at(utility::conversions::to_string_t("value"))); - m_{{name}}.insert(std::pair( key, newItem )); - } - {{/items.isDateTime}} - {{/items.isString}} - {{/items.isPrimitiveType}} - } - } - {{^required}} - } - {{/required}} - } - {{/isMapContainer}} - {{^isListContainer}} - {{^isMapContainer}} - {{^isPrimitiveType}} - {{^required}} - if(val.has_field(utility::conversions::to_string_t("{{baseName}}"))) - { - const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("{{baseName}}")); - if(!fieldValue.is_null()) - { - {{#isString}} - {{setter}}(ModelBase::stringFromJson(fieldValue)); - {{/isString}} - {{#isByteArray}} - {{setter}}(ModelBase::stringFromJson(fieldValue)); - {{/isByteArray}} - {{^isString}} - {{#isDateTime}} - {{setter}}(ModelBase::dateFromJson(fieldValue)); - {{/isDateTime}} - {{^isDateTime}} - {{^isByteArray}} - auto newItem = std::make_shared<{{{datatype}}}::element_type>(); - newItem->fromJson(fieldValue); - {{setter}}( newItem ); - {{/isByteArray}} - {{/isDateTime}} - {{/isString}} - } - } - {{/required}} - {{#required}} - {{#isString}} - {{setter}}(ModelBase::stringFromJson(val.at(utility::conversions::to_string_t("{{baseName}}")))); - {{/isString}} - {{#isByteArray}} - {{setter}}(ModelBase::stringFromJson(val[utility::conversions::to_string_t("{{baseName}}")])); - {{/isByteArray}} - {{^isString}} - {{^isByteArray}} - {{#isDateTime}} - {{setter}} - (ModelBase::dateFromJson(val.at(utility::conversions::to_string_t("{{baseName}}")))); - {{/isDateTime}} - {{^isDateTime}} - {{#vendorExtensions.x-codegen-file}} - {{setter}}(ModelBase::fileFromJson(val.at(utility::conversions::to_string_t("{{baseName}}")))); - {{/vendorExtensions.x-codegen-file}} - {{^vendorExtensions.x-codegen-file}} - auto new{{name}} = std::make_shared<{{{dataType}}}::element_type>(); - new{{name}}->fromJson(val.at(utility::conversions::to_string_t("{{baseName}}"))); - {{setter}}( new{{name}} ); - {{/vendorExtensions.x-codegen-file}} - {{/isDateTime}} - {{/isByteArray}} - {{/isString}} - {{/required}} - {{/isPrimitiveType}} - {{/isMapContainer}} - {{/isListContainer}} - {{/isInherited}} - {{/vars}} + }{{/isInherited}}{{/vars}} + return ok; } void {{classname}}::toMultipart(std::shared_ptr multipart, const utility::string_t& prefix) const @@ -393,115 +167,17 @@ void {{classname}}::toMultipart(std::shared_ptr multipart, co { namePrefix += utility::conversions::to_string_t("."); } - {{#vars}} - {{#isPrimitiveType}} - {{^isMapContainer}} - {{^isListContainer}} - {{^required}} if(m_{{name}}IsSet) { multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("{{baseName}}"), m_{{name}})); } - {{/required}} - {{#required}} - multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("{{baseName}}"), m_{{name}})); - {{/required}} - {{/isListContainer}} - {{/isMapContainer}} - {{/isPrimitiveType}} - {{#isListContainer}} - { - std::vector jsonArray; - for( auto& item : m_{{name}} ) - { - jsonArray.push_back(ModelBase::toJson(item)); - } - {{#required}}multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("{{baseName}}"), web::json::value::array(jsonArray), utility::conversions::to_string_t("application/json"))); - {{/required}}{{^required}} - if(jsonArray.size() > 0) - { - multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("{{baseName}}"), web::json::value::array(jsonArray), utility::conversions::to_string_t("application/json"))); - } - {{/required}} - } - {{/isListContainer}} - {{#isMapContainer}} - { - std::vector jsonArray; - for( auto& item : m_{{name}} ) - { - web::json::value tmp = web::json::value::object(); - tmp[utility::conversions::to_string_t("key")] = ModelBase::toJson(item.first); - tmp[utility::conversions::to_string_t("value")] = ModelBase::toJson(item.second); - jsonArray.push_back(tmp); - } - {{#required}}multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("{{baseName}}"), web::json::value::array(jsonArray), utility::conversions::to_string_t("application/json"))); - {{/required}}{{^required}} - if(jsonArray.size() > 0) - { - multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("{{baseName}}"), web::json::value::array(jsonArray), utility::conversions::to_string_t("application/json"))); - } - {{/required}} - } - {{/isMapContainer}} - {{^isListContainer}} - {{^isMapContainer}} - {{^isPrimitiveType}} - {{^required}} - if(m_{{name}}IsSet) - { - {{#isString}} - multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("{{baseName}}"), m_{{name}})); - {{/isString}} - {{#isByteArray}} - multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("{{baseName}}"), m_{{name}})); - {{/isByteArray}} - {{^isString}} - {{#isDateTime}} - multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("{{baseName}}"), m_{{name}})); - {{/isDateTime}} - {{^isDateTime}} - {{^isByteArray}}if (m_{{name}}.get()) - { - m_{{name}}->toMultipart(multipart, utility::conversions::to_string_t("{{baseName}}.")); - } - {{/isByteArray}} - {{/isDateTime}} - {{/isString}} - } - {{/required}} - {{#required}} - {{#isString}} - multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("{{baseName}}"), m_{{name}})); - {{/isString}} - {{#isByteArray}} - multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("{{baseName}}"), m_{{name}})); - {{/isByteArray}} - {{^isString}} - {{#isDateTime}} - multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("{{baseName}}"), m_{{name}})); - {{/isDateTime}} - {{^isDateTime}} - {{^isByteArray}} - {{#vendorExtensions.x-codegen-file}} - multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("{{baseName}}"), m_{{name}})); - {{/vendorExtensions.x-codegen-file}} - {{^vendorExtensions.x-codegen-file}} - m_{{name}}->toMultipart(multipart, utility::conversions::to_string_t("{{baseName}}.")); - {{/vendorExtensions.x-codegen-file}} - {{/isByteArray}} - {{/isDateTime}} - {{/isString}} - {{/required}} - {{/isPrimitiveType}} - {{/isMapContainer}} - {{/isListContainer}} {{/vars}} } -void {{classname}}::fromMultiPart(std::shared_ptr multipart, const utility::string_t& prefix) +bool {{classname}}::fromMultiPart(std::shared_ptr 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(".")) { @@ -509,168 +185,14 @@ void {{classname}}::fromMultiPart(std::shared_ptr multipart, } {{#vars}} - {{#isPrimitiveType}} - {{^isListContainer}} - {{^isMapContainer}} - {{^required}} if(multipart->hasContent(utility::conversions::to_string_t("{{baseName}}"))) { - {{setter}}(ModelBase::{{baseType}}FromHttpContent(multipart->getContent(utility::conversions::to_string_t("{{baseName}}")))); + {{{dataType}}} refVal_{{baseName}}; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("{{baseName}}")), refVal_{{baseName}} ); + {{setter}}(refVal_{{baseName}}); } - {{/required}} - {{#required}} - {{setter}}(ModelBase::{{baseType}}FromHttpContent(multipart->getContent(utility::conversions::to_string_t("{{baseName}}")))); - {{/required}} - {{/isMapContainer}} - {{/isListContainer}} - {{/isPrimitiveType}} - {{#isListContainer}} - { - m_{{name}}.clear(); - {{^required}} - if(multipart->hasContent(utility::conversions::to_string_t("{{baseName}}"))) - { - {{/required}} - - web::json::value jsonArray = web::json::value::parse(ModelBase::stringFromHttpContent(multipart->getContent(utility::conversions::to_string_t("{{baseName}}")))); - for( auto& item : jsonArray.as_array() ) - { - {{#isPrimitiveType}} - m_{{name}}.push_back(ModelBase::{{items.baseType}}FromJson(item)); - {{/isPrimitiveType}} - {{^isPrimitiveType}} - {{#items.isString}} - m_{{name}}.push_back(ModelBase::stringFromJson(item)); - {{/items.isString}} - {{^items.isString}} - {{#items.isDateTime}} - m_{{name}}.push_back(ModelBase::dateFromJson(item)); - {{/items.isDateTime}} - {{^items.isDateTime}} - if(item.is_null()) - { - m_{{name}}.push_back( {{{items.datatype}}}(nullptr) ); - } - else - { - auto newItem = std::make_shared<{{{items.datatype}}}::element_type>(); - newItem->fromJson(item); - m_{{name}}.push_back( newItem ); - } - {{/items.isDateTime}} - {{/items.isString}} - {{/isPrimitiveType}} - } - {{^required}} - } - {{/required}} - } - {{/isListContainer}} - {{#isMapContainer}} - { - m_{{name}}.clear(); - {{^required}} - if(multipart->hasContent(utility::conversions::to_string_t("{{baseName}}"))) - { - {{/required}} - - web::json::value jsonArray = web::json::value::parse(ModelBase::stringFromHttpContent(multipart->getContent(utility::conversions::to_string_t("{{baseName}}")))); - for( auto& item : jsonArray.as_array() ) - { - utility::string_t key; - if(item.has_field(utility::conversions::to_string_t("key"))) - { - key = ModelBase::stringFromJson(item[utility::conversions::to_string_t("key")]); - } - {{#items.isPrimitiveType}} - m_{{name}}.insert(std::pair( key, ModelBase::{{items.baseType}}FromJson(item[utility::conversions::to_string_t("value")]))); - {{/items.isPrimitiveType}} - {{^items.isPrimitiveType}} - {{#items.isString}} - m_{{name}}.insert(std::pair( key, ModelBase::stringFromJson(item[utility::conversions::to_string_t("value")]))); - {{/items.isString}} - {{^items.isString}} - {{#items.isDateTime}} - m_{{name}}.insert(std::pair( key, ModelBase::dateFromJson(item[utility::conversions::to_string_t("value")]))); - {{/items.isDateTime}} - {{^items.isDateTime}} - if(item.is_null()) - { - m_{{name}}.insert(std::pair( key, {{{items.datatype}}}(nullptr) )); - } - else - { - auto newItem = std::make_shared<{{{items.datatype}}}::element_type>(); - newItem->fromJson(item[utility::conversions::to_string_t("value")]); - m_{{name}}.insert(std::pair( key, newItem )); - } - {{/items.isDateTime}} - {{/items.isString}} - {{/items.isPrimitiveType}} - } - {{^required}} - } - {{/required}} - } - {{/isMapContainer}} - {{^isListContainer}} - {{^isMapContainer}} - {{^isPrimitiveType}} - {{^required}} - if(multipart->hasContent(utility::conversions::to_string_t("{{baseName}}"))) - { - {{#isString}} - {{setter}}(ModelBase::stringFromHttpContent(multipart->getContent(utility::conversions::to_string_t("{{baseName}}")))); - {{/isString}} - {{#isByteArray}} - {{setter}}(ModelBase::stringFromHttpContent(multipart->getContent(utility::conversions::to_string_t("{{baseName}}")))); - {{/isByteArray}} - {{^isString}} - {{^isByteArray}} - {{#isDateTime}} - {{setter}}(ModelBase::dateFromHttpContent(multipart->getContent(utility::conversions::to_string_t("{{baseName}}")))); - {{/isDateTime}} - {{^isDateTime}} - if(multipart->hasContent(utility::conversions::to_string_t("{{baseName}}"))) - { - auto newItem = std::make_shared<{{{datatype}}}::element_type>(); - newItem->fromMultiPart(multipart, utility::conversions::to_string_t("{{baseName}}.")); - {{setter}}( newItem ); - } - {{/isDateTime}} - {{/isByteArray}} - {{/isString}} - } - {{/required}} - {{#required}} - {{#isString}} - {{setter}}(ModelBase::stringFromHttpContent(multipart->getContent(utility::conversions::to_string_t("{{baseName}}")))); - {{/isString}} - {{#isByteArray}} - {{setter}}(ModelBase::stringFromHttpContent(multipart->getContent(utility::conversions::to_string_t("{{baseName}}")))); - {{/isByteArray}} - {{^isString}} - {{^isByteArray}} - {{#isDateTime}} - {{setter}}(ModelBase::dateFromHttpContent(multipart->getContent(utility::conversions::to_string_t("{{baseName}}")))); - {{/isDateTime}} - {{^isDateTime}} - {{#vendorExtensions.x-codegen-file}} - {{setter}}(multipart->getContent(utility::conversions::to_string_t("{{baseName}}"))); - {{/vendorExtensions.x-codegen-file}} - {{^vendorExtensions.x-codegen-file}} - auto new{{name}} = std::make_shared<{{{dataType}}}::element_type>(); - new{{name}}->fromMultiPart(multipart, utility::conversions::to_string_t("{{baseName}}.")); - {{setter}}( new{{name}} ); - {{/vendorExtensions.x-codegen-file}} - {{/isDateTime}} - {{/isByteArray}} - {{/isString}} - {{/required}} - {{/isPrimitiveType}} - {{/isMapContainer}} - {{/isListContainer}} {{/vars}} + return ok; } {{#vars}} @@ -696,10 +218,9 @@ void {{classname}}::{{setter}}(const {{{dataType}}}& value) {{/isPrimitiveType}} { m_{{name}} = value; - {{^required}}m_{{name}}IsSet = true;{{/required}} + m_{{name}}IsSet = true; } -{{^required}} bool {{classname}}::{{nameInCamelCase}}IsSet() const { return m_{{name}}IsSet; @@ -709,8 +230,6 @@ void {{classname}}::unset{{name}}() { m_{{name}}IsSet = false; } - -{{/required}} {{/isInherited}} {{/vars}} {{/isEnum}} diff --git a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/modelbase-header.mustache b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/modelbase-header.mustache index c88ab1dfe63..a887e56fc1b 100644 --- a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/modelbase-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/modelbase-header.mustache @@ -9,14 +9,15 @@ #define {{modelHeaderGuardPrefix}}_ModelBase_H_ {{{defaultInclude}}} +#include +#include + #include "HttpContent.h" #include "MultipartFormData.h" #include #include -#include - {{#modelNamespaceDeclarations}} namespace {{this}} { {{/modelNamespaceDeclarations}} @@ -30,68 +31,252 @@ public: virtual void validate() = 0; virtual web::json::value toJson() const = 0; - virtual void fromJson(const web::json::value& json) = 0; + virtual bool fromJson( const web::json::value& json ) = 0; - virtual void toMultipart(std::shared_ptr multipart, const utility::string_t& namePrefix) const = 0; - virtual void fromMultiPart(std::shared_ptr multipart, const utility::string_t& namePrefix) = 0; + virtual void toMultipart( std::shared_ptr multipart, const utility::string_t& namePrefix ) const = 0; + virtual bool fromMultiPart( std::shared_ptr multipart, const utility::string_t& namePrefix ) = 0; - static web::json::value toJson( const utility::string_t& value ); - static web::json::value toJson( const utility::datetime& value ); - static web::json::value toJson( std::shared_ptr value ); - static web::json::value toJson( std::shared_ptr value ); - static web::json::value toJson( int32_t value ); - static web::json::value toJson( int64_t value ); - static web::json::value toJson( double value ); - static web::json::value toJson( bool value ); - template - static web::json::value toJson(const std::vector& value); + virtual bool isSet() const; - static int64_t int64_tFromJson(const web::json::value& val); - static int32_t int32_tFromJson(const web::json::value& val); - static float floatFromJson(const web::json::value& val); - static utility::string_t stringFromJson(const web::json::value& val); - static utility::datetime dateFromJson(const web::json::value& val); - static double doubleFromJson(const web::json::value& val); - static bool boolFromJson(const web::json::value& val); - static std::shared_ptr fileFromJson(const web::json::value& val); + static utility::string_t toString( const bool val ); + static utility::string_t toString( const float val ); + static utility::string_t toString( const double val ); + static utility::string_t toString( const int32_t val ); + static utility::string_t toString( const int64_t val ); + static utility::string_t toString( const utility::string_t &val ); + static utility::string_t toString( const utility::datetime &val ); + static utility::string_t toString( const web::json::value &val ); + static utility::string_t toString( const std::shared_ptr& val ); + template + static utility::string_t toString( const std::shared_ptr& val ); + template + static utility::string_t toString( const std::vector & val ); - static std::shared_ptr toHttpContent( const utility::string_t& name, const utility::string_t& value, const utility::string_t& contentType = utility::conversions::to_string_t("")); - static std::shared_ptr toHttpContent( const utility::string_t& name, const utility::datetime& value, const utility::string_t& contentType = utility::conversions::to_string_t("")); - static std::shared_ptr toHttpContent( const utility::string_t& name, std::shared_ptr value ); - static std::shared_ptr toHttpContent( const utility::string_t& name, const web::json::value& value, const utility::string_t& contentType = utility::conversions::to_string_t("application/json") ); + static web::json::value toJson( bool val ); + static web::json::value toJson( float val ); + static web::json::value toJson( double val ); + static web::json::value toJson( int32_t val ); + static web::json::value toJson( int64_t val ); + static web::json::value toJson( const utility::string_t& val ); + static web::json::value toJson( const utility::datetime& val ); + static web::json::value toJson( const web::json::value& val ); + static web::json::value toJson( const std::shared_ptr& val ); + template + static web::json::value toJson( const std::shared_ptr& val ); + template + static web::json::value toJson( const std::vector& val ); + template + static web::json::value toJson( const std::map& val ); + + static bool fromString( const utility::string_t& val, bool & ); + static bool fromString( const utility::string_t& val, float & ); + static bool fromString( const utility::string_t& val, double & ); + static bool fromString( const utility::string_t& val, int32_t & ); + static bool fromString( const utility::string_t& val, int64_t & ); + static bool fromString( const utility::string_t& val, utility::string_t & ); + static bool fromString( const utility::string_t& val, utility::datetime & ); + static bool fromString( const utility::string_t& val, web::json::value & ); + static bool fromString( const utility::string_t& val, std::shared_ptr & ); + template + static bool fromString( const utility::string_t& val, std::shared_ptr& ); + template + static bool fromString( const utility::string_t& val, std::vector & ); + template + static bool fromString( const utility::string_t& val, std::map & ); + + static bool fromJson( const web::json::value& val, bool & ); + static bool fromJson( const web::json::value& val, float & ); + static bool fromJson( const web::json::value& val, double & ); + static bool fromJson( const web::json::value& val, int32_t & ); + static bool fromJson( const web::json::value& val, int64_t & ); + static bool fromJson( const web::json::value& val, utility::string_t & ); + static bool fromJson( const web::json::value& val, utility::datetime & ); + static bool fromJson( const web::json::value& val, web::json::value & ); + static bool fromJson( const web::json::value& val, std::shared_ptr & ); + template + static bool fromJson( const web::json::value& val, std::shared_ptr& ); + template + static bool fromJson( const web::json::value& val, std::vector & ); + template + static bool fromJson( const web::json::value& val, std::map & ); + + + static std::shared_ptr toHttpContent( const utility::string_t& name, bool value, const utility::string_t& contentType = utility::conversions::to_string_t("") ); + static std::shared_ptr toHttpContent( const utility::string_t& name, float value, const utility::string_t& contentType = utility::conversions::to_string_t("") ); + static std::shared_ptr toHttpContent( const utility::string_t& name, double value, const utility::string_t& contentType = utility::conversions::to_string_t("") ); static std::shared_ptr toHttpContent( const utility::string_t& name, int32_t value, const utility::string_t& contentType = utility::conversions::to_string_t("") ); static std::shared_ptr toHttpContent( const utility::string_t& name, int64_t value, const utility::string_t& contentType = utility::conversions::to_string_t("") ); - static std::shared_ptr toHttpContent( const utility::string_t& name, double value, const utility::string_t& contentType = utility::conversions::to_string_t("") ); - template + static std::shared_ptr toHttpContent( const utility::string_t& name, const utility::string_t& value, const utility::string_t& contentType = utility::conversions::to_string_t("")); + static std::shared_ptr toHttpContent( const utility::string_t& name, const utility::datetime& value, const utility::string_t& contentType = utility::conversions::to_string_t("")); + static std::shared_ptr toHttpContent( const utility::string_t& name, const web::json::value& value, const utility::string_t& contentType = utility::conversions::to_string_t("application/json") ); + static std::shared_ptr toHttpContent( const utility::string_t& name, const std::shared_ptr& ); + template + static std::shared_ptr toHttpContent( const utility::string_t& name, const std::shared_ptr& , const utility::string_t& contentType = utility::conversions::to_string_t("application/json") ); + template static std::shared_ptr toHttpContent( const utility::string_t& name, const std::vector& value, const utility::string_t& contentType = utility::conversions::to_string_t("") ); + template + static std::shared_ptr toHttpContent( const utility::string_t& name, const std::map& value, const utility::string_t& contentType = utility::conversions::to_string_t("") ); - static int64_t int64_tFromHttpContent(std::shared_ptr val); - static int32_t int32_tFromHttpContent(std::shared_ptr val); - static float floatFromHttpContent(std::shared_ptr val); - static utility::string_t stringFromHttpContent(std::shared_ptr val); - static utility::datetime dateFromHttpContent(std::shared_ptr val); - static bool boolFromHttpContent(std::shared_ptr val); - static double doubleFromHttpContent(std::shared_ptr val); - static web::json::value valueFromHttpContent(std::shared_ptr val); - + static bool fromHttpContent( std::shared_ptr val, bool & ); + static bool fromHttpContent( std::shared_ptr val, float & ); + static bool fromHttpContent( std::shared_ptr val, double & ); + static bool fromHttpContent( std::shared_ptr val, int64_t & ); + static bool fromHttpContent( std::shared_ptr val, int32_t & ); + static bool fromHttpContent( std::shared_ptr val, utility::string_t & ); + static bool fromHttpContent( std::shared_ptr val, utility::datetime & ); + static bool fromHttpContent( std::shared_ptr val, web::json::value & ); + static bool fromHttpContent( std::shared_ptr val, std::shared_ptr& ); + template + static bool fromHttpContent( std::shared_ptr val, std::shared_ptr& ); + template + static bool fromHttpContent( std::shared_ptr val, std::vector & ); + template + static bool fromHttpContent( std::shared_ptr val, std::map & ); static utility::string_t toBase64( utility::string_t value ); static utility::string_t toBase64( std::shared_ptr value ); static std::shared_ptr fromBase64( const utility::string_t& encoded ); +protected: + bool m_IsSet; }; -template -web::json::value ModelBase::toJson(const std::vector& value) { - std::vector ret; - for (auto& x : value) { - ret.push_back(toJson(x)); +template +utility::string_t ModelBase::toString( const std::shared_ptr& val ) +{ + utility::stringstream_t ss; + if( val != nullptr ) + { + val->toJson().serialize(ss); + } + return utility::string_t(ss.str()); +} +template +utility::string_t ModelBase::toString( const std::vector & val ) +{ + utility::string_t strArray; + for ( const auto &item : val ) + { + strArray.append( toString(item) + "," ); + } + if (val.count() > 0) + { + strArray.pop_back(); + } + return strArray; +} +template +web::json::value ModelBase::toJson( const std::shared_ptr& val ) +{ + web::json::value retVal; + if(val != nullptr) + { + retVal = val->toJson(); + } + return retVal; +} +template +web::json::value ModelBase::toJson( const std::vector& value ) +{ + std::vector ret; + for ( const auto& x : value ) + { + ret.push_back( toJson(x) ); } - return web::json::value::array(ret); } - -template -std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, const std::vector& value, const utility::string_t& contentType ) { +template +web::json::value ModelBase::toJson( const std::map& val ) +{ + web::json::value obj; + for ( const auto &itemkey : val ) + { + obj[itemkey.first] = toJson( itemkey.second ); + } + return obj; +} +template +bool ModelBase::fromString( const utility::string_t& val, std::shared_ptr& outVal ) +{ + bool ok = false; + if(outVal == nullptr) + { + outVal = std::shared_ptr(new T()); + } + if( outVal != nullptr ) + { + ok = outVal->fromJson(web::json::value::parse(val)); + } + return ok; +} +template +bool ModelBase::fromJson( const web::json::value& val, std::shared_ptr &outVal ) +{ + bool ok = false; + if(outVal == nullptr) + { + outVal = std::shared_ptr(new T()); + } + if( outVal != nullptr ) + { + ok = outVal->fromJson(val); + } + return ok; +} +template +bool ModelBase::fromJson( const web::json::value& val, std::vector &outVal ) +{ + bool ok = true; + if (val.is_array()) + { + for (const auto jitem : val.as_array()) + { + T item; + ok &= fromJson(jitem, item); + outVal.push_back(item); + } + } + else + { + ok = false; + } + return ok; +} +template +bool ModelBase::fromJson( const web::json::value& jval, std::map &outVal ) +{ + bool ok = true; + if ( jval.is_object() ) + { + auto obj = jval.as_object(); + for( auto objItr = obj.begin() ; objItr != obj.end() ; objItr++ ) + { + T itemVal; + ok &= fromJson(objItr->second, itemVal); + outVal.insert(std::pair(objItr->first, itemVal)); + } + } + else + { + ok = false; + } + return ok; +} +template +std::shared_ptr ModelBase::toHttpContent(const utility::string_t& name, const std::shared_ptr& value , const utility::string_t& contentType ) +{ + std::shared_ptr content( new HttpContent ); + if (value != nullptr ) + { + content->setName( name ); + content->setContentDisposition( utility::conversions::to_string_t("form-data") ); + content->setContentType( contentType ); + content->setData( std::shared_ptr( new std::stringstream( utility::conversions::to_utf8string( value->toJson().serialize() ) ) ) ); + } + return content; +} +template +std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, const std::vector& value, const utility::string_t& contentType ) +{ web::json::value json_array = ModelBase::toJson(value); std::shared_ptr content( new HttpContent ); content->setName( name ); @@ -100,7 +285,39 @@ std::shared_ptr ModelBase::toHttpContent( const utility::string_t& content->setData( std::shared_ptr( new std::stringstream( utility::conversions::to_utf8string(json_array.serialize()) ) ) ); return content; } - +template +std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, const std::map& value, const utility::string_t& contentType ) +{ + web::json::value jobj = ModelBase::toJson(value); + std::shared_ptr content( new HttpContent ); + content->setName( name ); + content->setContentDisposition( utility::conversions::to_string_t("form-data") ); + content->setContentType( contentType ); + content->setData( std::shared_ptr( new std::stringstream( utility::conversions::to_utf8string(jobj.serialize()) ) ) ); + return content; +} +template +bool ModelBase::fromHttpContent( std::shared_ptr val, std::shared_ptr& outVal ) +{ + utility::string_t str; + if(val == nullptr) return false; + if( outVal == nullptr ) + { + outVal = std::shared_ptr(new T()); + } + ModelBase::fromHttpContent(val, str); + return fromString(str, outVal); +} +template +bool ModelBase::fromHttpContent( std::shared_ptr val, std::vector & ) +{ + return true; +} +template +bool ModelBase::fromHttpContent( std::shared_ptr val, std::map & ) +{ + return true; +} {{#modelNamespaceDeclarations}} } {{/modelNamespaceDeclarations}} diff --git a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/modelbase-source.mustache b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/modelbase-source.mustache index f4548d75605..a103cd77763 100644 --- a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/modelbase-source.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/modelbase-source.mustache @@ -5,20 +5,80 @@ namespace {{this}} { {{/modelNamespaceDeclarations}} -ModelBase::ModelBase() +ModelBase::ModelBase(): m_IsSet(false) { } ModelBase::~ModelBase() { } - -web::json::value ModelBase::toJson( const utility::string_t& value ) +bool ModelBase::isSet() const { - return web::json::value::string(value); + return m_IsSet; } -web::json::value ModelBase::toJson( const utility::datetime& value ) +utility::string_t ModelBase::toString( const bool val ) { - return web::json::value::string(value.to_string(utility::datetime::ISO_8601)); + utility::stringstream_t ss; + ss << val; + return utility::string_t(ss.str()); +} +utility::string_t ModelBase::toString( const float val ) +{ + utility::stringstream_t ss; + ss << val; + return utility::string_t(ss.str()); +} +utility::string_t ModelBase::toString( const double val ) +{ + utility::stringstream_t ss; + ss << val; + return utility::string_t(ss.str()); +} +utility::string_t ModelBase::toString( const int32_t val ) +{ + utility::stringstream_t ss; + ss << val; + return utility::string_t(ss.str()); +} +utility::string_t ModelBase::toString( const int64_t val ) +{ + utility::stringstream_t ss; + ss << val; + return utility::string_t(ss.str()); +} +utility::string_t ModelBase::toString (const utility::string_t &val ) +{ + utility::stringstream_t ss; + ss << val; + return utility::string_t(ss.str()); +} +utility::string_t ModelBase::toString( const utility::datetime &val ) +{ + return val.to_string(utility::datetime::ISO_8601); +} +utility::string_t ModelBase::toString( const web::json::value &val ) +{ + return val.serialize(); +} +utility::string_t ModelBase::toString( const std::shared_ptr& val ) +{ + utility::stringstream_t ss; + if( val != nullptr ) + { + ss << val->getData(); + } + return utility::string_t(ss.str()); +} +web::json::value ModelBase::toJson(bool value) +{ + return web::json::value::boolean(value); +} +web::json::value ModelBase::toJson( float value ) +{ + return web::json::value::number(value); +} +web::json::value ModelBase::toJson( double value ) +{ + return web::json::value::number(value); } web::json::value ModelBase::toJson( int32_t value ) { @@ -28,88 +88,259 @@ web::json::value ModelBase::toJson( int64_t value ) { return web::json::value::number(value); } -web::json::value ModelBase::toJson( double value ) +web::json::value ModelBase::toJson( const utility::string_t& value ) { - return web::json::value::number(value); -} -web::json::value ModelBase::toJson(bool value) { - return web::json::value::boolean(value); + return web::json::value::string(value); } - -web::json::value ModelBase::toJson( std::shared_ptr content ) +web::json::value ModelBase::toJson( const utility::datetime& value ) +{ + return web::json::value::string(value.to_string(utility::datetime::ISO_8601)); +} +web::json::value ModelBase::toJson( const web::json::value& value ) { - web::json::value value; - value[utility::conversions::to_string_t("ContentDisposition")] = ModelBase::toJson(content->getContentDisposition()); - value[utility::conversions::to_string_t("ContentType")] = ModelBase::toJson(content->getContentType()); - value[utility::conversions::to_string_t("FileName")] = ModelBase::toJson(content->getFileName()); - value[utility::conversions::to_string_t("InputStream")] = web::json::value::string( ModelBase::toBase64(content->getData()) ); return value; } - -std::shared_ptr ModelBase::fileFromJson(const web::json::value& val) +web::json::value ModelBase::toJson( const std::shared_ptr& content ) { - std::shared_ptr content(new HttpContent); - - if(val.has_field(utility::conversions::to_string_t("ContentDisposition"))) + web::json::value value; + if(content != nullptr) { - content->setContentDisposition( ModelBase::stringFromJson(val.at(utility::conversions::to_string_t("ContentDisposition"))) ); + value[utility::conversions::to_string_t("ContentDisposition")] = ModelBase::toJson(content->getContentDisposition()); + value[utility::conversions::to_string_t("ContentType")] = ModelBase::toJson(content->getContentType()); + value[utility::conversions::to_string_t("FileName")] = ModelBase::toJson(content->getFileName()); + value[utility::conversions::to_string_t("InputStream")] = web::json::value::string( ModelBase::toBase64(content->getData()) ); } - if(val.has_field(utility::conversions::to_string_t("ContentType"))) - { - content->setContentType( ModelBase::stringFromJson(val.at(utility::conversions::to_string_t("ContentType"))) ); - } - if(val.has_field(utility::conversions::to_string_t("FileName"))) - { - content->setFileName( ModelBase::stringFromJson(val.at(utility::conversions::to_string_t("FileName"))) ); - } - if(val.has_field(utility::conversions::to_string_t("InputStream"))) - { - content->setData( ModelBase::fromBase64( ModelBase::stringFromJson(val.at(utility::conversions::to_string_t("InputStream")))) ); - } - - return content; + return value; } - -web::json::value ModelBase::toJson( std::shared_ptr content ) +bool ModelBase::fromString( const utility::string_t& val, bool &outVal ) { - return content.get() ? content->toJson() : web::json::value::null(); + utility::stringstream_t ss(val); + bool success = true; + try + { + ss >> outVal; + } + catch (...) + { + success = false; + } + return success; } - -std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, const utility::string_t& value, const utility::string_t& contentType) +bool ModelBase::fromString( const utility::string_t& val, float &outVal ) { - std::shared_ptr content(new HttpContent); - content->setName( name ); - content->setContentDisposition( utility::conversions::to_string_t("form-data") ); - content->setContentType( contentType ); - content->setData( std::shared_ptr( new std::stringstream( utility::conversions::to_utf8string(value) ) ) ); - return content; + utility::stringstream_t ss(val); + bool success = true; + try + { + ss >> outVal; + } + catch (...) + { + success = false; + } + return success; } -std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, const utility::datetime& value, const utility::string_t& contentType ) +bool ModelBase::fromString( const utility::string_t& val, double &outVal ) +{ + utility::stringstream_t ss(val); + bool success = true; + try + { + ss >> outVal; + } + catch (...) + { + success = false; + } + return success; +} +bool ModelBase::fromString( const utility::string_t& val, int32_t &outVal ) +{ + utility::stringstream_t ss(val); + bool success = true; + try + { + ss >> outVal; + } + catch (...) + { + success = false; + } + return success; +} +bool ModelBase::fromString( const utility::string_t& val, int64_t &outVal ) +{ + utility::stringstream_t ss(val); + bool success = true; + try + { + ss >> outVal; + } + catch (...) + { + success = false; + } + return success; +} +bool ModelBase::fromString( const utility::string_t& val, utility::string_t &outVal ) +{ + utility::stringstream_t ss(val); + bool success = true; + try + { + ss >> outVal; + } + catch (...) + { + success = false; + } + return success; +} +bool ModelBase::fromString( const utility::string_t& val, utility::datetime &outVal ) +{ + bool success = true; + auto dt = utility::datetime::from_string(val, utility::datetime::ISO_8601); + if( dt.is_initialized() ) + { + outVal = dt; + } + else + { + success = false; + } + return success; +} +bool ModelBase::fromString( const utility::string_t& val, web::json::value &outVal ) +{ + outVal = web::json::value::parse(val); + return !outVal.is_null(); +} +bool ModelBase::fromString( const utility::string_t& val, std::shared_ptr& outVal ) +{ + bool ok = true; + if(outVal == nullptr) + { + outVal = std::shared_ptr(new HttpContent()); + } + std::shared_ptr ssptr = std::make_shared(val); + if(outVal != nullptr) + { + outVal->setData(ssptr); + } + else + { + ok = false; + } + return ok; +} +bool ModelBase::fromJson( const web::json::value& val, bool & outVal ) +{ + outVal = !val.is_boolean() ? false : val.as_bool(); + return val.is_boolean(); +} +bool ModelBase::fromJson( const web::json::value& val, float & outVal ) +{ + outVal = !val.is_double() ? std::numeric_limits::quiet_NaN(): static_cast(val.as_double()); + return val.is_double(); +} +bool ModelBase::fromJson( const web::json::value& val, double & outVal ) +{ + outVal = !val.is_double() ? std::numeric_limits::quiet_NaN(): val.as_double(); + return val.is_double() ; +} +bool ModelBase::fromJson( const web::json::value& val, int32_t & outVal ) +{ + outVal = !val.is_integer() ? std::numeric_limits::quiet_NaN() : val.as_integer(); + return val.is_integer(); +} +bool ModelBase::fromJson( const web::json::value& val, int64_t & outVal ) +{ + outVal = !val.is_null() ? std::numeric_limits::quiet_NaN() : val.as_number().to_int64(); + return val.is_number(); +} +bool ModelBase::fromJson( const web::json::value& val, utility::string_t & outVal ) +{ + outVal = val.is_string() ? val.as_string() : utility::conversions::to_string_t(""); + return val.is_string(); +} +bool ModelBase::fromJson( const web::json::value& val, utility::datetime & outVal ) +{ + outVal = val.is_null() ? utility::datetime::from_string(utility::conversions::to_string_t("NULL"), utility::datetime::ISO_8601) : utility::datetime::from_string(val.as_string(), utility::datetime::ISO_8601); + return outVal.is_initialized(); +} +bool ModelBase::fromJson( const web::json::value& val, web::json::value & outVal ) +{ + outVal = val; + return !val.is_null(); +} +bool ModelBase::fromJson( const web::json::value& val, std::shared_ptr& content ) +{ + bool result = false; + if( content != nullptr) + { + result = true; + if(content == nullptr) + { + content = std::shared_ptr(new HttpContent()); + } + if(val.has_field(utility::conversions::to_string_t("ContentDisposition"))) + { + utility::string_t value; + result = result && ModelBase::fromJson(val.at(utility::conversions::to_string_t("ContentDisposition")), value); + content->setContentDisposition( value ); + } + if(val.has_field(utility::conversions::to_string_t("ContentType"))) + { + utility::string_t value; + result = result && ModelBase::fromJson(val.at(utility::conversions::to_string_t("ContentType")), value); + content->setContentType( value ); + } + if(val.has_field(utility::conversions::to_string_t("FileName"))) + { + utility::string_t value; + result = result && ModelBase::fromJson(val.at(utility::conversions::to_string_t("FileName")), value); + content->setFileName( value ); + } + if(val.has_field(utility::conversions::to_string_t("InputStream"))) + { + utility::string_t value; + result = result && ModelBase::fromJson(val.at(utility::conversions::to_string_t("InputStream")), value); + content->setData( ModelBase::fromBase64( value ) ); + } + } + return result; +} +std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, bool value, const utility::string_t& contentType ) { std::shared_ptr content( new HttpContent ); content->setName( name ); content->setContentDisposition( utility::conversions::to_string_t("form-data") ); content->setContentType( contentType ); - content->setData( std::shared_ptr( new std::stringstream( utility::conversions::to_utf8string(value.to_string(utility::datetime::ISO_8601) ) ) ) ); + std::stringstream* valueAsStringStream = new std::stringstream(); + (*valueAsStringStream) << value; + content->setData( std::shared_ptr( valueAsStringStream ) ); return content; } -std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, std::shared_ptr value ) -{ - std::shared_ptr content( new HttpContent ); - content->setName( name ); - content->setContentDisposition( value->getContentDisposition() ); - content->setContentType( value->getContentType() ); - content->setData( value->getData() ); - content->setFileName( value->getFileName() ); - return content; -} -std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, const web::json::value& value, const utility::string_t& contentType ) +std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, float value, const utility::string_t& contentType ) { std::shared_ptr content( new HttpContent ); content->setName( name ); content->setContentDisposition( utility::conversions::to_string_t("form-data") ); content->setContentType( contentType ); - content->setData( std::shared_ptr( new std::stringstream( utility::conversions::to_utf8string(value.serialize()) ) ) ); + std::stringstream* valueAsStringStream = new std::stringstream(); + (*valueAsStringStream) << value; + content->setData( std::shared_ptr( valueAsStringStream ) ); + return content; +} +std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, double value, const utility::string_t& contentType ) +{ + std::shared_ptr content( new HttpContent ); + content->setName( name ); + content->setContentDisposition( utility::conversions::to_string_t("form-data") ); + content->setContentType( contentType ); + std::stringstream* valueAsStringStream = new std::stringstream(); + (*valueAsStringStream) << value; + content->setData( std::shared_ptr( valueAsStringStream ) ); return content; } std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, int32_t value, const utility::string_t& contentType ) @@ -134,18 +365,118 @@ std::shared_ptr ModelBase::toHttpContent( const utility::string_t& content->setData( std::shared_ptr( valueAsStringStream) ) ; return content; } -std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, double value, const utility::string_t& contentType ) +std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, const utility::string_t& value, const utility::string_t& contentType) +{ + std::shared_ptr content(new HttpContent); + content->setName( name ); + content->setContentDisposition( utility::conversions::to_string_t("form-data") ); + content->setContentType( contentType ); + content->setData( std::shared_ptr( new std::stringstream( utility::conversions::to_utf8string(value) ) ) ); + return content; +} +std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, const utility::datetime& value, const utility::string_t& contentType ) { std::shared_ptr content( new HttpContent ); content->setName( name ); content->setContentDisposition( utility::conversions::to_string_t("form-data") ); content->setContentType( contentType ); - std::stringstream* valueAsStringStream = new std::stringstream(); - (*valueAsStringStream) << value; - content->setData( std::shared_ptr( valueAsStringStream ) ); + content->setData( std::shared_ptr( new std::stringstream( utility::conversions::to_utf8string(value.to_string(utility::datetime::ISO_8601) ) ) ) ); return content; } +std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, const web::json::value& value, const utility::string_t& contentType ) +{ + std::shared_ptr content( new HttpContent ); + content->setName( name ); + content->setContentDisposition( utility::conversions::to_string_t("form-data") ); + content->setContentType( contentType ); + content->setData( std::shared_ptr( new std::stringstream( utility::conversions::to_utf8string(value.serialize()) ) ) ); + return content; +} +std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, const std::shared_ptr& value ) +{ + std::shared_ptr content( new HttpContent ); + if( value != nullptr ) + { + content->setName( name ); + content->setContentDisposition( value->getContentDisposition() ); + content->setContentType( value->getContentType() ); + content->setData( value->getData() ); + content->setFileName( value->getFileName() ); + } + return content; +} +bool ModelBase::fromHttpContent(std::shared_ptr val, bool & outVal ) +{ + utility::string_t str; + if( val == nullptr ) return false; + ModelBase::fromHttpContent(val, str); + return fromString(str, outVal); +} +bool ModelBase::fromHttpContent(std::shared_ptr val, float & outVal ) +{ + utility::string_t str; + if( val == nullptr ) return false; + ModelBase::fromHttpContent(val, str); + return fromString(str, outVal); +} +bool ModelBase::fromHttpContent(std::shared_ptr val, double & outVal ) +{ + utility::string_t str; + if( val == nullptr ) return false; + ModelBase::fromHttpContent(val, str); + return fromString(str, outVal); +} +bool ModelBase::fromHttpContent(std::shared_ptr val, int32_t & outVal ) +{ + utility::string_t str; + if( val == nullptr ) return false; + ModelBase::fromHttpContent(val, str); + return fromString(str, outVal); +} +bool ModelBase::fromHttpContent(std::shared_ptr val, int64_t & outVal ) +{ + utility::string_t str; + if( val == nullptr ) return false; + ModelBase::fromHttpContent(val, str); + return fromString(str, outVal); +} +bool ModelBase::fromHttpContent(std::shared_ptr val, utility::string_t & outVal ) +{ + if( val == nullptr ) return false; + std::shared_ptr data = val->getData(); + data->seekg( 0, data->beg ); + std::string str((std::istreambuf_iterator(*data.get())), + std::istreambuf_iterator()); + outVal = utility::conversions::to_string_t(str); + return true; +} +bool ModelBase::fromHttpContent(std::shared_ptr val, utility::datetime & outVal ) +{ + utility::string_t str; + if( val == nullptr ) return false; + ModelBase::fromHttpContent(val, str); + outVal = utility::datetime::from_string(str, utility::datetime::ISO_8601); + return true; +} +bool ModelBase::fromHttpContent(std::shared_ptr val, web::json::value & outVal ) +{ + utility::string_t str; + if( val == nullptr ) return false; + ModelBase::fromHttpContent(val, str); + return fromString(str, outVal); +} +bool ModelBase::fromHttpContent(std::shared_ptr val, std::shared_ptr& outVal ) +{ + utility::string_t str; + if( val == nullptr ) return false; + if( outVal == nullptr ) + { + outVal = std::shared_ptr(new HttpContent()); + } + ModelBase::fromHttpContent(val, str); + return fromString(str, outVal); +} // base64 encoding/decoding based on : https://en.wikibooks.org/wiki/Algorithm_Implementation/Miscellaneous/Base64#C.2B.2B const static char Base64Chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; const static char Base64PadChar = '='; @@ -195,8 +526,6 @@ utility::string_t ModelBase::toBase64( std::shared_ptr value ) } return base64; } - - std::shared_ptr ModelBase::fromBase64( const utility::string_t& encoded ) { std::shared_ptr result(new std::stringstream); @@ -263,107 +592,6 @@ std::shared_ptr ModelBase::fromBase64( const utility::string_t& en return result; } -int64_t ModelBase::int64_tFromJson(const web::json::value& val) -{ - return val.is_null() ? std::numeric_limits::quiet_NaN() : val.as_number().to_int64(); -} - -int32_t ModelBase::int32_tFromJson(const web::json::value& val) -{ - return val.is_null() ? std::numeric_limits::quiet_NaN() : val.as_integer(); -} - -float ModelBase::floatFromJson(const web::json::value& val) -{ - return val.is_null() ? std::numeric_limits::quiet_NaN() : static_cast(val.as_double()); -} - -utility::string_t ModelBase::stringFromJson(const web::json::value& val) -{ - return val.is_string() ? val.as_string() : utility::conversions::to_string_t(""); -} - -utility::datetime ModelBase::dateFromJson(const web::json::value& val) -{ - return val.is_null() ? utility::datetime::from_string(utility::conversions::to_string_t("NULL"), utility::datetime::ISO_8601) : utility::datetime::from_string(val.as_string(), utility::datetime::ISO_8601); -} -bool ModelBase::boolFromJson(const web::json::value& val) -{ - return val.is_null() ? false : val.as_bool(); -} -double ModelBase::doubleFromJson(const web::json::value& val) -{ - return val.is_null() ? std::numeric_limits::quiet_NaN(): val.as_double(); -} - -int64_t ModelBase::int64_tFromHttpContent(std::shared_ptr val) -{ - utility::string_t str = ModelBase::stringFromHttpContent(val); - - utility::stringstream_t ss(str); - int64_t result = 0; - ss >> result; - return result; -} -int32_t ModelBase::int32_tFromHttpContent(std::shared_ptr val) -{ - utility::string_t str = ModelBase::stringFromHttpContent(val); - - utility::stringstream_t ss(str); - int32_t result = 0; - ss >> result; - return result; -} -float ModelBase::floatFromHttpContent(std::shared_ptr val) -{ - utility::string_t str = ModelBase::stringFromHttpContent(val); - - utility::stringstream_t ss(str); - float result = 0; - ss >> result; - return result; -} -utility::string_t ModelBase::stringFromHttpContent(std::shared_ptr val) -{ - std::shared_ptr data = val->getData(); - data->seekg( 0, data->beg ); - - std::string str((std::istreambuf_iterator(*data.get())), - std::istreambuf_iterator()); - - return utility::conversions::to_string_t(str); -} -utility::datetime ModelBase::dateFromHttpContent(std::shared_ptr val) -{ - utility::string_t str = ModelBase::stringFromHttpContent(val); - return utility::datetime::from_string(str, utility::datetime::ISO_8601); -} - -bool ModelBase::boolFromHttpContent(std::shared_ptr val) -{ - utility::string_t str = ModelBase::stringFromHttpContent(val); - - utility::stringstream_t ss(str); - bool result = false; - ss >> result; - return result; -} -double ModelBase::doubleFromHttpContent(std::shared_ptr val) -{ - utility::string_t str = ModelBase::stringFromHttpContent(val); - - utility::stringstream_t ss(str); - double result = 0.0; - ss >> result; - return result; -} - -web::json::value ModelBase::valueFromHttpContent(std::shared_ptr val) -{ - utility::string_t str = ModelBase::stringFromHttpContent(val); - return web::json::value::parse(str); -} - {{#modelNamespaceDeclarations}} } {{/modelNamespaceDeclarations}} diff --git a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/multipart-source.mustache b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/multipart-source.mustache index af0b86968d9..3984e18d7c6 100644 --- a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/multipart-source.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/multipart-source.mustache @@ -85,7 +85,7 @@ void MultipartFormData::writeTo( std::ostream& target ) data->seekg( 0, data->end ); std::vector dataBytes( data->tellg() ); - + data->seekg( 0, data->beg ); data->read( &dataBytes[0], dataBytes.size() ); diff --git a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/object-header.mustache b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/object-header.mustache index e048a80cd99..12e24ff8f5f 100644 --- a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/object-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/object-header.mustache @@ -29,10 +29,10 @@ public: void validate() override; web::json::value toJson() const override; - void fromJson(const web::json::value& json) override; + bool fromJson(const web::json::value& json) override; void toMultipart(std::shared_ptr multipart, const utility::string_t& namePrefix) const override; - void fromMultiPart(std::shared_ptr multipart, const utility::string_t& namePrefix) override; + bool fromMultiPart(std::shared_ptr multipart, const utility::string_t& namePrefix) override; ///////////////////////////////////////////// /// Object manipulation diff --git a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/object-source.mustache b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/object-source.mustache index ba81ba71976..e9c344e7e59 100644 --- a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/object-source.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/object-source.mustache @@ -16,7 +16,7 @@ Object::~Object() void Object::validate() { - // TODO: implement validation + } web::json::value Object::toJson() const @@ -24,12 +24,14 @@ web::json::value Object::toJson() const return m_object; } -void Object::fromJson(const web::json::value& val) +bool Object::fromJson(const web::json::value& val) { if (val.is_object()) { m_object = val; + m_IsSet = true; } + return isSet(); } void Object::toMultipart(std::shared_ptr multipart, const utility::string_t& prefix) const @@ -42,7 +44,7 @@ void Object::toMultipart(std::shared_ptr multipart, const uti multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("object"), m_object)); } -void Object::fromMultiPart(std::shared_ptr multipart, const utility::string_t& prefix) +bool Object::fromMultiPart(std::shared_ptr 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(".")) @@ -50,7 +52,11 @@ void Object::fromMultiPart(std::shared_ptr multipart, const u namePrefix += utility::conversions::to_string_t("."); } - m_object = ModelBase::valueFromHttpContent(multipart->getContent(namePrefix + utility::conversions::to_string_t("object"))); + if( ModelBase::fromHttpContent(multipart->getContent(namePrefix + utility::conversions::to_string_t("object")), m_object ) ) + { + m_IsSet = true; + } + return isSet(); } web::json::value Object::getValue(const utility::string_t& key) const @@ -61,7 +67,11 @@ web::json::value Object::getValue(const utility::string_t& key) const void Object::setValue(const utility::string_t& key, const web::json::value& value) { - m_object[key] = value; + if( !value.is_null() ) + { + m_object[key] = value; + m_IsSet = true; + } } {{#modelNamespaceDeclarations}} diff --git a/samples/client/petstore/cpp-restsdk/client/HttpContent.cpp b/samples/client/petstore/cpp-restsdk/client/HttpContent.cpp index 92289ec7ad1..801403ef571 100644 --- a/samples/client/petstore/cpp-restsdk/client/HttpContent.cpp +++ b/samples/client/petstore/cpp-restsdk/client/HttpContent.cpp @@ -24,7 +24,7 @@ HttpContent::~HttpContent() { } -utility::string_t HttpContent::getContentDisposition() +utility::string_t HttpContent::getContentDisposition() const { return m_ContentDisposition; } @@ -34,7 +34,7 @@ void HttpContent::setContentDisposition( const utility::string_t & value ) m_ContentDisposition = value; } -utility::string_t HttpContent::getName() +utility::string_t HttpContent::getName() const { return m_Name; } @@ -44,7 +44,7 @@ void HttpContent::setName( const utility::string_t & value ) m_Name = value; } -utility::string_t HttpContent::getFileName() +utility::string_t HttpContent::getFileName() const { return m_FileName; } @@ -54,7 +54,7 @@ void HttpContent::setFileName( const utility::string_t & value ) m_FileName = value; } -utility::string_t HttpContent::getContentType() +utility::string_t HttpContent::getContentType() const { return m_ContentType; } @@ -64,7 +64,7 @@ void HttpContent::setContentType( const utility::string_t & value ) m_ContentType = value; } -std::shared_ptr HttpContent::getData() +std::shared_ptr HttpContent::getData() const { return m_Data; } diff --git a/samples/client/petstore/cpp-restsdk/client/HttpContent.h b/samples/client/petstore/cpp-restsdk/client/HttpContent.h index fefd179c25a..e908687d2ac 100644 --- a/samples/client/petstore/cpp-restsdk/client/HttpContent.h +++ b/samples/client/petstore/cpp-restsdk/client/HttpContent.h @@ -35,19 +35,19 @@ public: HttpContent(); virtual ~HttpContent(); - virtual utility::string_t getContentDisposition(); + virtual utility::string_t getContentDisposition() const; virtual void setContentDisposition( const utility::string_t& value ); - virtual utility::string_t getName(); + virtual utility::string_t getName() const; virtual void setName( const utility::string_t& value ); - virtual utility::string_t getFileName(); + virtual utility::string_t getFileName() const; virtual void setFileName( const utility::string_t& value ); - virtual utility::string_t getContentType(); + virtual utility::string_t getContentType() const; virtual void setContentType( const utility::string_t& value ); - virtual std::shared_ptr getData(); + virtual std::shared_ptr getData() const; virtual void setData( std::shared_ptr value ); virtual void writeTo( std::ostream& stream ); diff --git a/samples/client/petstore/cpp-restsdk/client/ModelBase.cpp b/samples/client/petstore/cpp-restsdk/client/ModelBase.cpp index d3fd80b5544..639fe6d6efe 100644 --- a/samples/client/petstore/cpp-restsdk/client/ModelBase.cpp +++ b/samples/client/petstore/cpp-restsdk/client/ModelBase.cpp @@ -16,20 +16,80 @@ namespace openapitools { namespace client { namespace model { -ModelBase::ModelBase() +ModelBase::ModelBase(): m_IsSet(false) { } ModelBase::~ModelBase() { } - -web::json::value ModelBase::toJson( const utility::string_t& value ) +bool ModelBase::isSet() const { - return web::json::value::string(value); + return m_IsSet; } -web::json::value ModelBase::toJson( const utility::datetime& value ) +utility::string_t ModelBase::toString( const bool val ) { - return web::json::value::string(value.to_string(utility::datetime::ISO_8601)); + utility::stringstream_t ss; + ss << val; + return utility::string_t(ss.str()); +} +utility::string_t ModelBase::toString( const float val ) +{ + utility::stringstream_t ss; + ss << val; + return utility::string_t(ss.str()); +} +utility::string_t ModelBase::toString( const double val ) +{ + utility::stringstream_t ss; + ss << val; + return utility::string_t(ss.str()); +} +utility::string_t ModelBase::toString( const int32_t val ) +{ + utility::stringstream_t ss; + ss << val; + return utility::string_t(ss.str()); +} +utility::string_t ModelBase::toString( const int64_t val ) +{ + utility::stringstream_t ss; + ss << val; + return utility::string_t(ss.str()); +} +utility::string_t ModelBase::toString (const utility::string_t &val ) +{ + utility::stringstream_t ss; + ss << val; + return utility::string_t(ss.str()); +} +utility::string_t ModelBase::toString( const utility::datetime &val ) +{ + return val.to_string(utility::datetime::ISO_8601); +} +utility::string_t ModelBase::toString( const web::json::value &val ) +{ + return val.serialize(); +} +utility::string_t ModelBase::toString( const std::shared_ptr& val ) +{ + utility::stringstream_t ss; + if( val != nullptr ) + { + ss << val->getData(); + } + return utility::string_t(ss.str()); +} +web::json::value ModelBase::toJson(bool value) +{ + return web::json::value::boolean(value); +} +web::json::value ModelBase::toJson( float value ) +{ + return web::json::value::number(value); +} +web::json::value ModelBase::toJson( double value ) +{ + return web::json::value::number(value); } web::json::value ModelBase::toJson( int32_t value ) { @@ -39,88 +99,259 @@ web::json::value ModelBase::toJson( int64_t value ) { return web::json::value::number(value); } -web::json::value ModelBase::toJson( double value ) +web::json::value ModelBase::toJson( const utility::string_t& value ) { - return web::json::value::number(value); -} -web::json::value ModelBase::toJson(bool value) { - return web::json::value::boolean(value); + return web::json::value::string(value); } - -web::json::value ModelBase::toJson( std::shared_ptr content ) +web::json::value ModelBase::toJson( const utility::datetime& value ) +{ + return web::json::value::string(value.to_string(utility::datetime::ISO_8601)); +} +web::json::value ModelBase::toJson( const web::json::value& value ) { - web::json::value value; - value[utility::conversions::to_string_t("ContentDisposition")] = ModelBase::toJson(content->getContentDisposition()); - value[utility::conversions::to_string_t("ContentType")] = ModelBase::toJson(content->getContentType()); - value[utility::conversions::to_string_t("FileName")] = ModelBase::toJson(content->getFileName()); - value[utility::conversions::to_string_t("InputStream")] = web::json::value::string( ModelBase::toBase64(content->getData()) ); return value; } - -std::shared_ptr ModelBase::fileFromJson(const web::json::value& val) +web::json::value ModelBase::toJson( const std::shared_ptr& content ) { - std::shared_ptr content(new HttpContent); - - if(val.has_field(utility::conversions::to_string_t("ContentDisposition"))) + web::json::value value; + if(content != nullptr) { - content->setContentDisposition( ModelBase::stringFromJson(val.at(utility::conversions::to_string_t("ContentDisposition"))) ); + value[utility::conversions::to_string_t("ContentDisposition")] = ModelBase::toJson(content->getContentDisposition()); + value[utility::conversions::to_string_t("ContentType")] = ModelBase::toJson(content->getContentType()); + value[utility::conversions::to_string_t("FileName")] = ModelBase::toJson(content->getFileName()); + value[utility::conversions::to_string_t("InputStream")] = web::json::value::string( ModelBase::toBase64(content->getData()) ); } - if(val.has_field(utility::conversions::to_string_t("ContentType"))) - { - content->setContentType( ModelBase::stringFromJson(val.at(utility::conversions::to_string_t("ContentType"))) ); - } - if(val.has_field(utility::conversions::to_string_t("FileName"))) - { - content->setFileName( ModelBase::stringFromJson(val.at(utility::conversions::to_string_t("FileName"))) ); - } - if(val.has_field(utility::conversions::to_string_t("InputStream"))) - { - content->setData( ModelBase::fromBase64( ModelBase::stringFromJson(val.at(utility::conversions::to_string_t("InputStream")))) ); - } - - return content; + return value; } - -web::json::value ModelBase::toJson( std::shared_ptr content ) +bool ModelBase::fromString( const utility::string_t& val, bool &outVal ) { - return content.get() ? content->toJson() : web::json::value::null(); + utility::stringstream_t ss(val); + bool success = true; + try + { + ss >> outVal; + } + catch (...) + { + success = false; + } + return success; } - -std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, const utility::string_t& value, const utility::string_t& contentType) +bool ModelBase::fromString( const utility::string_t& val, float &outVal ) { - std::shared_ptr content(new HttpContent); - content->setName( name ); - content->setContentDisposition( utility::conversions::to_string_t("form-data") ); - content->setContentType( contentType ); - content->setData( std::shared_ptr( new std::stringstream( utility::conversions::to_utf8string(value) ) ) ); - return content; + utility::stringstream_t ss(val); + bool success = true; + try + { + ss >> outVal; + } + catch (...) + { + success = false; + } + return success; } -std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, const utility::datetime& value, const utility::string_t& contentType ) +bool ModelBase::fromString( const utility::string_t& val, double &outVal ) +{ + utility::stringstream_t ss(val); + bool success = true; + try + { + ss >> outVal; + } + catch (...) + { + success = false; + } + return success; +} +bool ModelBase::fromString( const utility::string_t& val, int32_t &outVal ) +{ + utility::stringstream_t ss(val); + bool success = true; + try + { + ss >> outVal; + } + catch (...) + { + success = false; + } + return success; +} +bool ModelBase::fromString( const utility::string_t& val, int64_t &outVal ) +{ + utility::stringstream_t ss(val); + bool success = true; + try + { + ss >> outVal; + } + catch (...) + { + success = false; + } + return success; +} +bool ModelBase::fromString( const utility::string_t& val, utility::string_t &outVal ) +{ + utility::stringstream_t ss(val); + bool success = true; + try + { + ss >> outVal; + } + catch (...) + { + success = false; + } + return success; +} +bool ModelBase::fromString( const utility::string_t& val, utility::datetime &outVal ) +{ + bool success = true; + auto dt = utility::datetime::from_string(val, utility::datetime::ISO_8601); + if( dt.is_initialized() ) + { + outVal = dt; + } + else + { + success = false; + } + return success; +} +bool ModelBase::fromString( const utility::string_t& val, web::json::value &outVal ) +{ + outVal = web::json::value::parse(val); + return !outVal.is_null(); +} +bool ModelBase::fromString( const utility::string_t& val, std::shared_ptr& outVal ) +{ + bool ok = true; + if(outVal == nullptr) + { + outVal = std::shared_ptr(new HttpContent()); + } + std::shared_ptr ssptr = std::make_shared(val); + if(outVal != nullptr) + { + outVal->setData(ssptr); + } + else + { + ok = false; + } + return ok; +} +bool ModelBase::fromJson( const web::json::value& val, bool & outVal ) +{ + outVal = !val.is_boolean() ? false : val.as_bool(); + return val.is_boolean(); +} +bool ModelBase::fromJson( const web::json::value& val, float & outVal ) +{ + outVal = !val.is_double() ? std::numeric_limits::quiet_NaN(): static_cast(val.as_double()); + return val.is_double(); +} +bool ModelBase::fromJson( const web::json::value& val, double & outVal ) +{ + outVal = !val.is_double() ? std::numeric_limits::quiet_NaN(): val.as_double(); + return val.is_double() ; +} +bool ModelBase::fromJson( const web::json::value& val, int32_t & outVal ) +{ + outVal = !val.is_integer() ? std::numeric_limits::quiet_NaN() : val.as_integer(); + return val.is_integer(); +} +bool ModelBase::fromJson( const web::json::value& val, int64_t & outVal ) +{ + outVal = !val.is_null() ? std::numeric_limits::quiet_NaN() : val.as_number().to_int64(); + return val.is_number(); +} +bool ModelBase::fromJson( const web::json::value& val, utility::string_t & outVal ) +{ + outVal = val.is_string() ? val.as_string() : utility::conversions::to_string_t(""); + return val.is_string(); +} +bool ModelBase::fromJson( const web::json::value& val, utility::datetime & outVal ) +{ + outVal = val.is_null() ? utility::datetime::from_string(utility::conversions::to_string_t("NULL"), utility::datetime::ISO_8601) : utility::datetime::from_string(val.as_string(), utility::datetime::ISO_8601); + return outVal.is_initialized(); +} +bool ModelBase::fromJson( const web::json::value& val, web::json::value & outVal ) +{ + outVal = val; + return !val.is_null(); +} +bool ModelBase::fromJson( const web::json::value& val, std::shared_ptr& content ) +{ + bool result = false; + if( content != nullptr) + { + result = true; + if(content == nullptr) + { + content = std::shared_ptr(new HttpContent()); + } + if(val.has_field(utility::conversions::to_string_t("ContentDisposition"))) + { + utility::string_t value; + result = result && ModelBase::fromJson(val.at(utility::conversions::to_string_t("ContentDisposition")), value); + content->setContentDisposition( value ); + } + if(val.has_field(utility::conversions::to_string_t("ContentType"))) + { + utility::string_t value; + result = result && ModelBase::fromJson(val.at(utility::conversions::to_string_t("ContentType")), value); + content->setContentType( value ); + } + if(val.has_field(utility::conversions::to_string_t("FileName"))) + { + utility::string_t value; + result = result && ModelBase::fromJson(val.at(utility::conversions::to_string_t("FileName")), value); + content->setFileName( value ); + } + if(val.has_field(utility::conversions::to_string_t("InputStream"))) + { + utility::string_t value; + result = result && ModelBase::fromJson(val.at(utility::conversions::to_string_t("InputStream")), value); + content->setData( ModelBase::fromBase64( value ) ); + } + } + return result; +} +std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, bool value, const utility::string_t& contentType ) { std::shared_ptr content( new HttpContent ); content->setName( name ); content->setContentDisposition( utility::conversions::to_string_t("form-data") ); content->setContentType( contentType ); - content->setData( std::shared_ptr( new std::stringstream( utility::conversions::to_utf8string(value.to_string(utility::datetime::ISO_8601) ) ) ) ); + std::stringstream* valueAsStringStream = new std::stringstream(); + (*valueAsStringStream) << value; + content->setData( std::shared_ptr( valueAsStringStream ) ); return content; } -std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, std::shared_ptr value ) -{ - std::shared_ptr content( new HttpContent ); - content->setName( name ); - content->setContentDisposition( value->getContentDisposition() ); - content->setContentType( value->getContentType() ); - content->setData( value->getData() ); - content->setFileName( value->getFileName() ); - return content; -} -std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, const web::json::value& value, const utility::string_t& contentType ) +std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, float value, const utility::string_t& contentType ) { std::shared_ptr content( new HttpContent ); content->setName( name ); content->setContentDisposition( utility::conversions::to_string_t("form-data") ); content->setContentType( contentType ); - content->setData( std::shared_ptr( new std::stringstream( utility::conversions::to_utf8string(value.serialize()) ) ) ); + std::stringstream* valueAsStringStream = new std::stringstream(); + (*valueAsStringStream) << value; + content->setData( std::shared_ptr( valueAsStringStream ) ); + return content; +} +std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, double value, const utility::string_t& contentType ) +{ + std::shared_ptr content( new HttpContent ); + content->setName( name ); + content->setContentDisposition( utility::conversions::to_string_t("form-data") ); + content->setContentType( contentType ); + std::stringstream* valueAsStringStream = new std::stringstream(); + (*valueAsStringStream) << value; + content->setData( std::shared_ptr( valueAsStringStream ) ); return content; } std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, int32_t value, const utility::string_t& contentType ) @@ -145,18 +376,118 @@ std::shared_ptr ModelBase::toHttpContent( const utility::string_t& content->setData( std::shared_ptr( valueAsStringStream) ) ; return content; } -std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, double value, const utility::string_t& contentType ) +std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, const utility::string_t& value, const utility::string_t& contentType) +{ + std::shared_ptr content(new HttpContent); + content->setName( name ); + content->setContentDisposition( utility::conversions::to_string_t("form-data") ); + content->setContentType( contentType ); + content->setData( std::shared_ptr( new std::stringstream( utility::conversions::to_utf8string(value) ) ) ); + return content; +} +std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, const utility::datetime& value, const utility::string_t& contentType ) { std::shared_ptr content( new HttpContent ); content->setName( name ); content->setContentDisposition( utility::conversions::to_string_t("form-data") ); content->setContentType( contentType ); - std::stringstream* valueAsStringStream = new std::stringstream(); - (*valueAsStringStream) << value; - content->setData( std::shared_ptr( valueAsStringStream ) ); + content->setData( std::shared_ptr( new std::stringstream( utility::conversions::to_utf8string(value.to_string(utility::datetime::ISO_8601) ) ) ) ); return content; } +std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, const web::json::value& value, const utility::string_t& contentType ) +{ + std::shared_ptr content( new HttpContent ); + content->setName( name ); + content->setContentDisposition( utility::conversions::to_string_t("form-data") ); + content->setContentType( contentType ); + content->setData( std::shared_ptr( new std::stringstream( utility::conversions::to_utf8string(value.serialize()) ) ) ); + return content; +} +std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, const std::shared_ptr& value ) +{ + std::shared_ptr content( new HttpContent ); + if( value != nullptr ) + { + content->setName( name ); + content->setContentDisposition( value->getContentDisposition() ); + content->setContentType( value->getContentType() ); + content->setData( value->getData() ); + content->setFileName( value->getFileName() ); + } + return content; +} +bool ModelBase::fromHttpContent(std::shared_ptr val, bool & outVal ) +{ + utility::string_t str; + if( val == nullptr ) return false; + ModelBase::fromHttpContent(val, str); + return fromString(str, outVal); +} +bool ModelBase::fromHttpContent(std::shared_ptr val, float & outVal ) +{ + utility::string_t str; + if( val == nullptr ) return false; + ModelBase::fromHttpContent(val, str); + return fromString(str, outVal); +} +bool ModelBase::fromHttpContent(std::shared_ptr val, double & outVal ) +{ + utility::string_t str; + if( val == nullptr ) return false; + ModelBase::fromHttpContent(val, str); + return fromString(str, outVal); +} +bool ModelBase::fromHttpContent(std::shared_ptr val, int32_t & outVal ) +{ + utility::string_t str; + if( val == nullptr ) return false; + ModelBase::fromHttpContent(val, str); + return fromString(str, outVal); +} +bool ModelBase::fromHttpContent(std::shared_ptr val, int64_t & outVal ) +{ + utility::string_t str; + if( val == nullptr ) return false; + ModelBase::fromHttpContent(val, str); + return fromString(str, outVal); +} +bool ModelBase::fromHttpContent(std::shared_ptr val, utility::string_t & outVal ) +{ + if( val == nullptr ) return false; + std::shared_ptr data = val->getData(); + data->seekg( 0, data->beg ); + std::string str((std::istreambuf_iterator(*data.get())), + std::istreambuf_iterator()); + outVal = utility::conversions::to_string_t(str); + return true; +} +bool ModelBase::fromHttpContent(std::shared_ptr val, utility::datetime & outVal ) +{ + utility::string_t str; + if( val == nullptr ) return false; + ModelBase::fromHttpContent(val, str); + outVal = utility::datetime::from_string(str, utility::datetime::ISO_8601); + return true; +} +bool ModelBase::fromHttpContent(std::shared_ptr val, web::json::value & outVal ) +{ + utility::string_t str; + if( val == nullptr ) return false; + ModelBase::fromHttpContent(val, str); + return fromString(str, outVal); +} +bool ModelBase::fromHttpContent(std::shared_ptr val, std::shared_ptr& outVal ) +{ + utility::string_t str; + if( val == nullptr ) return false; + if( outVal == nullptr ) + { + outVal = std::shared_ptr(new HttpContent()); + } + ModelBase::fromHttpContent(val, str); + return fromString(str, outVal); +} // base64 encoding/decoding based on : https://en.wikibooks.org/wiki/Algorithm_Implementation/Miscellaneous/Base64#C.2B.2B const static char Base64Chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; const static char Base64PadChar = '='; @@ -206,8 +537,6 @@ utility::string_t ModelBase::toBase64( std::shared_ptr value ) } return base64; } - - std::shared_ptr ModelBase::fromBase64( const utility::string_t& encoded ) { std::shared_ptr result(new std::stringstream); @@ -274,107 +603,6 @@ std::shared_ptr ModelBase::fromBase64( const utility::string_t& en return result; } -int64_t ModelBase::int64_tFromJson(const web::json::value& val) -{ - return val.is_null() ? std::numeric_limits::quiet_NaN() : val.as_number().to_int64(); -} - -int32_t ModelBase::int32_tFromJson(const web::json::value& val) -{ - return val.is_null() ? std::numeric_limits::quiet_NaN() : val.as_integer(); -} - -float ModelBase::floatFromJson(const web::json::value& val) -{ - return val.is_null() ? std::numeric_limits::quiet_NaN() : static_cast(val.as_double()); -} - -utility::string_t ModelBase::stringFromJson(const web::json::value& val) -{ - return val.is_string() ? val.as_string() : utility::conversions::to_string_t(""); -} - -utility::datetime ModelBase::dateFromJson(const web::json::value& val) -{ - return val.is_null() ? utility::datetime::from_string(utility::conversions::to_string_t("NULL"), utility::datetime::ISO_8601) : utility::datetime::from_string(val.as_string(), utility::datetime::ISO_8601); -} -bool ModelBase::boolFromJson(const web::json::value& val) -{ - return val.is_null() ? false : val.as_bool(); -} -double ModelBase::doubleFromJson(const web::json::value& val) -{ - return val.is_null() ? std::numeric_limits::quiet_NaN(): val.as_double(); -} - -int64_t ModelBase::int64_tFromHttpContent(std::shared_ptr val) -{ - utility::string_t str = ModelBase::stringFromHttpContent(val); - - utility::stringstream_t ss(str); - int64_t result = 0; - ss >> result; - return result; -} -int32_t ModelBase::int32_tFromHttpContent(std::shared_ptr val) -{ - utility::string_t str = ModelBase::stringFromHttpContent(val); - - utility::stringstream_t ss(str); - int32_t result = 0; - ss >> result; - return result; -} -float ModelBase::floatFromHttpContent(std::shared_ptr val) -{ - utility::string_t str = ModelBase::stringFromHttpContent(val); - - utility::stringstream_t ss(str); - float result = 0; - ss >> result; - return result; -} -utility::string_t ModelBase::stringFromHttpContent(std::shared_ptr val) -{ - std::shared_ptr data = val->getData(); - data->seekg( 0, data->beg ); - - std::string str((std::istreambuf_iterator(*data.get())), - std::istreambuf_iterator()); - - return utility::conversions::to_string_t(str); -} -utility::datetime ModelBase::dateFromHttpContent(std::shared_ptr val) -{ - utility::string_t str = ModelBase::stringFromHttpContent(val); - return utility::datetime::from_string(str, utility::datetime::ISO_8601); -} - -bool ModelBase::boolFromHttpContent(std::shared_ptr val) -{ - utility::string_t str = ModelBase::stringFromHttpContent(val); - - utility::stringstream_t ss(str); - bool result = false; - ss >> result; - return result; -} -double ModelBase::doubleFromHttpContent(std::shared_ptr val) -{ - utility::string_t str = ModelBase::stringFromHttpContent(val); - - utility::stringstream_t ss(str); - double result = 0.0; - ss >> result; - return result; -} - -web::json::value ModelBase::valueFromHttpContent(std::shared_ptr val) -{ - utility::string_t str = ModelBase::stringFromHttpContent(val); - return web::json::value::parse(str); -} - } } } diff --git a/samples/client/petstore/cpp-restsdk/client/ModelBase.h b/samples/client/petstore/cpp-restsdk/client/ModelBase.h index 2d33fbe33cb..b206ee4956e 100644 --- a/samples/client/petstore/cpp-restsdk/client/ModelBase.h +++ b/samples/client/petstore/cpp-restsdk/client/ModelBase.h @@ -19,14 +19,15 @@ #define ORG_OPENAPITOOLS_CLIENT_MODEL_ModelBase_H_ +#include +#include + #include "HttpContent.h" #include "MultipartFormData.h" #include #include -#include - namespace org { namespace openapitools { namespace client { @@ -41,68 +42,252 @@ public: virtual void validate() = 0; virtual web::json::value toJson() const = 0; - virtual void fromJson(const web::json::value& json) = 0; + virtual bool fromJson( const web::json::value& json ) = 0; - virtual void toMultipart(std::shared_ptr multipart, const utility::string_t& namePrefix) const = 0; - virtual void fromMultiPart(std::shared_ptr multipart, const utility::string_t& namePrefix) = 0; + virtual void toMultipart( std::shared_ptr multipart, const utility::string_t& namePrefix ) const = 0; + virtual bool fromMultiPart( std::shared_ptr multipart, const utility::string_t& namePrefix ) = 0; - static web::json::value toJson( const utility::string_t& value ); - static web::json::value toJson( const utility::datetime& value ); - static web::json::value toJson( std::shared_ptr value ); - static web::json::value toJson( std::shared_ptr value ); - static web::json::value toJson( int32_t value ); - static web::json::value toJson( int64_t value ); - static web::json::value toJson( double value ); - static web::json::value toJson( bool value ); - template - static web::json::value toJson(const std::vector& value); + virtual bool isSet() const; - static int64_t int64_tFromJson(const web::json::value& val); - static int32_t int32_tFromJson(const web::json::value& val); - static float floatFromJson(const web::json::value& val); - static utility::string_t stringFromJson(const web::json::value& val); - static utility::datetime dateFromJson(const web::json::value& val); - static double doubleFromJson(const web::json::value& val); - static bool boolFromJson(const web::json::value& val); - static std::shared_ptr fileFromJson(const web::json::value& val); + static utility::string_t toString( const bool val ); + static utility::string_t toString( const float val ); + static utility::string_t toString( const double val ); + static utility::string_t toString( const int32_t val ); + static utility::string_t toString( const int64_t val ); + static utility::string_t toString( const utility::string_t &val ); + static utility::string_t toString( const utility::datetime &val ); + static utility::string_t toString( const web::json::value &val ); + static utility::string_t toString( const std::shared_ptr& val ); + template + static utility::string_t toString( const std::shared_ptr& val ); + template + static utility::string_t toString( const std::vector & val ); - static std::shared_ptr toHttpContent( const utility::string_t& name, const utility::string_t& value, const utility::string_t& contentType = utility::conversions::to_string_t("")); - static std::shared_ptr toHttpContent( const utility::string_t& name, const utility::datetime& value, const utility::string_t& contentType = utility::conversions::to_string_t("")); - static std::shared_ptr toHttpContent( const utility::string_t& name, std::shared_ptr value ); - static std::shared_ptr toHttpContent( const utility::string_t& name, const web::json::value& value, const utility::string_t& contentType = utility::conversions::to_string_t("application/json") ); + static web::json::value toJson( bool val ); + static web::json::value toJson( float val ); + static web::json::value toJson( double val ); + static web::json::value toJson( int32_t val ); + static web::json::value toJson( int64_t val ); + static web::json::value toJson( const utility::string_t& val ); + static web::json::value toJson( const utility::datetime& val ); + static web::json::value toJson( const web::json::value& val ); + static web::json::value toJson( const std::shared_ptr& val ); + template + static web::json::value toJson( const std::shared_ptr& val ); + template + static web::json::value toJson( const std::vector& val ); + template + static web::json::value toJson( const std::map& val ); + + static bool fromString( const utility::string_t& val, bool & ); + static bool fromString( const utility::string_t& val, float & ); + static bool fromString( const utility::string_t& val, double & ); + static bool fromString( const utility::string_t& val, int32_t & ); + static bool fromString( const utility::string_t& val, int64_t & ); + static bool fromString( const utility::string_t& val, utility::string_t & ); + static bool fromString( const utility::string_t& val, utility::datetime & ); + static bool fromString( const utility::string_t& val, web::json::value & ); + static bool fromString( const utility::string_t& val, std::shared_ptr & ); + template + static bool fromString( const utility::string_t& val, std::shared_ptr& ); + template + static bool fromString( const utility::string_t& val, std::vector & ); + template + static bool fromString( const utility::string_t& val, std::map & ); + + static bool fromJson( const web::json::value& val, bool & ); + static bool fromJson( const web::json::value& val, float & ); + static bool fromJson( const web::json::value& val, double & ); + static bool fromJson( const web::json::value& val, int32_t & ); + static bool fromJson( const web::json::value& val, int64_t & ); + static bool fromJson( const web::json::value& val, utility::string_t & ); + static bool fromJson( const web::json::value& val, utility::datetime & ); + static bool fromJson( const web::json::value& val, web::json::value & ); + static bool fromJson( const web::json::value& val, std::shared_ptr & ); + template + static bool fromJson( const web::json::value& val, std::shared_ptr& ); + template + static bool fromJson( const web::json::value& val, std::vector & ); + template + static bool fromJson( const web::json::value& val, std::map & ); + + + static std::shared_ptr toHttpContent( const utility::string_t& name, bool value, const utility::string_t& contentType = utility::conversions::to_string_t("") ); + static std::shared_ptr toHttpContent( const utility::string_t& name, float value, const utility::string_t& contentType = utility::conversions::to_string_t("") ); + static std::shared_ptr toHttpContent( const utility::string_t& name, double value, const utility::string_t& contentType = utility::conversions::to_string_t("") ); static std::shared_ptr toHttpContent( const utility::string_t& name, int32_t value, const utility::string_t& contentType = utility::conversions::to_string_t("") ); static std::shared_ptr toHttpContent( const utility::string_t& name, int64_t value, const utility::string_t& contentType = utility::conversions::to_string_t("") ); - static std::shared_ptr toHttpContent( const utility::string_t& name, double value, const utility::string_t& contentType = utility::conversions::to_string_t("") ); - template + static std::shared_ptr toHttpContent( const utility::string_t& name, const utility::string_t& value, const utility::string_t& contentType = utility::conversions::to_string_t("")); + static std::shared_ptr toHttpContent( const utility::string_t& name, const utility::datetime& value, const utility::string_t& contentType = utility::conversions::to_string_t("")); + static std::shared_ptr toHttpContent( const utility::string_t& name, const web::json::value& value, const utility::string_t& contentType = utility::conversions::to_string_t("application/json") ); + static std::shared_ptr toHttpContent( const utility::string_t& name, const std::shared_ptr& ); + template + static std::shared_ptr toHttpContent( const utility::string_t& name, const std::shared_ptr& , const utility::string_t& contentType = utility::conversions::to_string_t("application/json") ); + template static std::shared_ptr toHttpContent( const utility::string_t& name, const std::vector& value, const utility::string_t& contentType = utility::conversions::to_string_t("") ); + template + static std::shared_ptr toHttpContent( const utility::string_t& name, const std::map& value, const utility::string_t& contentType = utility::conversions::to_string_t("") ); - static int64_t int64_tFromHttpContent(std::shared_ptr val); - static int32_t int32_tFromHttpContent(std::shared_ptr val); - static float floatFromHttpContent(std::shared_ptr val); - static utility::string_t stringFromHttpContent(std::shared_ptr val); - static utility::datetime dateFromHttpContent(std::shared_ptr val); - static bool boolFromHttpContent(std::shared_ptr val); - static double doubleFromHttpContent(std::shared_ptr val); - static web::json::value valueFromHttpContent(std::shared_ptr val); - + static bool fromHttpContent( std::shared_ptr val, bool & ); + static bool fromHttpContent( std::shared_ptr val, float & ); + static bool fromHttpContent( std::shared_ptr val, double & ); + static bool fromHttpContent( std::shared_ptr val, int64_t & ); + static bool fromHttpContent( std::shared_ptr val, int32_t & ); + static bool fromHttpContent( std::shared_ptr val, utility::string_t & ); + static bool fromHttpContent( std::shared_ptr val, utility::datetime & ); + static bool fromHttpContent( std::shared_ptr val, web::json::value & ); + static bool fromHttpContent( std::shared_ptr val, std::shared_ptr& ); + template + static bool fromHttpContent( std::shared_ptr val, std::shared_ptr& ); + template + static bool fromHttpContent( std::shared_ptr val, std::vector & ); + template + static bool fromHttpContent( std::shared_ptr val, std::map & ); static utility::string_t toBase64( utility::string_t value ); static utility::string_t toBase64( std::shared_ptr value ); static std::shared_ptr fromBase64( const utility::string_t& encoded ); +protected: + bool m_IsSet; }; -template -web::json::value ModelBase::toJson(const std::vector& value) { - std::vector ret; - for (auto& x : value) { - ret.push_back(toJson(x)); +template +utility::string_t ModelBase::toString( const std::shared_ptr& val ) +{ + utility::stringstream_t ss; + if( val != nullptr ) + { + val->toJson().serialize(ss); + } + return utility::string_t(ss.str()); +} +template +utility::string_t ModelBase::toString( const std::vector & val ) +{ + utility::string_t strArray; + for ( const auto &item : val ) + { + strArray.append( toString(item) + "," ); + } + if (val.count() > 0) + { + strArray.pop_back(); + } + return strArray; +} +template +web::json::value ModelBase::toJson( const std::shared_ptr& val ) +{ + web::json::value retVal; + if(val != nullptr) + { + retVal = val->toJson(); + } + return retVal; +} +template +web::json::value ModelBase::toJson( const std::vector& value ) +{ + std::vector ret; + for ( const auto& x : value ) + { + ret.push_back( toJson(x) ); } - return web::json::value::array(ret); } - -template -std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, const std::vector& value, const utility::string_t& contentType ) { +template +web::json::value ModelBase::toJson( const std::map& val ) +{ + web::json::value obj; + for ( const auto &itemkey : val ) + { + obj[itemkey.first] = toJson( itemkey.second ); + } + return obj; +} +template +bool ModelBase::fromString( const utility::string_t& val, std::shared_ptr& outVal ) +{ + bool ok = false; + if(outVal == nullptr) + { + outVal = std::shared_ptr(new T()); + } + if( outVal != nullptr ) + { + ok = outVal->fromJson(web::json::value::parse(val)); + } + return ok; +} +template +bool ModelBase::fromJson( const web::json::value& val, std::shared_ptr &outVal ) +{ + bool ok = false; + if(outVal == nullptr) + { + outVal = std::shared_ptr(new T()); + } + if( outVal != nullptr ) + { + ok = outVal->fromJson(val); + } + return ok; +} +template +bool ModelBase::fromJson( const web::json::value& val, std::vector &outVal ) +{ + bool ok = true; + if (val.is_array()) + { + for (const auto jitem : val.as_array()) + { + T item; + ok &= fromJson(jitem, item); + outVal.push_back(item); + } + } + else + { + ok = false; + } + return ok; +} +template +bool ModelBase::fromJson( const web::json::value& jval, std::map &outVal ) +{ + bool ok = true; + if ( jval.is_object() ) + { + auto obj = jval.as_object(); + for( auto objItr = obj.begin() ; objItr != obj.end() ; objItr++ ) + { + T itemVal; + ok &= fromJson(objItr->second, itemVal); + outVal.insert(std::pair(objItr->first, itemVal)); + } + } + else + { + ok = false; + } + return ok; +} +template +std::shared_ptr ModelBase::toHttpContent(const utility::string_t& name, const std::shared_ptr& value , const utility::string_t& contentType ) +{ + std::shared_ptr content( new HttpContent ); + if (value != nullptr ) + { + content->setName( name ); + content->setContentDisposition( utility::conversions::to_string_t("form-data") ); + content->setContentType( contentType ); + content->setData( std::shared_ptr( new std::stringstream( utility::conversions::to_utf8string( value->toJson().serialize() ) ) ) ); + } + return content; +} +template +std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, const std::vector& value, const utility::string_t& contentType ) +{ web::json::value json_array = ModelBase::toJson(value); std::shared_ptr content( new HttpContent ); content->setName( name ); @@ -111,7 +296,39 @@ std::shared_ptr ModelBase::toHttpContent( const utility::string_t& content->setData( std::shared_ptr( new std::stringstream( utility::conversions::to_utf8string(json_array.serialize()) ) ) ); return content; } - +template +std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, const std::map& value, const utility::string_t& contentType ) +{ + web::json::value jobj = ModelBase::toJson(value); + std::shared_ptr content( new HttpContent ); + content->setName( name ); + content->setContentDisposition( utility::conversions::to_string_t("form-data") ); + content->setContentType( contentType ); + content->setData( std::shared_ptr( new std::stringstream( utility::conversions::to_utf8string(jobj.serialize()) ) ) ); + return content; +} +template +bool ModelBase::fromHttpContent( std::shared_ptr val, std::shared_ptr& outVal ) +{ + utility::string_t str; + if(val == nullptr) return false; + if( outVal == nullptr ) + { + outVal = std::shared_ptr(new T()); + } + ModelBase::fromHttpContent(val, str); + return fromString(str, outVal); +} +template +bool ModelBase::fromHttpContent( std::shared_ptr val, std::vector & ) +{ + return true; +} +template +bool ModelBase::fromHttpContent( std::shared_ptr val, std::map & ) +{ + return true; +} } } } diff --git a/samples/client/petstore/cpp-restsdk/client/MultipartFormData.cpp b/samples/client/petstore/cpp-restsdk/client/MultipartFormData.cpp index 5e840c1acbb..5939e9bf4d2 100644 --- a/samples/client/petstore/cpp-restsdk/client/MultipartFormData.cpp +++ b/samples/client/petstore/cpp-restsdk/client/MultipartFormData.cpp @@ -96,7 +96,7 @@ void MultipartFormData::writeTo( std::ostream& target ) data->seekg( 0, data->end ); std::vector dataBytes( data->tellg() ); - + data->seekg( 0, data->beg ); data->read( &dataBytes[0], dataBytes.size() ); diff --git a/samples/client/petstore/cpp-restsdk/client/Object.cpp b/samples/client/petstore/cpp-restsdk/client/Object.cpp index 6c0e441a9fc..239560a3e31 100644 --- a/samples/client/petstore/cpp-restsdk/client/Object.cpp +++ b/samples/client/petstore/cpp-restsdk/client/Object.cpp @@ -27,7 +27,7 @@ Object::~Object() void Object::validate() { - // TODO: implement validation + } web::json::value Object::toJson() const @@ -35,12 +35,14 @@ web::json::value Object::toJson() const return m_object; } -void Object::fromJson(const web::json::value& val) +bool Object::fromJson(const web::json::value& val) { if (val.is_object()) { m_object = val; + m_IsSet = true; } + return isSet(); } void Object::toMultipart(std::shared_ptr multipart, const utility::string_t& prefix) const @@ -53,7 +55,7 @@ void Object::toMultipart(std::shared_ptr multipart, const uti multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("object"), m_object)); } -void Object::fromMultiPart(std::shared_ptr multipart, const utility::string_t& prefix) +bool Object::fromMultiPart(std::shared_ptr 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(".")) @@ -61,7 +63,11 @@ void Object::fromMultiPart(std::shared_ptr multipart, const u namePrefix += utility::conversions::to_string_t("."); } - m_object = ModelBase::valueFromHttpContent(multipart->getContent(namePrefix + utility::conversions::to_string_t("object"))); + if( ModelBase::fromHttpContent(multipart->getContent(namePrefix + utility::conversions::to_string_t("object")), m_object ) ) + { + m_IsSet = true; + } + return isSet(); } web::json::value Object::getValue(const utility::string_t& key) const @@ -72,7 +78,11 @@ web::json::value Object::getValue(const utility::string_t& key) const void Object::setValue(const utility::string_t& key, const web::json::value& value) { - m_object[key] = value; + if( !value.is_null() ) + { + m_object[key] = value; + m_IsSet = true; + } } } diff --git a/samples/client/petstore/cpp-restsdk/client/Object.h b/samples/client/petstore/cpp-restsdk/client/Object.h index 6f3a186ba51..982ac809a10 100644 --- a/samples/client/petstore/cpp-restsdk/client/Object.h +++ b/samples/client/petstore/cpp-restsdk/client/Object.h @@ -40,10 +40,10 @@ public: void validate() override; web::json::value toJson() const override; - void fromJson(const web::json::value& json) override; + bool fromJson(const web::json::value& json) override; void toMultipart(std::shared_ptr multipart, const utility::string_t& namePrefix) const override; - void fromMultiPart(std::shared_ptr multipart, const utility::string_t& namePrefix) override; + bool fromMultiPart(std::shared_ptr multipart, const utility::string_t& namePrefix) override; ///////////////////////////////////////////// /// Object manipulation diff --git a/samples/client/petstore/cpp-restsdk/client/README.md b/samples/client/petstore/cpp-restsdk/client/README.md index d3aca4f9bf2..66f1f50cf02 100644 --- a/samples/client/petstore/cpp-restsdk/client/README.md +++ b/samples/client/petstore/cpp-restsdk/client/README.md @@ -36,10 +36,10 @@ make - Once visual studio opens, CMake should show up in top menu bar. - Select CMake > Build All. -*Note: If the CMake menu item doesn't show up in Visual Studio, CMake -for Visual Studio must be installed. In this case, open the 'Visual Studio -Installer' application. Select 'modify' Visual Studio 2017. Make sure -'Desktop Development with C++' is installed, and specifically that 'Visual +*Note: If the CMake menu item doesn't show up in Visual Studio, CMake +for Visual Studio must be installed. In this case, open the 'Visual Studio +Installer' application. Select 'modify' Visual Studio 2017. Make sure +'Desktop Development with C++' is installed, and specifically that 'Visual C++ tools for CMake' is selected in the 'Installation Details' section. Also be sure to review the CMakeLists.txt file. Edits are likely required.* diff --git a/samples/client/petstore/cpp-restsdk/client/api/PetApi.cpp b/samples/client/petstore/cpp-restsdk/client/api/PetApi.cpp index bf7611ae8c8..6e861032b03 100644 --- a/samples/client/petstore/cpp-restsdk/client/api/PetApi.cpp +++ b/samples/client/petstore/cpp-restsdk/client/api/PetApi.cpp @@ -108,6 +108,7 @@ pplx::task PetApi::addPet(std::shared_ptr body) const { body->toMultipart(localVarMultipart, utility::conversions::to_string_t("body")); } + localVarHttpBody = localVarMultipart; localVarRequestHttpContentType += utility::conversions::to_string_t("; boundary=") + localVarMultipart->getBoundary(); @@ -373,15 +374,12 @@ pplx::task>> PetApi::findPetsByStatus(std::vect 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 localVarItemObj(new Pet()); - localVarItemObj->fromJson(localVarItem); + std::shared_ptr localVarItemObj; + ModelBase::fromJson(localVarItem, localVarItemObj); localVarResult.push_back(localVarItemObj); - } - } // else if(localVarResponseHttpContentType == utility::conversions::to_string_t("multipart/form-data")) // { @@ -504,15 +502,12 @@ pplx::task>> PetApi::findPetsByTags(std::vector 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 localVarItemObj(new Pet()); - localVarItemObj->fromJson(localVarItem); + std::shared_ptr localVarItemObj; + ModelBase::fromJson(localVarItem, localVarItemObj); localVarResult.push_back(localVarItemObj); - } - } // else if(localVarResponseHttpContentType == utility::conversions::to_string_t("multipart/form-data")) // { @@ -640,7 +635,7 @@ pplx::task> PetApi::getPetById(int64_t petId) const { web::json::value localVarJson = web::json::value::parse(localVarResponse); - localVarResult->fromJson(localVarJson); + ModelBase::fromJson(localVarJson, localVarResult); } // else if(localVarResponseHttpContentType == utility::conversions::to_string_t("multipart/form-data")) // { @@ -728,6 +723,7 @@ pplx::task PetApi::updatePet(std::shared_ptr body) const { body->toMultipart(localVarMultipart, utility::conversions::to_string_t("body")); } + localVarHttpBody = localVarMultipart; localVarRequestHttpContentType += utility::conversions::to_string_t("; boundary=") + localVarMultipart->getBoundary(); @@ -1005,7 +1001,7 @@ pplx::task> PetApi::uploadFile(int64_t petId, boost { web::json::value localVarJson = web::json::value::parse(localVarResponse); - localVarResult->fromJson(localVarJson); + ModelBase::fromJson(localVarJson, localVarResult); } // else if(localVarResponseHttpContentType == utility::conversions::to_string_t("multipart/form-data")) // { diff --git a/samples/client/petstore/cpp-restsdk/client/api/StoreApi.cpp b/samples/client/petstore/cpp-restsdk/client/api/StoreApi.cpp index 994335d4fe2..50f8e56cc92 100644 --- a/samples/client/petstore/cpp-restsdk/client/api/StoreApi.cpp +++ b/samples/client/petstore/cpp-restsdk/client/api/StoreApi.cpp @@ -248,10 +248,10 @@ pplx::task> StoreApi::getInventory() const for( auto& localVarItem : localVarJson.as_object() ) { - localVarResult[localVarItem.first] = ModelBase::int32_tFromJson(localVarItem.second); - + int32_t localVarItemObj; + ModelBase::fromJson(localVarItem.second, localVarItemObj); + localVarResult[localVarItem.first] = localVarItemObj; } - } // else if(localVarResponseHttpContentType == utility::conversions::to_string_t("multipart/form-data")) // { @@ -371,7 +371,7 @@ pplx::task> StoreApi::getOrderById(int64_t orderId) const { web::json::value localVarJson = web::json::value::parse(localVarResponse); - localVarResult->fromJson(localVarJson); + ModelBase::fromJson(localVarJson, localVarResult); } // else if(localVarResponseHttpContentType == utility::conversions::to_string_t("multipart/form-data")) // { @@ -459,6 +459,7 @@ pplx::task> StoreApi::placeOrder(std::shared_ptr b { body->toMultipart(localVarMultipart, utility::conversions::to_string_t("body")); } + localVarHttpBody = localVarMultipart; localVarRequestHttpContentType += utility::conversions::to_string_t("; boundary=") + localVarMultipart->getBoundary(); @@ -511,7 +512,7 @@ pplx::task> StoreApi::placeOrder(std::shared_ptr b { web::json::value localVarJson = web::json::value::parse(localVarResponse); - localVarResult->fromJson(localVarJson); + ModelBase::fromJson(localVarJson, localVarResult); } // else if(localVarResponseHttpContentType == utility::conversions::to_string_t("multipart/form-data")) // { diff --git a/samples/client/petstore/cpp-restsdk/client/api/UserApi.cpp b/samples/client/petstore/cpp-restsdk/client/api/UserApi.cpp index 7f2ffec929a..0e88a16d991 100644 --- a/samples/client/petstore/cpp-restsdk/client/api/UserApi.cpp +++ b/samples/client/petstore/cpp-restsdk/client/api/UserApi.cpp @@ -106,6 +106,7 @@ pplx::task UserApi::createUser(std::shared_ptr body) const { body->toMultipart(localVarMultipart, utility::conversions::to_string_t("body")); } + localVarHttpBody = localVarMultipart; localVarRequestHttpContentType += utility::conversions::to_string_t("; boundary=") + localVarMultipart->getBoundary(); @@ -222,16 +223,17 @@ pplx::task UserApi::createUsersWithArrayInput(std::vector localVarMultipart(new MultipartFormData); + { std::vector localVarJsonArray; for( auto& localVarItem : body ) { - localVarJsonArray.push_back( localVarItem.get() ? localVarItem->toJson() : web::json::value::null() ); - + localVarJsonArray.push_back(ModelBase::toJson(localVarItem)); } - localVarMultipart->add(ModelBase::toHttpContent(utility::conversions::to_string_t("body"), web::json::value::array(localVarJsonArray), utility::conversions::to_string_t("application/json"))); + localVarMultipart->add(ModelBase::toHttpContent(utility::conversions::to_string_t("body"), localVarJsonArray, utility::conversions::to_string_t("application/json"))); } + localVarHttpBody = localVarMultipart; localVarRequestHttpContentType += utility::conversions::to_string_t("; boundary=") + localVarMultipart->getBoundary(); } @@ -347,16 +349,17 @@ pplx::task UserApi::createUsersWithListInput(std::vector localVarMultipart(new MultipartFormData); + { std::vector localVarJsonArray; for( auto& localVarItem : body ) { - localVarJsonArray.push_back( localVarItem.get() ? localVarItem->toJson() : web::json::value::null() ); - + localVarJsonArray.push_back(ModelBase::toJson(localVarItem)); } - localVarMultipart->add(ModelBase::toHttpContent(utility::conversions::to_string_t("body"), web::json::value::array(localVarJsonArray), utility::conversions::to_string_t("application/json"))); + localVarMultipart->add(ModelBase::toHttpContent(utility::conversions::to_string_t("body"), localVarJsonArray, utility::conversions::to_string_t("application/json"))); } + localVarHttpBody = localVarMultipart; localVarRequestHttpContentType += utility::conversions::to_string_t("; boundary=") + localVarMultipart->getBoundary(); } @@ -610,7 +613,7 @@ pplx::task> UserApi::getUserByName(utility::string_t usern { web::json::value localVarJson = web::json::value::parse(localVarResponse); - localVarResult->fromJson(localVarJson); + ModelBase::fromJson(localVarJson, localVarResult); } // else if(localVarResponseHttpContentType == utility::conversions::to_string_t("multipart/form-data")) // { @@ -740,8 +743,7 @@ pplx::task UserApi::loginUser(utility::string_t username, uti { web::json::value localVarJson = web::json::value::parse(localVarResponse); - localVarResult = ModelBase::stringFromJson(localVarJson); - + ModelBase::fromJson(localVarJson, localVarResult); } else if(localVarResponseHttpContentType == utility::conversions::to_string_t("text/plain")) { @@ -931,6 +933,7 @@ pplx::task UserApi::updateUser(utility::string_t username, std::shared_ptr { body->toMultipart(localVarMultipart, utility::conversions::to_string_t("body")); } + localVarHttpBody = localVarMultipart; localVarRequestHttpContentType += utility::conversions::to_string_t("; boundary=") + localVarMultipart->getBoundary(); diff --git a/samples/client/petstore/cpp-restsdk/client/model/ApiResponse.cpp b/samples/client/petstore/cpp-restsdk/client/model/ApiResponse.cpp index 2e1ba85a6c2..d5b78d5cc59 100644 --- a/samples/client/petstore/cpp-restsdk/client/model/ApiResponse.cpp +++ b/samples/client/petstore/cpp-restsdk/client/model/ApiResponse.cpp @@ -42,8 +42,9 @@ void ApiResponse::validate() web::json::value ApiResponse::toJson() const { - web::json::value val = web::json::value::object(); + web::json::value val = web::json::value::object(); + if(m_CodeIsSet) { val[utility::conversions::to_string_t("code")] = ModelBase::toJson(m_Code); @@ -60,14 +61,18 @@ web::json::value ApiResponse::toJson() const return val; } -void ApiResponse::fromJson(const web::json::value& val) +bool ApiResponse::fromJson(const web::json::value& val) { + bool ok = true; + if(val.has_field(utility::conversions::to_string_t("code"))) { const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("code")); if(!fieldValue.is_null()) { - setCode(ModelBase::int32_tFromJson(fieldValue)); + int32_t refVal_code; + ok &= ModelBase::fromJson(fieldValue, refVal_code); + setCode(refVal_code); } } if(val.has_field(utility::conversions::to_string_t("type"))) @@ -75,7 +80,9 @@ void ApiResponse::fromJson(const web::json::value& val) const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("type")); if(!fieldValue.is_null()) { - setType(ModelBase::stringFromJson(fieldValue)); + utility::string_t refVal_type; + ok &= ModelBase::fromJson(fieldValue, refVal_type); + setType(refVal_type); } } if(val.has_field(utility::conversions::to_string_t("message"))) @@ -83,9 +90,12 @@ void ApiResponse::fromJson(const web::json::value& val) const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("message")); if(!fieldValue.is_null()) { - setMessage(ModelBase::stringFromJson(fieldValue)); + utility::string_t refVal_message; + ok &= ModelBase::fromJson(fieldValue, refVal_message); + setMessage(refVal_message); } } + return ok; } void ApiResponse::toMultipart(std::shared_ptr multipart, const utility::string_t& prefix) const @@ -95,7 +105,6 @@ void ApiResponse::toMultipart(std::shared_ptr multipart, cons { namePrefix += utility::conversions::to_string_t("."); } - if(m_CodeIsSet) { multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("code"), m_Code)); @@ -110,8 +119,9 @@ void ApiResponse::toMultipart(std::shared_ptr multipart, cons } } -void ApiResponse::fromMultiPart(std::shared_ptr multipart, const utility::string_t& prefix) +bool ApiResponse::fromMultiPart(std::shared_ptr 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(".")) { @@ -120,16 +130,23 @@ void ApiResponse::fromMultiPart(std::shared_ptr multipart, co if(multipart->hasContent(utility::conversions::to_string_t("code"))) { - setCode(ModelBase::int32_tFromHttpContent(multipart->getContent(utility::conversions::to_string_t("code")))); + int32_t refVal_code; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("code")), refVal_code ); + setCode(refVal_code); } if(multipart->hasContent(utility::conversions::to_string_t("type"))) { - setType(ModelBase::stringFromHttpContent(multipart->getContent(utility::conversions::to_string_t("type")))); + utility::string_t refVal_type; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("type")), refVal_type ); + setType(refVal_type); } if(multipart->hasContent(utility::conversions::to_string_t("message"))) { - setMessage(ModelBase::stringFromHttpContent(multipart->getContent(utility::conversions::to_string_t("message")))); + utility::string_t refVal_message; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("message")), refVal_message ); + setMessage(refVal_message); } + return ok; } int32_t ApiResponse::getCode() const @@ -152,7 +169,6 @@ void ApiResponse::unsetCode() { m_CodeIsSet = false; } - utility::string_t ApiResponse::getType() const { return m_Type; @@ -173,7 +189,6 @@ void ApiResponse::unsetType() { m_TypeIsSet = false; } - utility::string_t ApiResponse::getMessage() const { return m_Message; @@ -194,7 +209,6 @@ void ApiResponse::unsetMessage() { m_MessageIsSet = false; } - } } } diff --git a/samples/client/petstore/cpp-restsdk/client/model/ApiResponse.h b/samples/client/petstore/cpp-restsdk/client/model/ApiResponse.h index 530171c4aa7..56ba9242575 100644 --- a/samples/client/petstore/cpp-restsdk/client/model/ApiResponse.h +++ b/samples/client/petstore/cpp-restsdk/client/model/ApiResponse.h @@ -45,10 +45,10 @@ public: void validate() override; web::json::value toJson() const override; - void fromJson(const web::json::value& json) override; + bool fromJson(const web::json::value& json) override; void toMultipart(std::shared_ptr multipart, const utility::string_t& namePrefix) const override; - void fromMultiPart(std::shared_ptr multipart, const utility::string_t& namePrefix) override; + bool fromMultiPart(std::shared_ptr multipart, const utility::string_t& namePrefix) override; ///////////////////////////////////////////// /// ApiResponse members diff --git a/samples/client/petstore/cpp-restsdk/client/model/Category.cpp b/samples/client/petstore/cpp-restsdk/client/model/Category.cpp index e710f6448c9..fa72f22cb2b 100644 --- a/samples/client/petstore/cpp-restsdk/client/model/Category.cpp +++ b/samples/client/petstore/cpp-restsdk/client/model/Category.cpp @@ -40,8 +40,9 @@ void Category::validate() web::json::value Category::toJson() const { - web::json::value val = web::json::value::object(); + web::json::value val = web::json::value::object(); + if(m_IdIsSet) { val[utility::conversions::to_string_t("id")] = ModelBase::toJson(m_Id); @@ -54,14 +55,18 @@ web::json::value Category::toJson() const return val; } -void Category::fromJson(const web::json::value& val) +bool Category::fromJson(const web::json::value& val) { + bool ok = true; + if(val.has_field(utility::conversions::to_string_t("id"))) { const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("id")); if(!fieldValue.is_null()) { - setId(ModelBase::int64_tFromJson(fieldValue)); + int64_t refVal_id; + ok &= ModelBase::fromJson(fieldValue, refVal_id); + setId(refVal_id); } } if(val.has_field(utility::conversions::to_string_t("name"))) @@ -69,9 +74,12 @@ void Category::fromJson(const web::json::value& val) const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("name")); if(!fieldValue.is_null()) { - setName(ModelBase::stringFromJson(fieldValue)); + utility::string_t refVal_name; + ok &= ModelBase::fromJson(fieldValue, refVal_name); + setName(refVal_name); } } + return ok; } void Category::toMultipart(std::shared_ptr multipart, const utility::string_t& prefix) const @@ -81,7 +89,6 @@ void Category::toMultipart(std::shared_ptr multipart, const u { namePrefix += utility::conversions::to_string_t("."); } - if(m_IdIsSet) { multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("id"), m_Id)); @@ -92,8 +99,9 @@ void Category::toMultipart(std::shared_ptr multipart, const u } } -void Category::fromMultiPart(std::shared_ptr multipart, const utility::string_t& prefix) +bool Category::fromMultiPart(std::shared_ptr 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(".")) { @@ -102,12 +110,17 @@ void Category::fromMultiPart(std::shared_ptr multipart, const if(multipart->hasContent(utility::conversions::to_string_t("id"))) { - setId(ModelBase::int64_tFromHttpContent(multipart->getContent(utility::conversions::to_string_t("id")))); + int64_t refVal_id; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("id")), refVal_id ); + setId(refVal_id); } if(multipart->hasContent(utility::conversions::to_string_t("name"))) { - setName(ModelBase::stringFromHttpContent(multipart->getContent(utility::conversions::to_string_t("name")))); + utility::string_t refVal_name; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("name")), refVal_name ); + setName(refVal_name); } + return ok; } int64_t Category::getId() const @@ -130,7 +143,6 @@ void Category::unsetId() { m_IdIsSet = false; } - utility::string_t Category::getName() const { return m_Name; @@ -151,7 +163,6 @@ void Category::unsetName() { m_NameIsSet = false; } - } } } diff --git a/samples/client/petstore/cpp-restsdk/client/model/Category.h b/samples/client/petstore/cpp-restsdk/client/model/Category.h index f35054c6d45..3be2acc0498 100644 --- a/samples/client/petstore/cpp-restsdk/client/model/Category.h +++ b/samples/client/petstore/cpp-restsdk/client/model/Category.h @@ -45,10 +45,10 @@ public: void validate() override; web::json::value toJson() const override; - void fromJson(const web::json::value& json) override; + bool fromJson(const web::json::value& json) override; void toMultipart(std::shared_ptr multipart, const utility::string_t& namePrefix) const override; - void fromMultiPart(std::shared_ptr multipart, const utility::string_t& namePrefix) override; + bool fromMultiPart(std::shared_ptr multipart, const utility::string_t& namePrefix) override; ///////////////////////////////////////////// /// Category members diff --git a/samples/client/petstore/cpp-restsdk/client/model/Order.cpp b/samples/client/petstore/cpp-restsdk/client/model/Order.cpp index f3f6659de71..553d9ce5f9c 100644 --- a/samples/client/petstore/cpp-restsdk/client/model/Order.cpp +++ b/samples/client/petstore/cpp-restsdk/client/model/Order.cpp @@ -48,8 +48,9 @@ void Order::validate() web::json::value Order::toJson() const { - web::json::value val = web::json::value::object(); + web::json::value val = web::json::value::object(); + if(m_IdIsSet) { val[utility::conversions::to_string_t("id")] = ModelBase::toJson(m_Id); @@ -78,14 +79,18 @@ web::json::value Order::toJson() const return val; } -void Order::fromJson(const web::json::value& val) +bool Order::fromJson(const web::json::value& val) { + bool ok = true; + if(val.has_field(utility::conversions::to_string_t("id"))) { const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("id")); if(!fieldValue.is_null()) { - setId(ModelBase::int64_tFromJson(fieldValue)); + int64_t refVal_id; + ok &= ModelBase::fromJson(fieldValue, refVal_id); + setId(refVal_id); } } if(val.has_field(utility::conversions::to_string_t("petId"))) @@ -93,7 +98,9 @@ void Order::fromJson(const web::json::value& val) const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("petId")); if(!fieldValue.is_null()) { - setPetId(ModelBase::int64_tFromJson(fieldValue)); + int64_t refVal_petId; + ok &= ModelBase::fromJson(fieldValue, refVal_petId); + setPetId(refVal_petId); } } if(val.has_field(utility::conversions::to_string_t("quantity"))) @@ -101,7 +108,9 @@ void Order::fromJson(const web::json::value& val) const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("quantity")); if(!fieldValue.is_null()) { - setQuantity(ModelBase::int32_tFromJson(fieldValue)); + int32_t refVal_quantity; + ok &= ModelBase::fromJson(fieldValue, refVal_quantity); + setQuantity(refVal_quantity); } } if(val.has_field(utility::conversions::to_string_t("shipDate"))) @@ -109,7 +118,9 @@ void Order::fromJson(const web::json::value& val) const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("shipDate")); if(!fieldValue.is_null()) { - setShipDate(ModelBase::dateFromJson(fieldValue)); + utility::datetime refVal_shipDate; + ok &= ModelBase::fromJson(fieldValue, refVal_shipDate); + setShipDate(refVal_shipDate); } } if(val.has_field(utility::conversions::to_string_t("status"))) @@ -117,7 +128,9 @@ void Order::fromJson(const web::json::value& val) const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("status")); if(!fieldValue.is_null()) { - setStatus(ModelBase::stringFromJson(fieldValue)); + utility::string_t refVal_status; + ok &= ModelBase::fromJson(fieldValue, refVal_status); + setStatus(refVal_status); } } if(val.has_field(utility::conversions::to_string_t("complete"))) @@ -125,9 +138,12 @@ void Order::fromJson(const web::json::value& val) const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("complete")); if(!fieldValue.is_null()) { - setComplete(ModelBase::boolFromJson(fieldValue)); + bool refVal_complete; + ok &= ModelBase::fromJson(fieldValue, refVal_complete); + setComplete(refVal_complete); } } + return ok; } void Order::toMultipart(std::shared_ptr multipart, const utility::string_t& prefix) const @@ -137,7 +153,6 @@ void Order::toMultipart(std::shared_ptr multipart, const util { namePrefix += utility::conversions::to_string_t("."); } - if(m_IdIsSet) { multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("id"), m_Id)); @@ -164,8 +179,9 @@ void Order::toMultipart(std::shared_ptr multipart, const util } } -void Order::fromMultiPart(std::shared_ptr multipart, const utility::string_t& prefix) +bool Order::fromMultiPart(std::shared_ptr 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(".")) { @@ -174,28 +190,41 @@ void Order::fromMultiPart(std::shared_ptr multipart, const ut if(multipart->hasContent(utility::conversions::to_string_t("id"))) { - setId(ModelBase::int64_tFromHttpContent(multipart->getContent(utility::conversions::to_string_t("id")))); + int64_t refVal_id; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("id")), refVal_id ); + setId(refVal_id); } if(multipart->hasContent(utility::conversions::to_string_t("petId"))) { - setPetId(ModelBase::int64_tFromHttpContent(multipart->getContent(utility::conversions::to_string_t("petId")))); + int64_t refVal_petId; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("petId")), refVal_petId ); + setPetId(refVal_petId); } if(multipart->hasContent(utility::conversions::to_string_t("quantity"))) { - setQuantity(ModelBase::int32_tFromHttpContent(multipart->getContent(utility::conversions::to_string_t("quantity")))); + int32_t refVal_quantity; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("quantity")), refVal_quantity ); + setQuantity(refVal_quantity); } if(multipart->hasContent(utility::conversions::to_string_t("shipDate"))) { - setShipDate(ModelBase::dateFromHttpContent(multipart->getContent(utility::conversions::to_string_t("shipDate")))); + utility::datetime refVal_shipDate; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("shipDate")), refVal_shipDate ); + setShipDate(refVal_shipDate); } if(multipart->hasContent(utility::conversions::to_string_t("status"))) { - setStatus(ModelBase::stringFromHttpContent(multipart->getContent(utility::conversions::to_string_t("status")))); + utility::string_t refVal_status; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("status")), refVal_status ); + setStatus(refVal_status); } if(multipart->hasContent(utility::conversions::to_string_t("complete"))) { - setComplete(ModelBase::boolFromHttpContent(multipart->getContent(utility::conversions::to_string_t("complete")))); + bool refVal_complete; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("complete")), refVal_complete ); + setComplete(refVal_complete); } + return ok; } int64_t Order::getId() const @@ -218,7 +247,6 @@ void Order::unsetId() { m_IdIsSet = false; } - int64_t Order::getPetId() const { return m_PetId; @@ -239,7 +267,6 @@ void Order::unsetPetId() { m_PetIdIsSet = false; } - int32_t Order::getQuantity() const { return m_Quantity; @@ -260,7 +287,6 @@ void Order::unsetQuantity() { m_QuantityIsSet = false; } - utility::datetime Order::getShipDate() const { return m_ShipDate; @@ -281,7 +307,6 @@ void Order::unsetShipDate() { m_ShipDateIsSet = false; } - utility::string_t Order::getStatus() const { return m_Status; @@ -302,7 +327,6 @@ void Order::unsetStatus() { m_StatusIsSet = false; } - bool Order::isComplete() const { return m_Complete; @@ -323,7 +347,6 @@ void Order::unsetComplete() { m_CompleteIsSet = false; } - } } } diff --git a/samples/client/petstore/cpp-restsdk/client/model/Order.h b/samples/client/petstore/cpp-restsdk/client/model/Order.h index d1d2239d7f8..9daab29402c 100644 --- a/samples/client/petstore/cpp-restsdk/client/model/Order.h +++ b/samples/client/petstore/cpp-restsdk/client/model/Order.h @@ -45,10 +45,10 @@ public: void validate() override; web::json::value toJson() const override; - void fromJson(const web::json::value& json) override; + bool fromJson(const web::json::value& json) override; void toMultipart(std::shared_ptr multipart, const utility::string_t& namePrefix) const override; - void fromMultiPart(std::shared_ptr multipart, const utility::string_t& namePrefix) override; + bool fromMultiPart(std::shared_ptr multipart, const utility::string_t& namePrefix) override; ///////////////////////////////////////////// /// Order members diff --git a/samples/client/petstore/cpp-restsdk/client/model/Pet.cpp b/samples/client/petstore/cpp-restsdk/client/model/Pet.cpp index 728c051d5cd..0a9c66cdfbc 100644 --- a/samples/client/petstore/cpp-restsdk/client/model/Pet.cpp +++ b/samples/client/petstore/cpp-restsdk/client/model/Pet.cpp @@ -27,6 +27,8 @@ Pet::Pet() m_IdIsSet = false; m_CategoryIsSet = false; m_Name = utility::conversions::to_string_t(""); + m_NameIsSet = false; + m_PhotoUrlsIsSet = false; m_TagsIsSet = false; m_Status = utility::conversions::to_string_t(""); m_StatusIsSet = false; @@ -43,8 +45,9 @@ void Pet::validate() web::json::value Pet::toJson() const { - web::json::value val = web::json::value::object(); + web::json::value val = web::json::value::object(); + if(m_IdIsSet) { val[utility::conversions::to_string_t("id")] = ModelBase::toJson(m_Id); @@ -53,25 +56,17 @@ web::json::value Pet::toJson() const { val[utility::conversions::to_string_t("category")] = ModelBase::toJson(m_Category); } - val[utility::conversions::to_string_t("name")] = ModelBase::toJson(m_Name); + if(m_NameIsSet) { - std::vector jsonArray; - for( auto& item : m_PhotoUrls ) - { - jsonArray.push_back(ModelBase::toJson(item)); - } - val[utility::conversions::to_string_t("photoUrls")] = web::json::value::array(jsonArray); + val[utility::conversions::to_string_t("name")] = ModelBase::toJson(m_Name); } + if(m_PhotoUrlsIsSet) { - std::vector jsonArray; - for( auto& item : m_Tags ) - { - jsonArray.push_back(ModelBase::toJson(item)); - } - if(jsonArray.size() > 0) - { - val[utility::conversions::to_string_t("tags")] = web::json::value::array(jsonArray); - } + val[utility::conversions::to_string_t("photoUrls")] = ModelBase::toJson(m_PhotoUrls); + } + if(m_TagsIsSet) + { + val[utility::conversions::to_string_t("tags")] = ModelBase::toJson(m_Tags); } if(m_StatusIsSet) { @@ -81,14 +76,18 @@ web::json::value Pet::toJson() const return val; } -void Pet::fromJson(const web::json::value& val) +bool Pet::fromJson(const web::json::value& val) { + bool ok = true; + if(val.has_field(utility::conversions::to_string_t("id"))) { const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("id")); if(!fieldValue.is_null()) { - setId(ModelBase::int64_tFromJson(fieldValue)); + int64_t refVal_id; + ok &= ModelBase::fromJson(fieldValue, refVal_id); + setId(refVal_id); } } if(val.has_field(utility::conversions::to_string_t("category"))) @@ -96,38 +95,39 @@ void Pet::fromJson(const web::json::value& val) const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("category")); if(!fieldValue.is_null()) { - auto newItem = std::make_shared::element_type>(); - newItem->fromJson(fieldValue); - setCategory( newItem ); + std::shared_ptr refVal_category; + ok &= ModelBase::fromJson(fieldValue, refVal_category); + setCategory(refVal_category); } } - setName(ModelBase::stringFromJson(val.at(utility::conversions::to_string_t("name")))); + if(val.has_field(utility::conversions::to_string_t("name"))) { - m_PhotoUrls.clear(); - std::vector jsonArray; - for( auto& item : val.at(utility::conversions::to_string_t("photoUrls")).as_array() ) + const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("name")); + if(!fieldValue.is_null()) { - m_PhotoUrls.push_back(ModelBase::stringFromJson(item)); + utility::string_t refVal_name; + ok &= ModelBase::fromJson(fieldValue, refVal_name); + setName(refVal_name); } } + if(val.has_field(utility::conversions::to_string_t("photoUrls"))) { - m_Tags.clear(); - std::vector jsonArray; - if(val.has_field(utility::conversions::to_string_t("tags"))) + const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("photoUrls")); + if(!fieldValue.is_null()) { - for( auto& item : val.at(utility::conversions::to_string_t("tags")).as_array() ) - { - if(item.is_null()) - { - m_Tags.push_back( std::shared_ptr(nullptr) ); - } - else - { - auto newItem = std::make_shared::element_type>(); - newItem->fromJson(item); - m_Tags.push_back( newItem ); - } + std::vector refVal_photoUrls; + ok &= ModelBase::fromJson(fieldValue, refVal_photoUrls); + setPhotoUrls(refVal_photoUrls); } + } + if(val.has_field(utility::conversions::to_string_t("tags"))) + { + const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("tags")); + if(!fieldValue.is_null()) + { + std::vector> refVal_tags; + ok &= ModelBase::fromJson(fieldValue, refVal_tags); + setTags(refVal_tags); } } if(val.has_field(utility::conversions::to_string_t("status"))) @@ -135,9 +135,12 @@ void Pet::fromJson(const web::json::value& val) const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("status")); if(!fieldValue.is_null()) { - setStatus(ModelBase::stringFromJson(fieldValue)); + utility::string_t refVal_status; + ok &= ModelBase::fromJson(fieldValue, refVal_status); + setStatus(refVal_status); } } + return ok; } void Pet::toMultipart(std::shared_ptr multipart, const utility::string_t& prefix) const @@ -147,38 +150,25 @@ void Pet::toMultipart(std::shared_ptr multipart, const utilit { namePrefix += utility::conversions::to_string_t("."); } - if(m_IdIsSet) { multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("id"), m_Id)); } if(m_CategoryIsSet) { - if (m_Category.get()) - { - m_Category->toMultipart(multipart, utility::conversions::to_string_t("category.")); - } + multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("category"), m_Category)); } - multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("name"), m_Name)); + if(m_NameIsSet) { - std::vector jsonArray; - for( auto& item : m_PhotoUrls ) - { - jsonArray.push_back(ModelBase::toJson(item)); - } - multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("photoUrls"), web::json::value::array(jsonArray), utility::conversions::to_string_t("application/json"))); - } + multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("name"), m_Name)); + } + if(m_PhotoUrlsIsSet) { - std::vector jsonArray; - for( auto& item : m_Tags ) - { - jsonArray.push_back(ModelBase::toJson(item)); - } - - if(jsonArray.size() > 0) - { - multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("tags"), web::json::value::array(jsonArray), utility::conversions::to_string_t("application/json"))); - } + multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("photoUrls"), m_PhotoUrls)); + } + if(m_TagsIsSet) + { + multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("tags"), m_Tags)); } if(m_StatusIsSet) { @@ -186,8 +176,9 @@ void Pet::toMultipart(std::shared_ptr multipart, const utilit } } -void Pet::fromMultiPart(std::shared_ptr multipart, const utility::string_t& prefix) +bool Pet::fromMultiPart(std::shared_ptr 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(".")) { @@ -196,52 +187,41 @@ void Pet::fromMultiPart(std::shared_ptr multipart, const util if(multipart->hasContent(utility::conversions::to_string_t("id"))) { - setId(ModelBase::int64_tFromHttpContent(multipart->getContent(utility::conversions::to_string_t("id")))); + int64_t refVal_id; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("id")), refVal_id ); + setId(refVal_id); } if(multipart->hasContent(utility::conversions::to_string_t("category"))) { - if(multipart->hasContent(utility::conversions::to_string_t("category"))) - { - auto newItem = std::make_shared::element_type>(); - newItem->fromMultiPart(multipart, utility::conversions::to_string_t("category.")); - setCategory( newItem ); - } + std::shared_ptr refVal_category; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("category")), refVal_category ); + setCategory(refVal_category); } - setName(ModelBase::stringFromHttpContent(multipart->getContent(utility::conversions::to_string_t("name")))); + if(multipart->hasContent(utility::conversions::to_string_t("name"))) { - m_PhotoUrls.clear(); - - web::json::value jsonArray = web::json::value::parse(ModelBase::stringFromHttpContent(multipart->getContent(utility::conversions::to_string_t("photoUrls")))); - for( auto& item : jsonArray.as_array() ) - { - m_PhotoUrls.push_back(ModelBase::stringFromJson(item)); - } + utility::string_t refVal_name; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("name")), refVal_name ); + setName(refVal_name); } + if(multipart->hasContent(utility::conversions::to_string_t("photoUrls"))) { - m_Tags.clear(); - if(multipart->hasContent(utility::conversions::to_string_t("tags"))) - { - - web::json::value jsonArray = web::json::value::parse(ModelBase::stringFromHttpContent(multipart->getContent(utility::conversions::to_string_t("tags")))); - for( auto& item : jsonArray.as_array() ) - { - if(item.is_null()) - { - m_Tags.push_back( std::shared_ptr(nullptr) ); - } - else - { - auto newItem = std::make_shared::element_type>(); - newItem->fromJson(item); - m_Tags.push_back( newItem ); - } - } - } + std::vector refVal_photoUrls; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("photoUrls")), refVal_photoUrls ); + setPhotoUrls(refVal_photoUrls); + } + if(multipart->hasContent(utility::conversions::to_string_t("tags"))) + { + std::vector> refVal_tags; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("tags")), refVal_tags ); + setTags(refVal_tags); } if(multipart->hasContent(utility::conversions::to_string_t("status"))) { - setStatus(ModelBase::stringFromHttpContent(multipart->getContent(utility::conversions::to_string_t("status")))); + utility::string_t refVal_status; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("status")), refVal_status ); + setStatus(refVal_status); } + return ok; } int64_t Pet::getId() const @@ -264,7 +244,6 @@ void Pet::unsetId() { m_IdIsSet = false; } - std::shared_ptr Pet::getCategory() const { return m_Category; @@ -285,7 +264,6 @@ void Pet::unsetCategory() { m_CategoryIsSet = false; } - utility::string_t Pet::getName() const { return m_Name; @@ -294,9 +272,18 @@ utility::string_t Pet::getName() const void Pet::setName(const utility::string_t& value) { m_Name = value; - + m_NameIsSet = true; } +bool Pet::nameIsSet() const +{ + return m_NameIsSet; +} + +void Pet::unsetName() +{ + m_NameIsSet = false; +} std::vector& Pet::getPhotoUrls() { return m_PhotoUrls; @@ -305,9 +292,18 @@ std::vector& Pet::getPhotoUrls() void Pet::setPhotoUrls(const std::vector& value) { m_PhotoUrls = value; - + m_PhotoUrlsIsSet = true; } +bool Pet::photoUrlsIsSet() const +{ + return m_PhotoUrlsIsSet; +} + +void Pet::unsetPhotoUrls() +{ + m_PhotoUrlsIsSet = false; +} std::vector>& Pet::getTags() { return m_Tags; @@ -328,7 +324,6 @@ void Pet::unsetTags() { m_TagsIsSet = false; } - utility::string_t Pet::getStatus() const { return m_Status; @@ -349,7 +344,6 @@ void Pet::unsetStatus() { m_StatusIsSet = false; } - } } } diff --git a/samples/client/petstore/cpp-restsdk/client/model/Pet.h b/samples/client/petstore/cpp-restsdk/client/model/Pet.h index bbc0a09cac6..100107c93c3 100644 --- a/samples/client/petstore/cpp-restsdk/client/model/Pet.h +++ b/samples/client/petstore/cpp-restsdk/client/model/Pet.h @@ -48,10 +48,10 @@ public: void validate() override; web::json::value toJson() const override; - void fromJson(const web::json::value& json) override; + bool fromJson(const web::json::value& json) override; void toMultipart(std::shared_ptr multipart, const utility::string_t& namePrefix) const override; - void fromMultiPart(std::shared_ptr multipart, const utility::string_t& namePrefix) override; + bool fromMultiPart(std::shared_ptr multipart, const utility::string_t& namePrefix) override; ///////////////////////////////////////////// /// Pet members @@ -78,14 +78,18 @@ public: /// /// utility::string_t getName() const; - + bool nameIsSet() const; + void unsetName(); + void setName(const utility::string_t& value); /// /// /// std::vector& getPhotoUrls(); - + bool photoUrlsIsSet() const; + void unsetPhotoUrls(); + void setPhotoUrls(const std::vector& value); /// @@ -113,8 +117,10 @@ protected: std::shared_ptr m_Category; bool m_CategoryIsSet; utility::string_t m_Name; - std::vector m_PhotoUrls; - std::vector> m_Tags; + bool m_NameIsSet; + std::vector m_PhotoUrls; + bool m_PhotoUrlsIsSet; + std::vector> m_Tags; bool m_TagsIsSet; utility::string_t m_Status; bool m_StatusIsSet; diff --git a/samples/client/petstore/cpp-restsdk/client/model/Tag.cpp b/samples/client/petstore/cpp-restsdk/client/model/Tag.cpp index b1a7598237e..fb5924de7d4 100644 --- a/samples/client/petstore/cpp-restsdk/client/model/Tag.cpp +++ b/samples/client/petstore/cpp-restsdk/client/model/Tag.cpp @@ -40,8 +40,9 @@ void Tag::validate() web::json::value Tag::toJson() const { - web::json::value val = web::json::value::object(); + web::json::value val = web::json::value::object(); + if(m_IdIsSet) { val[utility::conversions::to_string_t("id")] = ModelBase::toJson(m_Id); @@ -54,14 +55,18 @@ web::json::value Tag::toJson() const return val; } -void Tag::fromJson(const web::json::value& val) +bool Tag::fromJson(const web::json::value& val) { + bool ok = true; + if(val.has_field(utility::conversions::to_string_t("id"))) { const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("id")); if(!fieldValue.is_null()) { - setId(ModelBase::int64_tFromJson(fieldValue)); + int64_t refVal_id; + ok &= ModelBase::fromJson(fieldValue, refVal_id); + setId(refVal_id); } } if(val.has_field(utility::conversions::to_string_t("name"))) @@ -69,9 +74,12 @@ void Tag::fromJson(const web::json::value& val) const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("name")); if(!fieldValue.is_null()) { - setName(ModelBase::stringFromJson(fieldValue)); + utility::string_t refVal_name; + ok &= ModelBase::fromJson(fieldValue, refVal_name); + setName(refVal_name); } } + return ok; } void Tag::toMultipart(std::shared_ptr multipart, const utility::string_t& prefix) const @@ -81,7 +89,6 @@ void Tag::toMultipart(std::shared_ptr multipart, const utilit { namePrefix += utility::conversions::to_string_t("."); } - if(m_IdIsSet) { multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("id"), m_Id)); @@ -92,8 +99,9 @@ void Tag::toMultipart(std::shared_ptr multipart, const utilit } } -void Tag::fromMultiPart(std::shared_ptr multipart, const utility::string_t& prefix) +bool Tag::fromMultiPart(std::shared_ptr 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(".")) { @@ -102,12 +110,17 @@ void Tag::fromMultiPart(std::shared_ptr multipart, const util if(multipart->hasContent(utility::conversions::to_string_t("id"))) { - setId(ModelBase::int64_tFromHttpContent(multipart->getContent(utility::conversions::to_string_t("id")))); + int64_t refVal_id; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("id")), refVal_id ); + setId(refVal_id); } if(multipart->hasContent(utility::conversions::to_string_t("name"))) { - setName(ModelBase::stringFromHttpContent(multipart->getContent(utility::conversions::to_string_t("name")))); + utility::string_t refVal_name; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("name")), refVal_name ); + setName(refVal_name); } + return ok; } int64_t Tag::getId() const @@ -130,7 +143,6 @@ void Tag::unsetId() { m_IdIsSet = false; } - utility::string_t Tag::getName() const { return m_Name; @@ -151,7 +163,6 @@ void Tag::unsetName() { m_NameIsSet = false; } - } } } diff --git a/samples/client/petstore/cpp-restsdk/client/model/Tag.h b/samples/client/petstore/cpp-restsdk/client/model/Tag.h index a9153548fbd..f374e6efd17 100644 --- a/samples/client/petstore/cpp-restsdk/client/model/Tag.h +++ b/samples/client/petstore/cpp-restsdk/client/model/Tag.h @@ -45,10 +45,10 @@ public: void validate() override; web::json::value toJson() const override; - void fromJson(const web::json::value& json) override; + bool fromJson(const web::json::value& json) override; void toMultipart(std::shared_ptr multipart, const utility::string_t& namePrefix) const override; - void fromMultiPart(std::shared_ptr multipart, const utility::string_t& namePrefix) override; + bool fromMultiPart(std::shared_ptr multipart, const utility::string_t& namePrefix) override; ///////////////////////////////////////////// /// Tag members diff --git a/samples/client/petstore/cpp-restsdk/client/model/User.cpp b/samples/client/petstore/cpp-restsdk/client/model/User.cpp index f0fa581f868..d59b5366d3f 100644 --- a/samples/client/petstore/cpp-restsdk/client/model/User.cpp +++ b/samples/client/petstore/cpp-restsdk/client/model/User.cpp @@ -52,8 +52,9 @@ void User::validate() web::json::value User::toJson() const { - web::json::value val = web::json::value::object(); + web::json::value val = web::json::value::object(); + if(m_IdIsSet) { val[utility::conversions::to_string_t("id")] = ModelBase::toJson(m_Id); @@ -90,14 +91,18 @@ web::json::value User::toJson() const return val; } -void User::fromJson(const web::json::value& val) +bool User::fromJson(const web::json::value& val) { + bool ok = true; + if(val.has_field(utility::conversions::to_string_t("id"))) { const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("id")); if(!fieldValue.is_null()) { - setId(ModelBase::int64_tFromJson(fieldValue)); + int64_t refVal_id; + ok &= ModelBase::fromJson(fieldValue, refVal_id); + setId(refVal_id); } } if(val.has_field(utility::conversions::to_string_t("username"))) @@ -105,7 +110,9 @@ void User::fromJson(const web::json::value& val) const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("username")); if(!fieldValue.is_null()) { - setUsername(ModelBase::stringFromJson(fieldValue)); + utility::string_t refVal_username; + ok &= ModelBase::fromJson(fieldValue, refVal_username); + setUsername(refVal_username); } } if(val.has_field(utility::conversions::to_string_t("firstName"))) @@ -113,7 +120,9 @@ void User::fromJson(const web::json::value& val) const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("firstName")); if(!fieldValue.is_null()) { - setFirstName(ModelBase::stringFromJson(fieldValue)); + utility::string_t refVal_firstName; + ok &= ModelBase::fromJson(fieldValue, refVal_firstName); + setFirstName(refVal_firstName); } } if(val.has_field(utility::conversions::to_string_t("lastName"))) @@ -121,7 +130,9 @@ void User::fromJson(const web::json::value& val) const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("lastName")); if(!fieldValue.is_null()) { - setLastName(ModelBase::stringFromJson(fieldValue)); + utility::string_t refVal_lastName; + ok &= ModelBase::fromJson(fieldValue, refVal_lastName); + setLastName(refVal_lastName); } } if(val.has_field(utility::conversions::to_string_t("email"))) @@ -129,7 +140,9 @@ void User::fromJson(const web::json::value& val) const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("email")); if(!fieldValue.is_null()) { - setEmail(ModelBase::stringFromJson(fieldValue)); + utility::string_t refVal_email; + ok &= ModelBase::fromJson(fieldValue, refVal_email); + setEmail(refVal_email); } } if(val.has_field(utility::conversions::to_string_t("password"))) @@ -137,7 +150,9 @@ void User::fromJson(const web::json::value& val) const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("password")); if(!fieldValue.is_null()) { - setPassword(ModelBase::stringFromJson(fieldValue)); + utility::string_t refVal_password; + ok &= ModelBase::fromJson(fieldValue, refVal_password); + setPassword(refVal_password); } } if(val.has_field(utility::conversions::to_string_t("phone"))) @@ -145,7 +160,9 @@ void User::fromJson(const web::json::value& val) const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("phone")); if(!fieldValue.is_null()) { - setPhone(ModelBase::stringFromJson(fieldValue)); + utility::string_t refVal_phone; + ok &= ModelBase::fromJson(fieldValue, refVal_phone); + setPhone(refVal_phone); } } if(val.has_field(utility::conversions::to_string_t("userStatus"))) @@ -153,9 +170,12 @@ void User::fromJson(const web::json::value& val) const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("userStatus")); if(!fieldValue.is_null()) { - setUserStatus(ModelBase::int32_tFromJson(fieldValue)); + int32_t refVal_userStatus; + ok &= ModelBase::fromJson(fieldValue, refVal_userStatus); + setUserStatus(refVal_userStatus); } } + return ok; } void User::toMultipart(std::shared_ptr multipart, const utility::string_t& prefix) const @@ -165,7 +185,6 @@ void User::toMultipart(std::shared_ptr multipart, const utili { namePrefix += utility::conversions::to_string_t("."); } - if(m_IdIsSet) { multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("id"), m_Id)); @@ -200,8 +219,9 @@ void User::toMultipart(std::shared_ptr multipart, const utili } } -void User::fromMultiPart(std::shared_ptr multipart, const utility::string_t& prefix) +bool User::fromMultiPart(std::shared_ptr 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(".")) { @@ -210,36 +230,53 @@ void User::fromMultiPart(std::shared_ptr multipart, const uti if(multipart->hasContent(utility::conversions::to_string_t("id"))) { - setId(ModelBase::int64_tFromHttpContent(multipart->getContent(utility::conversions::to_string_t("id")))); + int64_t refVal_id; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("id")), refVal_id ); + setId(refVal_id); } if(multipart->hasContent(utility::conversions::to_string_t("username"))) { - setUsername(ModelBase::stringFromHttpContent(multipart->getContent(utility::conversions::to_string_t("username")))); + utility::string_t refVal_username; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("username")), refVal_username ); + setUsername(refVal_username); } if(multipart->hasContent(utility::conversions::to_string_t("firstName"))) { - setFirstName(ModelBase::stringFromHttpContent(multipart->getContent(utility::conversions::to_string_t("firstName")))); + utility::string_t refVal_firstName; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("firstName")), refVal_firstName ); + setFirstName(refVal_firstName); } if(multipart->hasContent(utility::conversions::to_string_t("lastName"))) { - setLastName(ModelBase::stringFromHttpContent(multipart->getContent(utility::conversions::to_string_t("lastName")))); + utility::string_t refVal_lastName; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("lastName")), refVal_lastName ); + setLastName(refVal_lastName); } if(multipart->hasContent(utility::conversions::to_string_t("email"))) { - setEmail(ModelBase::stringFromHttpContent(multipart->getContent(utility::conversions::to_string_t("email")))); + utility::string_t refVal_email; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("email")), refVal_email ); + setEmail(refVal_email); } if(multipart->hasContent(utility::conversions::to_string_t("password"))) { - setPassword(ModelBase::stringFromHttpContent(multipart->getContent(utility::conversions::to_string_t("password")))); + utility::string_t refVal_password; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("password")), refVal_password ); + setPassword(refVal_password); } if(multipart->hasContent(utility::conversions::to_string_t("phone"))) { - setPhone(ModelBase::stringFromHttpContent(multipart->getContent(utility::conversions::to_string_t("phone")))); + utility::string_t refVal_phone; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("phone")), refVal_phone ); + setPhone(refVal_phone); } if(multipart->hasContent(utility::conversions::to_string_t("userStatus"))) { - setUserStatus(ModelBase::int32_tFromHttpContent(multipart->getContent(utility::conversions::to_string_t("userStatus")))); + int32_t refVal_userStatus; + ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("userStatus")), refVal_userStatus ); + setUserStatus(refVal_userStatus); } + return ok; } int64_t User::getId() const @@ -262,7 +299,6 @@ void User::unsetId() { m_IdIsSet = false; } - utility::string_t User::getUsername() const { return m_Username; @@ -283,7 +319,6 @@ void User::unsetUsername() { m_UsernameIsSet = false; } - utility::string_t User::getFirstName() const { return m_FirstName; @@ -304,7 +339,6 @@ void User::unsetFirstName() { m_FirstNameIsSet = false; } - utility::string_t User::getLastName() const { return m_LastName; @@ -325,7 +359,6 @@ void User::unsetLastName() { m_LastNameIsSet = false; } - utility::string_t User::getEmail() const { return m_Email; @@ -346,7 +379,6 @@ void User::unsetEmail() { m_EmailIsSet = false; } - utility::string_t User::getPassword() const { return m_Password; @@ -367,7 +399,6 @@ void User::unsetPassword() { m_PasswordIsSet = false; } - utility::string_t User::getPhone() const { return m_Phone; @@ -388,7 +419,6 @@ void User::unsetPhone() { m_PhoneIsSet = false; } - int32_t User::getUserStatus() const { return m_UserStatus; @@ -409,7 +439,6 @@ void User::unsetUserStatus() { m_UserStatusIsSet = false; } - } } } diff --git a/samples/client/petstore/cpp-restsdk/client/model/User.h b/samples/client/petstore/cpp-restsdk/client/model/User.h index b66202fccfa..51bf40aa24c 100644 --- a/samples/client/petstore/cpp-restsdk/client/model/User.h +++ b/samples/client/petstore/cpp-restsdk/client/model/User.h @@ -45,10 +45,10 @@ public: void validate() override; web::json::value toJson() const override; - void fromJson(const web::json::value& json) override; + bool fromJson(const web::json::value& json) override; void toMultipart(std::shared_ptr multipart, const utility::string_t& namePrefix) const override; - void fromMultiPart(std::shared_ptr multipart, const utility::string_t& namePrefix) override; + bool fromMultiPart(std::shared_ptr multipart, const utility::string_t& namePrefix) override; ///////////////////////////////////////////// /// User members