diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppUE4ClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppUE4ClientCodegen.java index 064b5282f62..dd71b9008f7 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppUE4ClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppUE4ClientCodegen.java @@ -497,13 +497,20 @@ public class CppUE4ClientCodegen extends AbstractCppCodegen { name = name.toLowerCase(Locale.ROOT); } + //Unreal variable names are CamelCase + String camelCaseName = camelize(name, false); + + //Avoid empty variable name at all costs + if(!camelCaseName.isEmpty()) { + name = camelCaseName; + } + // for reserved word or word starting with number, append _ if (isReservedWord(name) || name.matches("^\\d.*")) { name = escapeReservedWord(name); } - - //Unreal variable names are CamelCase - return camelize(name, false); + + return name; } @Override diff --git a/modules/openapi-generator/src/main/resources/cpp-ue4/Build.cs.mustache b/modules/openapi-generator/src/main/resources/cpp-ue4/Build.cs.mustache index a6fe9bd84ec..9b33c5d3d57 100644 --- a/modules/openapi-generator/src/main/resources/cpp-ue4/Build.cs.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-ue4/Build.cs.mustache @@ -15,5 +15,6 @@ public class {{unrealModuleName}} : ModuleRules "Json", } ); + PCHUsage = PCHUsageMode.NoPCHs; } } \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/cpp-ue4/api-operations-header.mustache b/modules/openapi-generator/src/main/resources/cpp-ue4/api-operations-header.mustache index 1486ef60e2c..bd168101e38 100644 --- a/modules/openapi-generator/src/main/resources/cpp-ue4/api-operations-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-ue4/api-operations-header.mustache @@ -52,7 +52,7 @@ public: {{#responses.0}} void SetHttpResponseCode(EHttpResponseCodes::Type InHttpResponseCode) final; {{/responses.0}} - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; {{#returnType}}{{{returnType}}} Content;{{/returnType}} }; diff --git a/modules/openapi-generator/src/main/resources/cpp-ue4/helpers-header.mustache b/modules/openapi-generator/src/main/resources/cpp-ue4/helpers-header.mustache index adbff0c0e88..1750a6f8e95 100644 --- a/modules/openapi-generator/src/main/resources/cpp-ue4/helpers-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-ue4/helpers-header.mustache @@ -6,6 +6,7 @@ #include "Serialization/JsonSerializer.h" #include "Dom/JsonObject.h" #include "Misc/Base64.h" +#include "PlatformHttp.h" class IHttpRequest; @@ -196,10 +197,22 @@ inline FString CollectionToUrlString_multi(const TArray& Collection, const TC ////////////////////////////////////////////////////////////////////////// -template::value, int>::type = 0> -inline void WriteJsonValue(JsonWriter& Writer, const T& Value) +inline void WriteJsonValue(JsonWriter& Writer, const TSharedPtr& Value) { - Writer->WriteValue(Value); + if (Value.IsValid()) + { + FJsonSerializer::Serialize(Value.ToSharedRef(), Writer, false); + } + else + { + Writer->WriteObjectStart(); + Writer->WriteObjectEnd(); + } +} + +inline void WriteJsonValue(JsonWriter& Writer, const TArray& Value) +{ + Writer->WriteValue(ToString(Value)); } inline void WriteJsonValue(JsonWriter& Writer, const FDateTime& Value) @@ -212,6 +225,12 @@ inline void WriteJsonValue(JsonWriter& Writer, const Model& Value) Value.WriteJson(Writer); } +template::value, int>::type = 0> +inline void WriteJsonValue(JsonWriter& Writer, const T& Value) +{ + Writer->WriteValue(Value); +} + template inline void WriteJsonValue(JsonWriter& Writer, const TArray& Value) { @@ -235,54 +254,8 @@ inline void WriteJsonValue(JsonWriter& Writer, const TMap& Value) Writer->WriteObjectEnd(); } -inline void WriteJsonValue(JsonWriter& Writer, const TSharedPtr& Value) -{ - if (Value.IsValid()) - { - FJsonSerializer::Serialize(Value.ToSharedRef(), Writer, false); - } - else - { - Writer->WriteObjectStart(); - Writer->WriteObjectEnd(); - } -} - -inline void WriteJsonValue(JsonWriter& Writer, const TArray& Value) -{ - Writer->WriteValue(ToString(Value)); -} - ////////////////////////////////////////////////////////////////////////// -template -inline bool TryGetJsonValue(const TSharedPtr& JsonObject, const FString& Key, T& Value) -{ - const TSharedPtr JsonValue = JsonObject->TryGetField(Key); - if (JsonValue.IsValid() && !JsonValue->IsNull()) - { - return TryGetJsonValue(JsonValue, Value); - } - return false; -} - -template -inline bool TryGetJsonValue(const TSharedPtr& JsonObject, const FString& Key, TOptional& OptionalValue) -{ - if(JsonObject->HasField(Key)) - { - T Value; - if (TryGetJsonValue(JsonObject, Key, Value)) - { - OptionalValue = Value; - return true; - } - else - return false; - } - return true; // Absence of optional value is not a parsing error -} - inline bool TryGetJsonValue(const TSharedPtr& JsonValue, FString& Value) { FString TmpValue; @@ -316,6 +289,34 @@ inline bool TryGetJsonValue(const TSharedPtr& JsonValue, bool& Value return false; } +inline bool TryGetJsonValue(const TSharedPtr& JsonValue, TSharedPtr& JsonObjectValue) +{ + const TSharedPtr* Object; + if (JsonValue->TryGetObject(Object)) + { + JsonObjectValue = *Object; + return true; + } + return false; +} + +inline bool TryGetJsonValue(const TSharedPtr& JsonValue, TArray& Value) +{ + FString TmpValue; + if (JsonValue->TryGetString(TmpValue)) + { + Base64UrlDecode(TmpValue, Value); + return true; + } + else + return false; +} + +inline bool TryGetJsonValue(const TSharedPtr& JsonValue, Model& Value) +{ + return Value.FromJson(JsonValue); +} + template::value, int>::type = 0> inline bool TryGetJsonValue(const TSharedPtr& JsonValue, T& Value) { @@ -329,15 +330,6 @@ inline bool TryGetJsonValue(const TSharedPtr& JsonValue, T& Value) return false; } -inline bool TryGetJsonValue(const TSharedPtr& JsonValue, Model& Value) -{ - const TSharedPtr* Object; - if (JsonValue->TryGetObject(Object)) - return Value.FromJson(*Object); - else - return false; -} - template inline bool TryGetJsonValue(const TSharedPtr& JsonValue, TArray& ArrayValue) { @@ -377,27 +369,32 @@ inline bool TryGetJsonValue(const TSharedPtr& JsonValue, TMap& JsonValue, TSharedPtr& JsonObjectValue) +template +inline bool TryGetJsonValue(const TSharedPtr& JsonObject, const FString& Key, T& Value) { - const TSharedPtr* Object; - if (JsonValue->TryGetObject(Object)) + const TSharedPtr JsonValue = JsonObject->TryGetField(Key); + if (JsonValue.IsValid() && !JsonValue->IsNull()) { - JsonObjectValue = *Object; - return true; + return TryGetJsonValue(JsonValue, Value); } return false; } -inline bool TryGetJsonValue(const TSharedPtr& JsonValue, TArray& Value) +template +inline bool TryGetJsonValue(const TSharedPtr& JsonObject, const FString& Key, TOptional& OptionalValue) { - FString TmpValue; - if (JsonValue->TryGetString(TmpValue)) + if(JsonObject->HasField(Key)) { - Base64UrlDecode(TmpValue, Value); - return true; + T Value; + if (TryGetJsonValue(JsonObject, Key, Value)) + { + OptionalValue = Value; + return true; + } + else + return false; } - else - return false; + return true; // Absence of optional value is not a parsing error } {{#cppNamespaceDeclarations}} diff --git a/modules/openapi-generator/src/main/resources/cpp-ue4/helpers-source.mustache b/modules/openapi-generator/src/main/resources/cpp-ue4/helpers-source.mustache index 1ae8bad54c6..ea97911bbd9 100644 --- a/modules/openapi-generator/src/main/resources/cpp-ue4/helpers-source.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-ue4/helpers-source.mustache @@ -6,6 +6,7 @@ #include "Interfaces/IHttpRequest.h" #include "PlatformHttp.h" #include "Misc/FileHelper.h" +#include "Misc/Paths.h" {{#cppNamespaceDeclarations}} namespace {{this}} diff --git a/modules/openapi-generator/src/main/resources/cpp-ue4/model-base-header.mustache b/modules/openapi-generator/src/main/resources/cpp-ue4/model-base-header.mustache index 67280bde989..94edaaf4327 100644 --- a/modules/openapi-generator/src/main/resources/cpp-ue4/model-base-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-ue4/model-base-header.mustache @@ -18,7 +18,7 @@ class {{dllapi}} Model public: virtual ~Model() {} virtual void WriteJson(JsonWriter& Writer) const = 0; - virtual bool FromJson(const TSharedPtr& JsonObject) = 0; + virtual bool FromJson(const TSharedPtr& JsonValue) = 0; }; class {{dllapi}} Request @@ -33,7 +33,7 @@ class {{dllapi}} Response { public: virtual ~Response() {} - virtual bool FromJson(const TSharedPtr& JsonObject) = 0; + virtual bool FromJson(const TSharedPtr& JsonValue) = 0; void SetSuccessful(bool InSuccessful) { Successful = InSuccessful; } bool IsSuccessful() const { return Successful; } diff --git a/modules/openapi-generator/src/main/resources/cpp-ue4/model-header.mustache b/modules/openapi-generator/src/main/resources/cpp-ue4/model-header.mustache index 6af8c720f0e..900dcf7f318 100644 --- a/modules/openapi-generator/src/main/resources/cpp-ue4/model-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-ue4/model-header.mustache @@ -21,9 +21,26 @@ class {{dllapi}} {{classname}} : public Model { public: virtual ~{{classname}}() {} - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; void WriteJson(JsonWriter& Writer) const final; + {{#isString}} + {{#isEnum}} + {{#allowableValues}} + enum class Values + { + {{#enumVars}} + {{name}}, + {{/enumVars}} + }; + + Values Value{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}; + {{/allowableValues}} + {{/isEnum}} + {{^isEnum}} + FString Value{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}; + {{/isEnum}} + {{/isString}} {{#vars}} {{#isEnum}} {{#allowableValues}} diff --git a/modules/openapi-generator/src/main/resources/cpp-ue4/model-source.mustache b/modules/openapi-generator/src/main/resources/cpp-ue4/model-source.mustache index 3e9b14788a6..f26df2ec50f 100644 --- a/modules/openapi-generator/src/main/resources/cpp-ue4/model-source.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-ue4/model-source.mustache @@ -11,6 +11,54 @@ namespace {{this}} { {{/cppNamespaceDeclarations}} {{#models}}{{#model}} +{{#isEnum}} +inline FString ToString(const {{classname}}::Values& Value) +{ + {{#allowableValues}} + switch (Value) + { + {{#enumVars}} + case {{classname}}::Values::{{name}}: + return TEXT({{{value}}}); + {{/enumVars}} + } + {{/allowableValues}} + + UE_LOG(Log{{unrealModuleName}}, Error, TEXT("Invalid {{classname}}::Values Value (%d)"), (int)Value); + return TEXT(""); +} + +inline FStringFormatArg ToStringFormatArg(const {{classname}}::Values& Value) +{ + return FStringFormatArg(ToString(Value)); +} + +inline void WriteJsonValue(JsonWriter& Writer, const {{classname}}::Values& Value) +{ + WriteJsonValue(Writer, ToString(Value)); +} + +inline bool TryGetJsonValue(const TSharedPtr& JsonValue, {{classname}}::Values& Value) +{ + {{#allowableValues}} + FString TmpValue; + if (JsonValue->TryGetString(TmpValue)) + { + static TMap StringToEnum = { {{#enumVars}} + { TEXT({{{value}}}), {{classname}}::Values::{{name}} },{{/enumVars}} }; + + const auto Found = StringToEnum.Find(TmpValue); + if(Found) + { + Value = *Found; + return true; + } + } + {{/allowableValues}} + return false; +} + +{{/isEnum}} {{#hasEnums}} {{#vars}} {{#isEnum}} @@ -65,9 +113,10 @@ inline bool TryGetJsonValue(const TSharedPtr& JsonValue, {{classname {{/hasEnums}} void {{classname}}::WriteJson(JsonWriter& Writer) const { - {{#parent}} - #error inheritance not handled right now - {{/parent}} + {{#isString}} + WriteJsonValue(Writer, Value); + {{/isString}} + {{^isString}} Writer->WriteObjectStart(); {{#vars}} {{#required}} @@ -81,18 +130,29 @@ void {{classname}}::WriteJson(JsonWriter& Writer) const {{/required}} {{/vars}} Writer->WriteObjectEnd(); + {{/isString}} } -bool {{classname}}::FromJson(const TSharedPtr& JsonObject) +bool {{classname}}::FromJson(const TSharedPtr& JsonValue) { + {{#isString}} + return TryGetJsonValue(JsonValue, Value); + {{/isString}} + {{^isString}} + const TSharedPtr* Object; + if (!JsonValue->TryGetObject(Object)) + return false; + bool ParseSuccess = true; {{#vars}} - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("{{baseName}}"), {{name}}); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("{{baseName}}"), {{name}}); {{/vars}} return ParseSuccess; + {{/isString}} } + {{/model}} {{/models}} {{#cppNamespaceDeclarations}} diff --git a/samples/client/petstore/cpp-ue4/OpenAPI.Build.cs b/samples/client/petstore/cpp-ue4/OpenAPI.Build.cs index 48294bae4e4..f6d2f83eba8 100644 --- a/samples/client/petstore/cpp-ue4/OpenAPI.Build.cs +++ b/samples/client/petstore/cpp-ue4/OpenAPI.Build.cs @@ -26,5 +26,6 @@ public class OpenAPI : ModuleRules "Json", } ); + PCHUsage = PCHUsageMode.NoPCHs; } } \ No newline at end of file diff --git a/samples/client/petstore/cpp-ue4/Private/OpenAPIApiResponse.cpp b/samples/client/petstore/cpp-ue4/Private/OpenAPIApiResponse.cpp index 54254095989..bd248608e46 100644 --- a/samples/client/petstore/cpp-ue4/Private/OpenAPIApiResponse.cpp +++ b/samples/client/petstore/cpp-ue4/Private/OpenAPIApiResponse.cpp @@ -38,14 +38,19 @@ void OpenAPIApiResponse::WriteJson(JsonWriter& Writer) const Writer->WriteObjectEnd(); } -bool OpenAPIApiResponse::FromJson(const TSharedPtr& JsonObject) +bool OpenAPIApiResponse::FromJson(const TSharedPtr& JsonValue) { + const TSharedPtr* Object; + if (!JsonValue->TryGetObject(Object)) + return false; + bool ParseSuccess = true; - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("code"), Code); - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("type"), Type); - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("message"), Message); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("code"), Code); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("type"), Type); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("message"), Message); return ParseSuccess; } + } diff --git a/samples/client/petstore/cpp-ue4/Private/OpenAPICategory.cpp b/samples/client/petstore/cpp-ue4/Private/OpenAPICategory.cpp index 2f3b816d3c9..109decaa0df 100644 --- a/samples/client/petstore/cpp-ue4/Private/OpenAPICategory.cpp +++ b/samples/client/petstore/cpp-ue4/Private/OpenAPICategory.cpp @@ -34,13 +34,18 @@ void OpenAPICategory::WriteJson(JsonWriter& Writer) const Writer->WriteObjectEnd(); } -bool OpenAPICategory::FromJson(const TSharedPtr& JsonObject) +bool OpenAPICategory::FromJson(const TSharedPtr& JsonValue) { + const TSharedPtr* Object; + if (!JsonValue->TryGetObject(Object)) + return false; + bool ParseSuccess = true; - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("id"), Id); - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("name"), Name); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("id"), Id); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("name"), Name); return ParseSuccess; } + } diff --git a/samples/client/petstore/cpp-ue4/Private/OpenAPIHelpers.cpp b/samples/client/petstore/cpp-ue4/Private/OpenAPIHelpers.cpp index 0a3be0adee2..7361ddf5fe3 100644 --- a/samples/client/petstore/cpp-ue4/Private/OpenAPIHelpers.cpp +++ b/samples/client/petstore/cpp-ue4/Private/OpenAPIHelpers.cpp @@ -17,6 +17,7 @@ #include "Interfaces/IHttpRequest.h" #include "PlatformHttp.h" #include "Misc/FileHelper.h" +#include "Misc/Paths.h" namespace OpenAPI { diff --git a/samples/client/petstore/cpp-ue4/Private/OpenAPIOrder.cpp b/samples/client/petstore/cpp-ue4/Private/OpenAPIOrder.cpp index f125c7ae40b..4442b2a1c10 100644 --- a/samples/client/petstore/cpp-ue4/Private/OpenAPIOrder.cpp +++ b/samples/client/petstore/cpp-ue4/Private/OpenAPIOrder.cpp @@ -96,17 +96,22 @@ void OpenAPIOrder::WriteJson(JsonWriter& Writer) const Writer->WriteObjectEnd(); } -bool OpenAPIOrder::FromJson(const TSharedPtr& JsonObject) +bool OpenAPIOrder::FromJson(const TSharedPtr& JsonValue) { + const TSharedPtr* Object; + if (!JsonValue->TryGetObject(Object)) + return false; + bool ParseSuccess = true; - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("id"), Id); - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("petId"), PetId); - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("quantity"), Quantity); - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("shipDate"), ShipDate); - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("status"), Status); - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("complete"), Complete); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("id"), Id); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("petId"), PetId); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("quantity"), Quantity); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("shipDate"), ShipDate); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("status"), Status); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("complete"), Complete); return ParseSuccess; } + } diff --git a/samples/client/petstore/cpp-ue4/Private/OpenAPIPet.cpp b/samples/client/petstore/cpp-ue4/Private/OpenAPIPet.cpp index b36df84ae6b..9cc4f201362 100644 --- a/samples/client/petstore/cpp-ue4/Private/OpenAPIPet.cpp +++ b/samples/client/petstore/cpp-ue4/Private/OpenAPIPet.cpp @@ -90,17 +90,22 @@ void OpenAPIPet::WriteJson(JsonWriter& Writer) const Writer->WriteObjectEnd(); } -bool OpenAPIPet::FromJson(const TSharedPtr& JsonObject) +bool OpenAPIPet::FromJson(const TSharedPtr& JsonValue) { + const TSharedPtr* Object; + if (!JsonValue->TryGetObject(Object)) + return false; + bool ParseSuccess = true; - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("id"), Id); - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("category"), Category); - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("name"), Name); - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("photoUrls"), PhotoUrls); - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("tags"), Tags); - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("status"), Status); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("id"), Id); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("category"), Category); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("name"), Name); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("photoUrls"), PhotoUrls); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("tags"), Tags); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("status"), Status); return ParseSuccess; } + } diff --git a/samples/client/petstore/cpp-ue4/Private/OpenAPITag.cpp b/samples/client/petstore/cpp-ue4/Private/OpenAPITag.cpp index 4f399573c7e..4714f75bf53 100644 --- a/samples/client/petstore/cpp-ue4/Private/OpenAPITag.cpp +++ b/samples/client/petstore/cpp-ue4/Private/OpenAPITag.cpp @@ -34,13 +34,18 @@ void OpenAPITag::WriteJson(JsonWriter& Writer) const Writer->WriteObjectEnd(); } -bool OpenAPITag::FromJson(const TSharedPtr& JsonObject) +bool OpenAPITag::FromJson(const TSharedPtr& JsonValue) { + const TSharedPtr* Object; + if (!JsonValue->TryGetObject(Object)) + return false; + bool ParseSuccess = true; - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("id"), Id); - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("name"), Name); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("id"), Id); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("name"), Name); return ParseSuccess; } + } diff --git a/samples/client/petstore/cpp-ue4/Private/OpenAPIUser.cpp b/samples/client/petstore/cpp-ue4/Private/OpenAPIUser.cpp index bb0fe0292bf..90eb9fc9b43 100644 --- a/samples/client/petstore/cpp-ue4/Private/OpenAPIUser.cpp +++ b/samples/client/petstore/cpp-ue4/Private/OpenAPIUser.cpp @@ -58,19 +58,24 @@ void OpenAPIUser::WriteJson(JsonWriter& Writer) const Writer->WriteObjectEnd(); } -bool OpenAPIUser::FromJson(const TSharedPtr& JsonObject) +bool OpenAPIUser::FromJson(const TSharedPtr& JsonValue) { + const TSharedPtr* Object; + if (!JsonValue->TryGetObject(Object)) + return false; + bool ParseSuccess = true; - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("id"), Id); - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("username"), Username); - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("firstName"), FirstName); - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("lastName"), LastName); - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("email"), Email); - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("password"), Password); - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("phone"), Phone); - ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("userStatus"), UserStatus); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("id"), Id); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("username"), Username); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("firstName"), FirstName); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("lastName"), LastName); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("email"), Email); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("password"), Password); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("phone"), Phone); + ParseSuccess &= TryGetJsonValue(*Object, TEXT("userStatus"), UserStatus); return ParseSuccess; } + } diff --git a/samples/client/petstore/cpp-ue4/Public/OpenAPIApiResponse.h b/samples/client/petstore/cpp-ue4/Public/OpenAPIApiResponse.h index 9eca3a5e273..2924d49d26e 100644 --- a/samples/client/petstore/cpp-ue4/Public/OpenAPIApiResponse.h +++ b/samples/client/petstore/cpp-ue4/Public/OpenAPIApiResponse.h @@ -26,7 +26,7 @@ class OPENAPI_API OpenAPIApiResponse : public Model { public: virtual ~OpenAPIApiResponse() {} - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; void WriteJson(JsonWriter& Writer) const final; TOptional Code; diff --git a/samples/client/petstore/cpp-ue4/Public/OpenAPIBaseModel.h b/samples/client/petstore/cpp-ue4/Public/OpenAPIBaseModel.h index 26efc0d9c6a..defa1b49263 100644 --- a/samples/client/petstore/cpp-ue4/Public/OpenAPIBaseModel.h +++ b/samples/client/petstore/cpp-ue4/Public/OpenAPIBaseModel.h @@ -27,7 +27,7 @@ class OPENAPI_API Model public: virtual ~Model() {} virtual void WriteJson(JsonWriter& Writer) const = 0; - virtual bool FromJson(const TSharedPtr& JsonObject) = 0; + virtual bool FromJson(const TSharedPtr& JsonValue) = 0; }; class OPENAPI_API Request @@ -42,7 +42,7 @@ class OPENAPI_API Response { public: virtual ~Response() {} - virtual bool FromJson(const TSharedPtr& JsonObject) = 0; + virtual bool FromJson(const TSharedPtr& JsonValue) = 0; void SetSuccessful(bool InSuccessful) { Successful = InSuccessful; } bool IsSuccessful() const { return Successful; } diff --git a/samples/client/petstore/cpp-ue4/Public/OpenAPICategory.h b/samples/client/petstore/cpp-ue4/Public/OpenAPICategory.h index b375f23e71a..a38dd8a632d 100644 --- a/samples/client/petstore/cpp-ue4/Public/OpenAPICategory.h +++ b/samples/client/petstore/cpp-ue4/Public/OpenAPICategory.h @@ -26,7 +26,7 @@ class OPENAPI_API OpenAPICategory : public Model { public: virtual ~OpenAPICategory() {} - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; void WriteJson(JsonWriter& Writer) const final; TOptional Id; diff --git a/samples/client/petstore/cpp-ue4/Public/OpenAPIHelpers.h b/samples/client/petstore/cpp-ue4/Public/OpenAPIHelpers.h index 42c7c6bd122..74e9e079610 100644 --- a/samples/client/petstore/cpp-ue4/Public/OpenAPIHelpers.h +++ b/samples/client/petstore/cpp-ue4/Public/OpenAPIHelpers.h @@ -17,6 +17,7 @@ #include "Serialization/JsonSerializer.h" #include "Dom/JsonObject.h" #include "Misc/Base64.h" +#include "PlatformHttp.h" class IHttpRequest; @@ -205,10 +206,22 @@ inline FString CollectionToUrlString_multi(const TArray& Collection, const TC ////////////////////////////////////////////////////////////////////////// -template::value, int>::type = 0> -inline void WriteJsonValue(JsonWriter& Writer, const T& Value) +inline void WriteJsonValue(JsonWriter& Writer, const TSharedPtr& Value) { - Writer->WriteValue(Value); + if (Value.IsValid()) + { + FJsonSerializer::Serialize(Value.ToSharedRef(), Writer, false); + } + else + { + Writer->WriteObjectStart(); + Writer->WriteObjectEnd(); + } +} + +inline void WriteJsonValue(JsonWriter& Writer, const TArray& Value) +{ + Writer->WriteValue(ToString(Value)); } inline void WriteJsonValue(JsonWriter& Writer, const FDateTime& Value) @@ -221,6 +234,12 @@ inline void WriteJsonValue(JsonWriter& Writer, const Model& Value) Value.WriteJson(Writer); } +template::value, int>::type = 0> +inline void WriteJsonValue(JsonWriter& Writer, const T& Value) +{ + Writer->WriteValue(Value); +} + template inline void WriteJsonValue(JsonWriter& Writer, const TArray& Value) { @@ -244,54 +263,8 @@ inline void WriteJsonValue(JsonWriter& Writer, const TMap& Value) Writer->WriteObjectEnd(); } -inline void WriteJsonValue(JsonWriter& Writer, const TSharedPtr& Value) -{ - if (Value.IsValid()) - { - FJsonSerializer::Serialize(Value.ToSharedRef(), Writer, false); - } - else - { - Writer->WriteObjectStart(); - Writer->WriteObjectEnd(); - } -} - -inline void WriteJsonValue(JsonWriter& Writer, const TArray& Value) -{ - Writer->WriteValue(ToString(Value)); -} - ////////////////////////////////////////////////////////////////////////// -template -inline bool TryGetJsonValue(const TSharedPtr& JsonObject, const FString& Key, T& Value) -{ - const TSharedPtr JsonValue = JsonObject->TryGetField(Key); - if (JsonValue.IsValid() && !JsonValue->IsNull()) - { - return TryGetJsonValue(JsonValue, Value); - } - return false; -} - -template -inline bool TryGetJsonValue(const TSharedPtr& JsonObject, const FString& Key, TOptional& OptionalValue) -{ - if(JsonObject->HasField(Key)) - { - T Value; - if (TryGetJsonValue(JsonObject, Key, Value)) - { - OptionalValue = Value; - return true; - } - else - return false; - } - return true; // Absence of optional value is not a parsing error -} - inline bool TryGetJsonValue(const TSharedPtr& JsonValue, FString& Value) { FString TmpValue; @@ -325,6 +298,34 @@ inline bool TryGetJsonValue(const TSharedPtr& JsonValue, bool& Value return false; } +inline bool TryGetJsonValue(const TSharedPtr& JsonValue, TSharedPtr& JsonObjectValue) +{ + const TSharedPtr* Object; + if (JsonValue->TryGetObject(Object)) + { + JsonObjectValue = *Object; + return true; + } + return false; +} + +inline bool TryGetJsonValue(const TSharedPtr& JsonValue, TArray& Value) +{ + FString TmpValue; + if (JsonValue->TryGetString(TmpValue)) + { + Base64UrlDecode(TmpValue, Value); + return true; + } + else + return false; +} + +inline bool TryGetJsonValue(const TSharedPtr& JsonValue, Model& Value) +{ + return Value.FromJson(JsonValue); +} + template::value, int>::type = 0> inline bool TryGetJsonValue(const TSharedPtr& JsonValue, T& Value) { @@ -338,15 +339,6 @@ inline bool TryGetJsonValue(const TSharedPtr& JsonValue, T& Value) return false; } -inline bool TryGetJsonValue(const TSharedPtr& JsonValue, Model& Value) -{ - const TSharedPtr* Object; - if (JsonValue->TryGetObject(Object)) - return Value.FromJson(*Object); - else - return false; -} - template inline bool TryGetJsonValue(const TSharedPtr& JsonValue, TArray& ArrayValue) { @@ -386,27 +378,32 @@ inline bool TryGetJsonValue(const TSharedPtr& JsonValue, TMap& JsonValue, TSharedPtr& JsonObjectValue) +template +inline bool TryGetJsonValue(const TSharedPtr& JsonObject, const FString& Key, T& Value) { - const TSharedPtr* Object; - if (JsonValue->TryGetObject(Object)) + const TSharedPtr JsonValue = JsonObject->TryGetField(Key); + if (JsonValue.IsValid() && !JsonValue->IsNull()) { - JsonObjectValue = *Object; - return true; + return TryGetJsonValue(JsonValue, Value); } return false; } -inline bool TryGetJsonValue(const TSharedPtr& JsonValue, TArray& Value) +template +inline bool TryGetJsonValue(const TSharedPtr& JsonObject, const FString& Key, TOptional& OptionalValue) { - FString TmpValue; - if (JsonValue->TryGetString(TmpValue)) + if(JsonObject->HasField(Key)) { - Base64UrlDecode(TmpValue, Value); - return true; + T Value; + if (TryGetJsonValue(JsonObject, Key, Value)) + { + OptionalValue = Value; + return true; + } + else + return false; } - else - return false; + return true; // Absence of optional value is not a parsing error } } diff --git a/samples/client/petstore/cpp-ue4/Public/OpenAPIOrder.h b/samples/client/petstore/cpp-ue4/Public/OpenAPIOrder.h index e3207190e29..f8bcaadde53 100644 --- a/samples/client/petstore/cpp-ue4/Public/OpenAPIOrder.h +++ b/samples/client/petstore/cpp-ue4/Public/OpenAPIOrder.h @@ -26,7 +26,7 @@ class OPENAPI_API OpenAPIOrder : public Model { public: virtual ~OpenAPIOrder() {} - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; void WriteJson(JsonWriter& Writer) const final; TOptional Id; diff --git a/samples/client/petstore/cpp-ue4/Public/OpenAPIPet.h b/samples/client/petstore/cpp-ue4/Public/OpenAPIPet.h index 0e81e5fe0f9..b916d319819 100644 --- a/samples/client/petstore/cpp-ue4/Public/OpenAPIPet.h +++ b/samples/client/petstore/cpp-ue4/Public/OpenAPIPet.h @@ -28,7 +28,7 @@ class OPENAPI_API OpenAPIPet : public Model { public: virtual ~OpenAPIPet() {} - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; void WriteJson(JsonWriter& Writer) const final; TOptional Id; diff --git a/samples/client/petstore/cpp-ue4/Public/OpenAPIPetApiOperations.h b/samples/client/petstore/cpp-ue4/Public/OpenAPIPetApiOperations.h index a93365d9546..5b5c2511869 100644 --- a/samples/client/petstore/cpp-ue4/Public/OpenAPIPetApiOperations.h +++ b/samples/client/petstore/cpp-ue4/Public/OpenAPIPetApiOperations.h @@ -41,7 +41,7 @@ class OPENAPI_API OpenAPIPetApi::AddPetResponse : public Response public: virtual ~AddPetResponse() {} void SetHttpResponseCode(EHttpResponseCodes::Type InHttpResponseCode) final; - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; }; @@ -66,7 +66,7 @@ class OPENAPI_API OpenAPIPetApi::DeletePetResponse : public Response public: virtual ~DeletePetResponse() {} void SetHttpResponseCode(EHttpResponseCodes::Type InHttpResponseCode) final; - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; }; @@ -97,7 +97,7 @@ class OPENAPI_API OpenAPIPetApi::FindPetsByStatusResponse : public Response public: virtual ~FindPetsByStatusResponse() {} void SetHttpResponseCode(EHttpResponseCodes::Type InHttpResponseCode) final; - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; TArray Content; }; @@ -122,7 +122,7 @@ class OPENAPI_API OpenAPIPetApi::FindPetsByTagsResponse : public Response public: virtual ~FindPetsByTagsResponse() {} void SetHttpResponseCode(EHttpResponseCodes::Type InHttpResponseCode) final; - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; TArray Content; }; @@ -147,7 +147,7 @@ class OPENAPI_API OpenAPIPetApi::GetPetByIdResponse : public Response public: virtual ~GetPetByIdResponse() {} void SetHttpResponseCode(EHttpResponseCodes::Type InHttpResponseCode) final; - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; OpenAPIPet Content; }; @@ -171,7 +171,7 @@ class OPENAPI_API OpenAPIPetApi::UpdatePetResponse : public Response public: virtual ~UpdatePetResponse() {} void SetHttpResponseCode(EHttpResponseCodes::Type InHttpResponseCode) final; - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; }; @@ -199,7 +199,7 @@ class OPENAPI_API OpenAPIPetApi::UpdatePetWithFormResponse : public Response public: virtual ~UpdatePetWithFormResponse() {} void SetHttpResponseCode(EHttpResponseCodes::Type InHttpResponseCode) final; - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; }; @@ -227,7 +227,7 @@ class OPENAPI_API OpenAPIPetApi::UploadFileResponse : public Response public: virtual ~UploadFileResponse() {} void SetHttpResponseCode(EHttpResponseCodes::Type InHttpResponseCode) final; - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; OpenAPIApiResponse Content; }; diff --git a/samples/client/petstore/cpp-ue4/Public/OpenAPIStoreApiOperations.h b/samples/client/petstore/cpp-ue4/Public/OpenAPIStoreApiOperations.h index 06e6809c185..84a4fd6cf0e 100644 --- a/samples/client/petstore/cpp-ue4/Public/OpenAPIStoreApiOperations.h +++ b/samples/client/petstore/cpp-ue4/Public/OpenAPIStoreApiOperations.h @@ -40,7 +40,7 @@ class OPENAPI_API OpenAPIStoreApi::DeleteOrderResponse : public Response public: virtual ~DeleteOrderResponse() {} void SetHttpResponseCode(EHttpResponseCodes::Type InHttpResponseCode) final; - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; }; @@ -63,7 +63,7 @@ class OPENAPI_API OpenAPIStoreApi::GetInventoryResponse : public Response public: virtual ~GetInventoryResponse() {} void SetHttpResponseCode(EHttpResponseCodes::Type InHttpResponseCode) final; - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; TMap Content; }; @@ -88,7 +88,7 @@ class OPENAPI_API OpenAPIStoreApi::GetOrderByIdResponse : public Response public: virtual ~GetOrderByIdResponse() {} void SetHttpResponseCode(EHttpResponseCodes::Type InHttpResponseCode) final; - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; OpenAPIOrder Content; }; @@ -112,7 +112,7 @@ class OPENAPI_API OpenAPIStoreApi::PlaceOrderResponse : public Response public: virtual ~PlaceOrderResponse() {} void SetHttpResponseCode(EHttpResponseCodes::Type InHttpResponseCode) final; - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; OpenAPIOrder Content; }; diff --git a/samples/client/petstore/cpp-ue4/Public/OpenAPITag.h b/samples/client/petstore/cpp-ue4/Public/OpenAPITag.h index 72bc6d130da..ab8a2646beb 100644 --- a/samples/client/petstore/cpp-ue4/Public/OpenAPITag.h +++ b/samples/client/petstore/cpp-ue4/Public/OpenAPITag.h @@ -26,7 +26,7 @@ class OPENAPI_API OpenAPITag : public Model { public: virtual ~OpenAPITag() {} - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; void WriteJson(JsonWriter& Writer) const final; TOptional Id; diff --git a/samples/client/petstore/cpp-ue4/Public/OpenAPIUser.h b/samples/client/petstore/cpp-ue4/Public/OpenAPIUser.h index 55494d8ec0b..fe4e1affa9f 100644 --- a/samples/client/petstore/cpp-ue4/Public/OpenAPIUser.h +++ b/samples/client/petstore/cpp-ue4/Public/OpenAPIUser.h @@ -26,7 +26,7 @@ class OPENAPI_API OpenAPIUser : public Model { public: virtual ~OpenAPIUser() {} - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; void WriteJson(JsonWriter& Writer) const final; TOptional Id; diff --git a/samples/client/petstore/cpp-ue4/Public/OpenAPIUserApiOperations.h b/samples/client/petstore/cpp-ue4/Public/OpenAPIUserApiOperations.h index 3e050a41ef0..8c287474ed3 100644 --- a/samples/client/petstore/cpp-ue4/Public/OpenAPIUserApiOperations.h +++ b/samples/client/petstore/cpp-ue4/Public/OpenAPIUserApiOperations.h @@ -40,7 +40,7 @@ class OPENAPI_API OpenAPIUserApi::CreateUserResponse : public Response public: virtual ~CreateUserResponse() {} void SetHttpResponseCode(EHttpResponseCodes::Type InHttpResponseCode) final; - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; }; @@ -64,7 +64,7 @@ class OPENAPI_API OpenAPIUserApi::CreateUsersWithArrayInputResponse : public Res public: virtual ~CreateUsersWithArrayInputResponse() {} void SetHttpResponseCode(EHttpResponseCodes::Type InHttpResponseCode) final; - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; }; @@ -88,7 +88,7 @@ class OPENAPI_API OpenAPIUserApi::CreateUsersWithListInputResponse : public Resp public: virtual ~CreateUsersWithListInputResponse() {} void SetHttpResponseCode(EHttpResponseCodes::Type InHttpResponseCode) final; - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; }; @@ -113,7 +113,7 @@ class OPENAPI_API OpenAPIUserApi::DeleteUserResponse : public Response public: virtual ~DeleteUserResponse() {} void SetHttpResponseCode(EHttpResponseCodes::Type InHttpResponseCode) final; - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; }; @@ -137,7 +137,7 @@ class OPENAPI_API OpenAPIUserApi::GetUserByNameResponse : public Response public: virtual ~GetUserByNameResponse() {} void SetHttpResponseCode(EHttpResponseCodes::Type InHttpResponseCode) final; - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; OpenAPIUser Content; }; @@ -163,7 +163,7 @@ class OPENAPI_API OpenAPIUserApi::LoginUserResponse : public Response public: virtual ~LoginUserResponse() {} void SetHttpResponseCode(EHttpResponseCodes::Type InHttpResponseCode) final; - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; FString Content; }; @@ -185,7 +185,7 @@ class OPENAPI_API OpenAPIUserApi::LogoutUserResponse : public Response public: virtual ~LogoutUserResponse() {} void SetHttpResponseCode(EHttpResponseCodes::Type InHttpResponseCode) final; - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; }; @@ -212,7 +212,7 @@ class OPENAPI_API OpenAPIUserApi::UpdateUserResponse : public Response public: virtual ~UpdateUserResponse() {} void SetHttpResponseCode(EHttpResponseCodes::Type InHttpResponseCode) final; - bool FromJson(const TSharedPtr& JsonObject) final; + bool FromJson(const TSharedPtr& JsonValue) final; };