From 7036b99e91cb3cdab3c12f59168d0467e3e050e7 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Tue, 23 Apr 2024 22:44:21 -0700 Subject: [PATCH] [cpp-restsdk] add support for AnyType (#18463) * [cpp-restsdk] add support for AnyType * [cpp-restsdk] fix the AnyType header generation path --- .gitignore | 5 ++ docs/generators/cpp-restsdk.md | 2 +- .../languages/CppRestSdkClientCodegen.java | 6 ++ .../anytype-header.mustache | 46 +++++++++++++++ .../anytype-source.mustache | 40 +++++++++++++ .../client/.openapi-generator/FILES | 3 + .../include/CppRestPetstoreClient/AnyType.h | 58 +++++++++++++++++++ .../cpp-restsdk/client/src/AnyType.cpp | 52 +++++++++++++++++ 8 files changed, 211 insertions(+), 1 deletion(-) create mode 100644 modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/anytype-header.mustache create mode 100644 modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/anytype-source.mustache create mode 100644 samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/AnyType.h create mode 100644 samples/client/petstore/cpp-restsdk/client/src/AnyType.cpp diff --git a/.gitignore b/.gitignore index bdc4f34b4d8..0bebe6bad6e 100644 --- a/.gitignore +++ b/.gitignore @@ -85,6 +85,11 @@ samples/client/petstore/cpp-restsdk/CMakeCache.txt samples/client/petstore/cpp-restsdk/CMakeFiles/ samples/client/petstore/cpp-restsdk/Makefile samples/client/petstore/cpp-restsdk/cmake_install.cmake +samples/client/petstore/cpp-restsdk/client/CMakeFiles +samples/client/petstore/cpp-restsdk/client/Makefile +samples/client/petstore/cpp-restsdk/client/cmake_install.cmake +samples/client/petstore/cpp-restsdk/client/CppRestPetstoreClientConfig.cmake +samples/client/petstore/cpp-restsdk/client/CMakeCache.txt #Java/Android **/.gradle diff --git a/docs/generators/cpp-restsdk.md b/docs/generators/cpp-restsdk.md index c60305614de..d27883938e5 100644 --- a/docs/generators/cpp-restsdk.md +++ b/docs/generators/cpp-restsdk.md @@ -184,7 +184,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl |Uuid|✗| |Array|✓|OAS2,OAS3 |Null|✗|OAS3 -|AnyType|✗|OAS2,OAS3 +|AnyType|✓|OAS2,OAS3 |Object|✓|OAS2,OAS3 |Maps|✓|ToolingExtension |CollectionFormat|✓|OAS2 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 bab05032f6b..518247ec8d9 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 @@ -111,6 +111,8 @@ public class CppRestSdkClientCodegen extends AbstractCppCodegen { .excludeParameterFeatures( ParameterFeature.Cookie ) + .includeDataTypeFeatures( + DataTypeFeature.AnyType) ); apiPackage = "org.openapitools.client.api"; @@ -174,6 +176,7 @@ public class CppRestSdkClientCodegen extends AbstractCppCodegen { importMapping.put("std::string", "#include "); importMapping.put("HttpContent", "#include \"HttpContent.h\""); importMapping.put("Object", "#include \"Object.h\""); + importMapping.put("AnyType", "#include \"AnyType.h\""); importMapping.put("utility::string_t", "#include "); importMapping.put("utility::datetime", "#include "); } @@ -214,11 +217,14 @@ public class CppRestSdkClientCodegen extends AbstractCppCodegen { importMapping.put("HttpContent", "#include \"" + packageName + "/" + "HttpContent.h\""); importMapping.put("Object", "#include \"" + packageName + "/" + "Object.h\""); + importMapping.put("AnyType", "#include \"" + packageName + "/" + "AnyType.h\""); supportingFiles.add(new SupportingFile("modelbase-header.mustache", getHeaderFolder(), "ModelBase.h")); supportingFiles.add(new SupportingFile("modelbase-source.mustache", getSourceFolder(), "ModelBase.cpp")); supportingFiles.add(new SupportingFile("object-header.mustache", getHeaderFolder(), "Object.h")); supportingFiles.add(new SupportingFile("object-source.mustache", getSourceFolder(), "Object.cpp")); + supportingFiles.add(new SupportingFile("anytype-header.mustache", getHeaderFolder(), "AnyType.h")); + supportingFiles.add(new SupportingFile("anytype-source.mustache", getSourceFolder(), "AnyType.cpp")); supportingFiles.add(new SupportingFile("apiclient-header.mustache", getHeaderFolder(), "ApiClient.h")); supportingFiles.add(new SupportingFile("apiclient-source.mustache", getSourceFolder(), "ApiClient.cpp")); supportingFiles.add(new SupportingFile("apiconfiguration-header.mustache", getHeaderFolder(), "ApiConfiguration.h")); diff --git a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/anytype-header.mustache b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/anytype-header.mustache new file mode 100644 index 00000000000..16206ff3bcc --- /dev/null +++ b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/anytype-header.mustache @@ -0,0 +1,46 @@ +{{>licenseInfo}} +/* + * AnyType.h + * + * This is the implementation of an any JSON type. + */ + +#ifndef {{modelHeaderGuardPrefix}}_AnyType_H_ +#define {{modelHeaderGuardPrefix}}_AnyType_H_ + +{{{defaultInclude}}} +#include "{{packageName}}/Object.h" + +#include +#include + +{{#modelNamespaceDeclarations}} +namespace {{this}} { +{{/modelNamespaceDeclarations}} + +class {{declspec}} AnyType : public Object { +public: + AnyType(); + virtual ~AnyType(); + + ///////////////////////////////////////////// + /// ModelBase overrides + void validate() override; + + web::json::value toJson() const override; + bool fromJson(const web::json::value &json) override; + + void toMultipart(std::shared_ptr multipart, + const utility::string_t &namePrefix) const override; + bool fromMultiPart(std::shared_ptr multipart, + const utility::string_t &namePrefix) override; + +private: + web::json::value m_value; +}; + +{{#modelNamespaceDeclarations}} +} +{{/modelNamespaceDeclarations}} + +#endif /* {{modelHeaderGuardPrefix}}_AnyType_H_ */ diff --git a/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/anytype-source.mustache b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/anytype-source.mustache new file mode 100644 index 00000000000..8f9421ab3fe --- /dev/null +++ b/modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/anytype-source.mustache @@ -0,0 +1,40 @@ +{{>licenseInfo}} +#include "{{packageName}}/AnyType.h" + +{{#modelNamespaceDeclarations}} +namespace {{this}} { +{{/modelNamespaceDeclarations}} + +AnyType::AnyType() { m_value = web::json::value::null(); } + +AnyType::~AnyType() {} + +void AnyType::validate() {} + +web::json::value AnyType::toJson() const { return m_value; } + +bool AnyType::fromJson(const web::json::value &val) { + m_value = val; + m_IsSet = true; + return isSet(); +} + +void AnyType::toMultipart(std::shared_ptr multipart, + const utility::string_t &prefix) const { + if (m_value.is_object()) { + return Object::toMultipart(multipart, prefix); + } + throw std::runtime_error("AnyType::toMultipart: unsupported type"); +} + +bool AnyType::fromMultiPart(std::shared_ptr multipart, + const utility::string_t &prefix) { + if (m_value.is_object()) { + return Object::fromMultiPart(multipart, prefix); + } + return false; +} + +{{#modelNamespaceDeclarations}} +} +{{/modelNamespaceDeclarations}} diff --git a/samples/client/petstore/cpp-restsdk/client/.openapi-generator/FILES b/samples/client/petstore/cpp-restsdk/client/.openapi-generator/FILES index a00a1414ecf..0fb62336872 100644 --- a/samples/client/petstore/cpp-restsdk/client/.openapi-generator/FILES +++ b/samples/client/petstore/cpp-restsdk/client/.openapi-generator/FILES @@ -1,8 +1,10 @@ .gitignore +.openapi-generator-ignore CMakeLists.txt Config.cmake.in README.md git_push.sh +include/CppRestPetstoreClient/AnyType.h include/CppRestPetstoreClient/ApiClient.h include/CppRestPetstoreClient/ApiConfiguration.h include/CppRestPetstoreClient/ApiException.h @@ -21,6 +23,7 @@ include/CppRestPetstoreClient/model/Order.h include/CppRestPetstoreClient/model/Pet.h include/CppRestPetstoreClient/model/Tag.h include/CppRestPetstoreClient/model/User.h +src/AnyType.cpp src/ApiClient.cpp src/ApiConfiguration.cpp src/ApiException.cpp diff --git a/samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/AnyType.h b/samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/AnyType.h new file mode 100644 index 00000000000..0d92c89bb5e --- /dev/null +++ b/samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/AnyType.h @@ -0,0 +1,58 @@ +/** + * OpenAPI Petstore + * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. + * + * The version of the OpenAPI document: 1.0.0 + * + * NOTE: This class is auto generated by OpenAPI-Generator 7.6.0-SNAPSHOT. + * https://openapi-generator.tech + * Do not edit the class manually. + */ + +/* + * AnyType.h + * + * This is the implementation of an any JSON type. + */ + +#ifndef ORG_OPENAPITOOLS_CLIENT_MODEL_AnyType_H_ +#define ORG_OPENAPITOOLS_CLIENT_MODEL_AnyType_H_ + + +#include "CppRestPetstoreClient/Object.h" + +#include +#include + +namespace org { +namespace openapitools { +namespace client { +namespace model { + +class AnyType : public Object { +public: + AnyType(); + virtual ~AnyType(); + + ///////////////////////////////////////////// + /// ModelBase overrides + void validate() override; + + web::json::value toJson() const override; + bool fromJson(const web::json::value &json) override; + + void toMultipart(std::shared_ptr multipart, + const utility::string_t &namePrefix) const override; + bool fromMultiPart(std::shared_ptr multipart, + const utility::string_t &namePrefix) override; + +private: + web::json::value m_value; +}; + +} +} +} +} + +#endif /* ORG_OPENAPITOOLS_CLIENT_MODEL_AnyType_H_ */ diff --git a/samples/client/petstore/cpp-restsdk/client/src/AnyType.cpp b/samples/client/petstore/cpp-restsdk/client/src/AnyType.cpp new file mode 100644 index 00000000000..a04a2692c6a --- /dev/null +++ b/samples/client/petstore/cpp-restsdk/client/src/AnyType.cpp @@ -0,0 +1,52 @@ +/** + * OpenAPI Petstore + * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. + * + * The version of the OpenAPI document: 1.0.0 + * + * NOTE: This class is auto generated by OpenAPI-Generator 7.6.0-SNAPSHOT. + * https://openapi-generator.tech + * Do not edit the class manually. + */ + +#include "CppRestPetstoreClient/AnyType.h" + +namespace org { +namespace openapitools { +namespace client { +namespace model { + +AnyType::AnyType() { m_value = web::json::value::null(); } + +AnyType::~AnyType() {} + +void AnyType::validate() {} + +web::json::value AnyType::toJson() const { return m_value; } + +bool AnyType::fromJson(const web::json::value &val) { + m_value = val; + m_IsSet = true; + return isSet(); +} + +void AnyType::toMultipart(std::shared_ptr multipart, + const utility::string_t &prefix) const { + if (m_value.is_object()) { + return Object::toMultipart(multipart, prefix); + } + throw std::runtime_error("AnyType::toMultipart: unsupported type"); +} + +bool AnyType::fromMultiPart(std::shared_ptr multipart, + const utility::string_t &prefix) { + if (m_value.is_object()) { + return Object::fromMultiPart(multipart, prefix); + } + return false; +} + +} +} +} +}