Skip to content

Commit

Permalink
UE4 client generator fixes (#6438)
Browse files Browse the repository at this point in the history
* Added the possibility for a codegen to set the location of the generated model files, similarly to API files.

* Removed std::shared_ptr from the generated types. Types in UE4Codegen are handled as POD structs, not shared pointers.

* Fixed handling of body parameters as per the specification, the single body object is written directly as the payload.

* Fixed handling of files and binaries, if a field is both isFile and isBinary, file will take precedence.

* Updated cpp-ue4 client samples

* Fixed handling of enums in models

Co-authored-by: William Cheng <[email protected]>
  • Loading branch information
Kahncode and wing328 authored May 31, 2020
1 parent a96e783 commit e3eb3c2
Show file tree
Hide file tree
Showing 25 changed files with 84 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ public interface CodegenConfig {

void postProcessParameter(CodegenParameter parameter);

String modelFilename(String templateName, String modelName);

String apiFilename(String templateName, String tag);

String apiTestFilename(String templateName, String tag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4828,6 +4828,11 @@ public String apiFilename(String templateName, String tag) {
return apiFileFolder() + File.separator + toApiFilename(tag) + suffix;
}

public String modelFilename(String templateName, String modelName) {
String suffix = modelTemplateFiles().get(templateName);
return modelFileFolder() + File.separator + toModelFilename(modelName) + suffix;
}

/**
* Return the full path and API documentation file
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,14 +363,9 @@ private void generateModelDocumentation(List<File> files, Map<String, Object> mo
}
}

private String getModelFilenameByTemplate(String modelName, String templateName){
String suffix = config.modelTemplateFiles().get(templateName);
return config.modelFileFolder() + File.separator + config.toModelFilename(modelName) + suffix;
}

private void generateModel(List<File> files, Map<String, Object> models, String modelName) throws IOException {
for (String templateName : config.modelTemplateFiles().keySet()) {
String filename = getModelFilenameByTemplate(modelName, templateName);
String filename = config.modelFilename(templateName, modelName);
File written = processTemplateToFile(models, templateName, filename, generateModels, CodegenConstants.MODELS);
if (written != null) {
files.add(written);
Expand Down Expand Up @@ -429,7 +424,7 @@ void generateModels(List<File> files, List<Object> allModels, List<String> unuse

for (String templateName : config.modelTemplateFiles().keySet()) {
// HACK: Because this returns early, could lead to some invalid model reporting.
String filename = getModelFilenameByTemplate(name, templateName);
String filename = config.modelFilename(templateName, name);
Path path = java.nio.file.Paths.get(filename);
this.templateProcessor.skip(path,"Skipped prior to model processing due to import mapping conflict (either by user or by generator)." );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,18 +331,16 @@ public String apiFileFolder() {
return outputFolder + File.separator + apiPackage().replace("::", File.separator);
}

/*
@Override
public String modelFilename(String templateName, String tag) {
public String modelFilename(String templateName, String modelName) {
String suffix = modelTemplateFiles().get(templateName);
String folder = privateFolder;
if (suffix == ".h") {
folder = publicFolder;
}

return modelFileFolder() + File.separator + folder + File.separator + toModelFilename(tag) + suffix;
return modelFileFolder() + File.separator + folder + File.separator + toModelFilename(modelName) + suffix;
}
*/

@Override
public String toModelFilename(String name) {
Expand Down Expand Up @@ -399,6 +397,10 @@ public String getTypeDeclaration(Schema p) {
}
}

@Override
public String getTypeDeclaration(String name) {
return name;
}

@Override
public String toDefaultValue(Schema p) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,17 @@ void {{classname}}::{{operationIdCamelCase}}Request::SetupHttpRequest(const TSha
FString JsonBody;
JsonWriter Writer = TJsonWriterFactory<>::Create(&JsonBody);

Writer->WriteObjectStart();
{{#bodyParams}}
{{#required}}
Writer->WriteIdentifierPrefix(TEXT("{{baseName}}")); WriteJsonValue(Writer, {{paramName}});
WriteJsonValue(Writer, {{paramName}});
{{/required}}
{{^required}}
if ({{paramName}}.IsSet())
{
Writer->WriteIdentifierPrefix(TEXT("{{baseName}}")); WriteJsonValue(Writer, {{paramName}}.GetValue());
WriteJsonValue(Writer, {{paramName}}.GetValue());
}
{{/required}}
{{/bodyParams}}
Writer->WriteObjectEnd();
Writer->Close();

HttpRequest->SetHeader(TEXT("Content-Type"), TEXT("application/json; charset=utf-8"));
Expand All @@ -176,29 +174,29 @@ void {{classname}}::{{operationIdCamelCase}}Request::SetupHttpRequest(const TSha
{{#isFile}}
FormData.AddFilePart(TEXT("{{baseName}}"), {{paramName}});
{{/isFile}}
{{^isFile}}
{{#isBinary}}
FormData.AddBinaryPart(TEXT("{{baseName}}"), {{paramName}});
{{/isBinary}}
{{#isBinary}}
{{^isFile}}
{{^isBinary}}
FormData.AddStringPart(TEXT("{{baseName}}"), *ToUrlString({{paramName}}));
{{/isFile}}
{{/isBinary}}
{{/isFile}}
{{/required}}
{{^required}}
if({{paramName}}.IsSet())
{
{{#isFile}}
FormData.AddFilePart(TEXT("{{baseName}}"), {{paramName}}.GetValue());
{{/isFile}}
{{^isFile}}
{{#isBinary}}
FormData.AddBinaryPart(TEXT("{{baseName}}"), {{paramName}}.GetValue());
{{/isBinary}}
{{^isBinary}}
{{^isFile}}
FormData.AddStringPart(TEXT("{{baseName}}"), *ToUrlString({{paramName}}.GetValue()));
{{/isFile}}
{{/isBinary}}
{{/isFile}}
}
{{/required}}
{{/isContainer}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ inline void WriteJsonValue(JsonWriter& Writer, const {{classname}}::{{{enumName}

inline bool TryGetJsonValue(const TSharedPtr<FJsonValue>& JsonValue, {{classname}}::{{{enumName}}}& Value)
{
{{#allowableValues}}
FString TmpValue;
if (JsonValue->TryGetString(TmpValue))
{
Expand All @@ -55,6 +56,7 @@ inline bool TryGetJsonValue(const TSharedPtr<FJsonValue>& JsonValue, {{classname
return true;
}
}
{{/allowableValues}}
return false;
}

Expand Down
31 changes: 31 additions & 0 deletions samples/client/petstore/cpp-ue4/.openapi-generator/FILES
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
OpenAPI.Build.cs
Private\OpenAPIApiResponse.cpp
Private\OpenAPIBaseModel.cpp
Private\OpenAPICategory.cpp
Private\OpenAPIHelpers.cpp
Private\OpenAPIModule.cpp
Private\OpenAPIModule.h
Private\OpenAPIOrder.cpp
Private\OpenAPIPet.cpp
Private\OpenAPIPetApi.cpp
Private\OpenAPIPetApiOperations.cpp
Private\OpenAPIStoreApi.cpp
Private\OpenAPIStoreApiOperations.cpp
Private\OpenAPITag.cpp
Private\OpenAPIUser.cpp
Private\OpenAPIUserApi.cpp
Private\OpenAPIUserApiOperations.cpp
Public\OpenAPIApiResponse.h
Public\OpenAPIBaseModel.h
Public\OpenAPICategory.h
Public\OpenAPIHelpers.h
Public\OpenAPIOrder.h
Public\OpenAPIPet.h
Public\OpenAPIPetApi.h
Public\OpenAPIPetApiOperations.h
Public\OpenAPIStoreApi.h
Public\OpenAPIStoreApiOperations.h
Public\OpenAPITag.h
Public\OpenAPIUser.h
Public\OpenAPIUserApi.h
Public\OpenAPIUserApiOperations.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ inline bool TryGetJsonValue(const TSharedPtr<FJsonValue>& JsonValue, OpenAPIOrde
FString TmpValue;
if (JsonValue->TryGetString(TmpValue))
{
static TMap<FString, OpenAPIOrder::StatusEnum> StringToEnum = { };
static TMap<FString, OpenAPIOrder::StatusEnum> StringToEnum = {
{ TEXT("placed"), OpenAPIOrder::StatusEnum::Placed },
{ TEXT("approved"), OpenAPIOrder::StatusEnum::Approved },
{ TEXT("delivered"), OpenAPIOrder::StatusEnum::Delivered }, };

const auto Found = StringToEnum.Find(TmpValue);
if(Found)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ inline bool TryGetJsonValue(const TSharedPtr<FJsonValue>& JsonValue, OpenAPIPet:
FString TmpValue;
if (JsonValue->TryGetString(TmpValue))
{
static TMap<FString, OpenAPIPet::StatusEnum> StringToEnum = { };
static TMap<FString, OpenAPIPet::StatusEnum> StringToEnum = {
{ TEXT("available"), OpenAPIPet::StatusEnum::Available },
{ TEXT("pending"), OpenAPIPet::StatusEnum::Pending },
{ TEXT("sold"), OpenAPIPet::StatusEnum::Sold }, };

const auto Found = StringToEnum.Find(TmpValue);
if(Found)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ void OpenAPIPetApi::AddPetRequest::SetupHttpRequest(const TSharedRef<IHttpReques
FString JsonBody;
JsonWriter Writer = TJsonWriterFactory<>::Create(&JsonBody);

Writer->WriteObjectStart();
Writer->WriteIdentifierPrefix(TEXT("body")); WriteJsonValue(Writer, Body);
Writer->WriteObjectEnd();
WriteJsonValue(Writer, Body);
Writer->Close();

HttpRequest->SetHeader(TEXT("Content-Type"), TEXT("application/json; charset=utf-8"));
Expand Down Expand Up @@ -366,9 +364,7 @@ void OpenAPIPetApi::UpdatePetRequest::SetupHttpRequest(const TSharedRef<IHttpReq
FString JsonBody;
JsonWriter Writer = TJsonWriterFactory<>::Create(&JsonBody);

Writer->WriteObjectStart();
Writer->WriteIdentifierPrefix(TEXT("body")); WriteJsonValue(Writer, Body);
Writer->WriteObjectEnd();
WriteJsonValue(Writer, Body);
Writer->Close();

HttpRequest->SetHeader(TEXT("Content-Type"), TEXT("application/json; charset=utf-8"));
Expand Down Expand Up @@ -517,7 +513,6 @@ void OpenAPIPetApi::UploadFileRequest::SetupHttpRequest(const TSharedRef<IHttpRe
if(File.IsSet())
{
FormData.AddFilePart(TEXT("file"), File.GetValue());
FormData.AddBinaryPart(TEXT("file"), File.GetValue());
}

FormData.SetupHttpRequest(HttpRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,7 @@ void OpenAPIStoreApi::PlaceOrderRequest::SetupHttpRequest(const TSharedRef<IHttp
FString JsonBody;
JsonWriter Writer = TJsonWriterFactory<>::Create(&JsonBody);

Writer->WriteObjectStart();
Writer->WriteIdentifierPrefix(TEXT("body")); WriteJsonValue(Writer, Body);
Writer->WriteObjectEnd();
WriteJsonValue(Writer, Body);
Writer->Close();

HttpRequest->SetHeader(TEXT("Content-Type"), TEXT("application/json; charset=utf-8"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ void OpenAPIUserApi::CreateUserRequest::SetupHttpRequest(const TSharedRef<IHttpR
FString JsonBody;
JsonWriter Writer = TJsonWriterFactory<>::Create(&JsonBody);

Writer->WriteObjectStart();
Writer->WriteIdentifierPrefix(TEXT("body")); WriteJsonValue(Writer, Body);
Writer->WriteObjectEnd();
WriteJsonValue(Writer, Body);
Writer->Close();

HttpRequest->SetHeader(TEXT("Content-Type"), TEXT("application/json; charset=utf-8"));
Expand Down Expand Up @@ -102,9 +100,7 @@ void OpenAPIUserApi::CreateUsersWithArrayInputRequest::SetupHttpRequest(const TS
FString JsonBody;
JsonWriter Writer = TJsonWriterFactory<>::Create(&JsonBody);

Writer->WriteObjectStart();
Writer->WriteIdentifierPrefix(TEXT("body")); WriteJsonValue(Writer, Body);
Writer->WriteObjectEnd();
WriteJsonValue(Writer, Body);
Writer->Close();

HttpRequest->SetHeader(TEXT("Content-Type"), TEXT("application/json; charset=utf-8"));
Expand Down Expand Up @@ -161,9 +157,7 @@ void OpenAPIUserApi::CreateUsersWithListInputRequest::SetupHttpRequest(const TSh
FString JsonBody;
JsonWriter Writer = TJsonWriterFactory<>::Create(&JsonBody);

Writer->WriteObjectStart();
Writer->WriteIdentifierPrefix(TEXT("body")); WriteJsonValue(Writer, Body);
Writer->WriteObjectEnd();
WriteJsonValue(Writer, Body);
Writer->Close();

HttpRequest->SetHeader(TEXT("Content-Type"), TEXT("application/json; charset=utf-8"));
Expand Down Expand Up @@ -433,9 +427,7 @@ void OpenAPIUserApi::UpdateUserRequest::SetupHttpRequest(const TSharedRef<IHttpR
FString JsonBody;
JsonWriter Writer = TJsonWriterFactory<>::Create(&JsonBody);

Writer->WriteObjectStart();
Writer->WriteIdentifierPrefix(TEXT("body")); WriteJsonValue(Writer, Body);
Writer->WriteObjectEnd();
WriteJsonValue(Writer, Body);
Writer->Close();

HttpRequest->SetHeader(TEXT("Content-Type"), TEXT("application/json; charset=utf-8"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class OPENAPI_API OpenAPIPet : public Model
TOptional<int64> Id;
TOptional<OpenAPICategory> Category;
FString Name;
TArray<std::shared_ptr<FString>> PhotoUrls;
TOptional<TArray<std::shared_ptr<OpenAPIOpenAPITag>>> Tags;
TArray<FString> PhotoUrls;
TOptional<TArray<OpenAPITag>> Tags;
enum class StatusEnum
{
Available,
Expand Down
12 changes: 6 additions & 6 deletions samples/client/petstore/cpp-ue4/Public/OpenAPIPetApiOperations.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class OPENAPI_API OpenAPIPetApi::AddPetRequest : public Request
FString ComputePath() const final;

/* Pet object that needs to be added to the store */
std::shared_ptr<OpenAPIOpenAPIPet> Body;
OpenAPIPet Body;
};

class OPENAPI_API OpenAPIPetApi::AddPetResponse : public Response
Expand Down Expand Up @@ -89,7 +89,7 @@ class OPENAPI_API OpenAPIPetApi::FindPetsByStatusRequest : public Request
Sold,
};
/* Status values that need to be considered for filter */
TArray<std::shared_ptr<StatusEnum>> Status;
TArray<StatusEnum> Status;
};

class OPENAPI_API OpenAPIPetApi::FindPetsByStatusResponse : public Response
Expand All @@ -99,7 +99,7 @@ class OPENAPI_API OpenAPIPetApi::FindPetsByStatusResponse : public Response
void SetHttpResponseCode(EHttpResponseCodes::Type InHttpResponseCode) final;
bool FromJson(const TSharedPtr<FJsonValue>& JsonObject) final;

TArray<std::shared_ptr<OpenAPIOpenAPIPet>> Content;
TArray<OpenAPIPet> Content;
};

/* Finds Pets by tags
Expand All @@ -114,7 +114,7 @@ class OPENAPI_API OpenAPIPetApi::FindPetsByTagsRequest : public Request
FString ComputePath() const final;

/* Tags to filter by */
TArray<std::shared_ptr<FString>> Tags;
TArray<FString> Tags;
};

class OPENAPI_API OpenAPIPetApi::FindPetsByTagsResponse : public Response
Expand All @@ -124,7 +124,7 @@ class OPENAPI_API OpenAPIPetApi::FindPetsByTagsResponse : public Response
void SetHttpResponseCode(EHttpResponseCodes::Type InHttpResponseCode) final;
bool FromJson(const TSharedPtr<FJsonValue>& JsonObject) final;

TArray<std::shared_ptr<OpenAPIOpenAPIPet>> Content;
TArray<OpenAPIPet> Content;
};

/* Find pet by ID
Expand Down Expand Up @@ -163,7 +163,7 @@ class OPENAPI_API OpenAPIPetApi::UpdatePetRequest : public Request
FString ComputePath() const final;

/* Pet object that needs to be added to the store */
std::shared_ptr<OpenAPIOpenAPIPet> Body;
OpenAPIPet Body;
};

class OPENAPI_API OpenAPIPetApi::UpdatePetResponse : public Response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class OPENAPI_API OpenAPIStoreApi::GetInventoryResponse : public Response
void SetHttpResponseCode(EHttpResponseCodes::Type InHttpResponseCode) final;
bool FromJson(const TSharedPtr<FJsonValue>& JsonObject) final;

TMap<FString, std::shared_ptr<int32>> Content;
TMap<FString, int32> Content;
};

/* Find purchase order by ID
Expand Down Expand Up @@ -104,7 +104,7 @@ class OPENAPI_API OpenAPIStoreApi::PlaceOrderRequest : public Request
FString ComputePath() const final;

/* order placed for purchasing the pet */
std::shared_ptr<OpenAPIOpenAPIOrder> Body;
OpenAPIOrder Body;
};

class OPENAPI_API OpenAPIStoreApi::PlaceOrderResponse : public Response
Expand Down
Loading

0 comments on commit e3eb3c2

Please sign in to comment.