Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions cli/tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,14 @@
builder.AddZLoggerConsole();
}).CreateLogger<Tests>();

var gen = new ServiceDocGenerator();

var builder = new DependencyBuilder();

builder.AddSingleton<BeamStandardTelemetryAttributeProvider>();
builder.AddSingleton<SingletonDependencyList<ITelemetryAttributeProvider>>();
builder.AddSingleton<IMicroserviceArgs>(new MicroserviceArgs());
builder.AddSingleton<IServiceOpenApiDocsCache, ServiceOpenApiDocsCache>();

Check failure on line 153 in cli/tests/BasicTests.cs

View workflow job for this annotation

GitHub Actions / testCli (10.0.x)

Nie można znaleźć nazwy typu lub przestrzeni nazw „ServiceOpenApiDocsCache” (brak dyrektywy using lub odwołania do zestawu?)

Check failure on line 153 in cli/tests/BasicTests.cs

View workflow job for this annotation

GitHub Actions / testCli (10.0.x)

Nie można znaleźć nazwy typu lub przestrzeni nazw „IServiceOpenApiDocsCache” (brak dyrektywy using lub odwołania do zestawu?)

Check failure on line 153 in cli/tests/BasicTests.cs

View workflow job for this annotation

GitHub Actions / testCli (10.0.x)

Nie można znaleźć nazwy typu lub przestrzeni nazw „ServiceOpenApiDocsCache” (brak dyrektywy using lub odwołania do zestawu?)

Check failure on line 153 in cli/tests/BasicTests.cs

View workflow job for this annotation

GitHub Actions / testCli (10.0.x)

Nie można znaleźć nazwy typu lub przestrzeni nazw „IServiceOpenApiDocsCache” (brak dyrektywy using lub odwołania do zestawu?)
var provider = builder.Build();
var doc = gen.Generate<TroublesomeService>(provider);
var doc = provider.GetService<IServiceOpenApiDocsCache>().GetServiceDocument(typeof(TroublesomeService));

Check failure on line 155 in cli/tests/BasicTests.cs

View workflow job for this annotation

GitHub Actions / testCli (10.0.x)

Nie można znaleźć nazwy typu lub przestrzeni nazw „IServiceOpenApiDocsCache” (brak dyrektywy using lub odwołania do zestawu?)

Check failure on line 155 in cli/tests/BasicTests.cs

View workflow job for this annotation

GitHub Actions / testCli (10.0.x)

Nie można znaleźć nazwy typu lub przestrzeni nazw „IServiceOpenApiDocsCache” (brak dyrektywy using lub odwołania do zestawu?)

