// Copyright Epic Games, Inc. All Rights Reserved. #include "V3/WebAPIOpenAPIProvider.h" #include "IWebAPIEditorModule.h" #include "WebAPIMessageLog.h" #include "WebAPIOpenAPIFactory.h" #include "Dom/WebAPISchema.h" #include "Internationalization/BreakIterator.h" #include "Serialization/JsonSerializer.h" #include "V3/WebAPIOpenAPIConverter.h" #include "V3/WebAPIOpenAPISchema.h" #define LOCTEXT_NAMESPACE "WebAPIOpenAPIProvider" TFuture FWebAPIOpenAPIProvider::ConvertToWebAPISchema(const TWeakObjectPtr& InDefinition) { TFuture FailedWithErrorsResult = MakeFulfilledPromise(EWebAPIConversionResult::FailedWithErrors).GetFuture(); TFuture FailedWithWarningsResult = MakeFulfilledPromise(EWebAPIConversionResult::FailedWithWarnings).GetFuture(); if(!InDefinition.IsValid()) { return FailedWithErrorsResult; } // Get cached file contents from Definition const UWebAPIOpenAPIAssetData* AssetData = InDefinition->AddOrGetImportedDataCache(TEXT("OpenAPI")); const TSharedPtr MessageLog = InDefinition->GetMessageLog(); // Load Json TSharedPtr JsonObject; if(!FJsonSerializer::Deserialize(TJsonReaderFactory::Create(AssetData->FileContents), JsonObject)) { return FailedWithErrorsResult; } // Parse OpenAPI JsonObject TSharedPtr Root = MakeShared(); if(!Root->FromJson(JsonObject.ToSharedRef())) { MessageLog->LogError(FText::FromString(TEXT("Failed to load the OpenAPI schema.")), FWebAPIOpenAPIProvider::LogName); return FailedWithErrorsResult; } // Convert to WebAPI schema UWebAPISchema* WebAPISchema = InDefinition->GetWebAPISchema(); WebAPISchema->TypeRegistry->Clear(); const TSharedRef Converter = MakeShared( Root, WebAPISchema, MessageLog.ToSharedRef(), InDefinition->GetProviderSettings()); if(Converter->Convert()) { if(!WebAPISchema->TypeRegistry->CheckAllNamed()) { return FailedWithWarningsResult; } return MakeFulfilledPromise(EWebAPIConversionResult::Succeeded).GetFuture(); } MessageLog->LogError(FText::FromString(TEXT("Failed to convert the OpenAPI schema to the WebAPI schema.")), FWebAPIOpenAPIProvider::LogName); return FailedWithErrorsResult; } #undef LOCTEXT_NAMESPACE