UnrealSourceGenerator.exportMacro = "TROUBLESOMEPROJECT_API";
UnrealSourceGenerator.blueprintExportMacro = "TROUBLESOMEPROJECTBLUEPRINTNODES_API";
Expand Down
15 changes: 2 additions & 13 deletions microservice/beamable.tooling.common/Microservice/AdminRoutes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Beamable.Server;
using Beamable.Server.Api.Usage;
using Beamable.Server.Editor;
using beamable.tooling.common.Microservice;
using Beamable.Tooling.Common.OpenAPI;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi;
Expand Down Expand Up @@ -84,19 +85,7 @@ public string HealthCheck()
[CustomResponseSerializationAttribute]
public string Docs()
{
var docs = new ServiceDocGenerator();
var ctx = GlobalProvider.GetService<StartupContext>();
var doc = docs.Generate(ctx, GlobalProvider);

if (!string.IsNullOrEmpty(PublicHost))
{
doc.Servers.Add(new OpenApiServer { Url = PublicHost });
}

var outputString = doc.Serialize(OpenApiSpecVersion.OpenApi3_0, OpenApiFormat.Json);


return outputString;
return GlobalProvider.GetService<IServiceOpenApiDocsCache>().GetCachedDocs(PublicHost);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Beamable.Common.Content;
using Beamable.Common.Dependencies;
using Beamable.Server;
using Beamable.Tooling.Common.OpenAPI;
using Microsoft.OpenApi;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;

namespace beamable.tooling.common.Microservice;


public interface IServiceOpenApiDocsCache
{
OpenApiDocument GetServiceDocument(Type type);
string GetCachedDocs(string publicHost);
}

public class ServiceOpenApiDocsCache : IServiceOpenApiDocsCache
{
public ServiceOpenApiDocsCache(IDependencyProvider dependencyProvider)
{
_dependencyProvider = dependencyProvider;
}
public bool HasCachedDocs => _cachedDocs.HasValue;
private readonly Optional<string> _cachedDocs = new Optional<string>();
private readonly IDependencyProvider _dependencyProvider;
private Dictionary<Type, OpenApiDocument> _serviceDocuments = new Dictionary<Type, OpenApiDocument>();

public OpenApiDocument GetServiceDocument(Type type)
{
if(_serviceDocuments.TryGetValue(type, out var doc))
{
return doc;
}
var gen = new ServiceDocGenerator();
doc = gen.GenerateDocumentByType(_dependencyProvider, type);
_serviceDocuments[type] = doc;
return doc;
}

public string GetCachedDocs(string publicHost)
{
if (HasCachedDocs)
{
return _cachedDocs.Value;
}

var docs = new ServiceDocGenerator();
var ctx = _dependencyProvider.GetService<StartupContext>();
var doc = docs.Generate(ctx, _dependencyProvider);
if (!string.IsNullOrEmpty(publicHost))
{
doc.Servers.Add(new OpenApiServer { Url = publicHost });
}

var outputString = doc.Serialize(OpenApiSpecVersion.OpenApi3_0, OpenApiFormat.Json);
_cachedDocs.Set(outputString);

return outputString;
}
}
36 changes: 25 additions & 11 deletions microservice/beamable.tooling.common/OpenAPI/ServiceDocGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -479,19 +479,33 @@ public static class DocGenExtensions
/// <returns>An OpenApiDocument containing the generated documentation.</returns>
public static OpenApiDocument Generate<TMicroservice>(this ServiceDocGenerator generator, IDependencyProvider provider) where TMicroservice : Microservice
{
var attr = typeof(TMicroservice).GetCustomAttribute(typeof(MicroserviceAttribute)) as MicroserviceAttribute;
var startupContext = new StartupContext
return generator.GenerateDocumentByType(provider, typeof(TMicroservice));
}

/// <summary>
/// Generates OpenAPI documentation for a specified microservice type.
/// </summary>
/// <typeparam name="TMicroservice">The type of the microservice to generate documentation for.</typeparam>
/// <param name="adminRoutes">The administrative routes associated with the microservice.</param>
/// <returns>An OpenApiDocument containing the generated documentation.</returns>
public static OpenApiDocument GenerateDocumentByType(this ServiceDocGenerator generator, IDependencyProvider provider, Type type, StartupContext context = null)
{
if (context == null)
{
routeSources = new BeamRouteSource[]
var attr = type.GetCustomAttribute(typeof(MicroserviceAttribute)) as MicroserviceAttribute;
context = new StartupContext
{
new BeamRouteSource
routeSources = new BeamRouteSource[]
{
InstanceType = typeof(TMicroservice)
}
},
attributes = attr,
args = provider.GetService<IMicroserviceArgs>()
};
return generator.Generate(startupContext, provider);
new BeamRouteSource
{
InstanceType = type
}
},
attributes = attr,
args = provider.GetService<IMicroserviceArgs>()
};
}
return generator.Generate(context, provider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ public static IDependencyBuilder ConfigureServices(StartupContext startupContext
.AddScoped<UserDataCache<Dictionary<string, string>>.FactoryFunction>(provider =>
StatsCacheFactory)
.AddScoped<UserDataCache<RankEntry>.FactoryFunction>(provider => LeaderboardRankEntryFactory)
.AddSingleton<IServiceOpenApiDocsCache, ServiceOpenApiDocsCache>()
.AddScoped<IBeamableServices>(ExtractSdks)

// allow the DI system to get a list of all telemetry attribute providers...
Expand Down
13 changes: 6 additions & 7 deletions microservice/microserviceTests/OpenAPITests/DocTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,23 @@ public void CreateRequestAttributes(IRequestAttributeContext ctx)
public void TestMethodScanning()
{
LoggingUtil.InitTestCorrelator();
var gen = new ServiceDocGenerator();

var builder = new DependencyBuilder();

builder.AddSingleton<BeamStandardTelemetryAttributeProvider>();
builder.AddSingleton<SingletonDependencyList<ITelemetryAttributeProvider>>();
builder.AddSingleton<IMicroserviceArgs, TestArgs>();
builder.AddSingleton<ExampleAttrs>();
builder.AddSingleton<IServiceOpenApiDocsCache, ServiceOpenApiDocsCache>();
var provider = builder.Build();
var doc = gen.Generate<DocService>(provider);


var doc = provider.GetService<IServiceOpenApiDocsCache>().GetServiceDocument(typeof(DocService));
string outputString = doc.Serialize(OpenApiSpecVersion.OpenApi3_0, OpenApiFormat.Json);


// Assert.AreEqual("docs", doc.Info.Title);
// Assert.AreEqual(1, doc.Paths.Count);

// var reqSchema = doc.Paths["/Add"].Operations[OperationType.Post].RequestBody.Content["application/json"].Schema;
// Assert.AreEqual(1, reqSchema.Required.Count);

var outputString = doc.Serialize(OpenApiSpecVersion.OpenApi3_0, OpenApiFormat.Json);
Console.WriteLine(outputString);
}

Expand Down
Loading