Skip to content

Commit

Permalink
Improve Open API API surface
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaliumhexacyanoferrat committed Oct 18, 2024
1 parent a4eaeb5 commit 95dd163
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 68 deletions.
16 changes: 0 additions & 16 deletions Modules/OpenApi/Discovery/Extensions.cs

This file was deleted.

40 changes: 1 addition & 39 deletions Modules/OpenApi/Discovery/MethodHandlerExplorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void Explore(IHandler handler, List<string> path, OpenApiDocument documen
}
}

var pathItem = GetPathItem(document, path, methodHandler.Operation);
var pathItem = OpenApiExtensions.GetPathItem(document, path, methodHandler.Operation);

foreach (var method in methodHandler.Configuration.SupportedMethods)
{
Expand Down Expand Up @@ -112,44 +112,6 @@ public void Explore(IHandler handler, List<string> path, OpenApiDocument documen
}
}

private static OpenApiPathItem GetPathItem(OpenApiDocument document, List<string> path, Operation operation)
{
var stringPath = BuildPath(operation.Path.Name, path);

if (document.Paths.TryGetValue(stringPath, out var existing))
{
return existing;
}

var newPath = new OpenApiPathItem();

document.Paths.Add(stringPath, newPath);

return newPath;
}

private static string BuildPath(string name, List<string> pathParts)
{
var builder = new StringBuilder("/");

if (pathParts.Count > 0)
{
builder.Append(string.Join('/', pathParts));
builder.Append('/');
}

if (name.Length > 0 && name[0] == '/')
{
builder.Append(name[1..]);
}
else
{
builder.Append(name);
}

return builder.ToString();
}

private static OpenApiParameterKind MapArgumentType(OperationArgumentSource source) => source switch
{
OperationArgumentSource.Path => OpenApiParameterKind.Path,
Expand Down
58 changes: 58 additions & 0 deletions Modules/OpenApi/Discovery/OpenApiExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Text;
using GenHTTP.Modules.Reflection.Operations;
using NSwag;

namespace GenHTTP.Modules.OpenApi.Discovery;

public static class OpenApiExtensions
{

public static bool MightBeNull(this Type type)
{
if (type.IsClass)
{
return true;
}

return Nullable.GetUnderlyingType(type) != null;
}

public static OpenApiPathItem GetPathItem(OpenApiDocument document, List<string> path, Operation operation)
{
var stringPath = BuildPath(operation.Path.Name, path);

if (document.Paths.TryGetValue(stringPath, out var existing))
{
return existing;
}

var newPath = new OpenApiPathItem();

document.Paths.Add(stringPath, newPath);

return newPath;
}

public static string BuildPath(string name, List<string> pathParts)
{
var builder = new StringBuilder("/");

if (pathParts.Count > 0)
{
builder.Append(string.Join('/', pathParts));
builder.Append('/');
}

if (name.Length > 0 && name[0] == '/')
{
builder.Append(name[1..]);
}
else
{
builder.Append(name);
}

return builder.ToString();
}

}
13 changes: 9 additions & 4 deletions Modules/OpenApi/Handler/OpenApiConcern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

namespace GenHTTP.Modules.OpenApi.Handler;

internal record ReturnDocument(OpenApiDocument Document, ulong Checksum);

public sealed class OpenApiConcern : IConcern
{
private OpenApiDocument? _Cached;
private ReturnDocument? _Cached;

#region Initialization

Expand Down Expand Up @@ -97,7 +99,7 @@ private IResponse GetDocument(IRequest request, OpenApiFormat format)
.Build();
}

private OpenApiDocument Discover(IRequest request, ApiDiscoveryRegistry registry)
private ReturnDocument Discover(IRequest request, ApiDiscoveryRegistry registry)
{
if (EnableCaching && _Cached != null)
{
Expand Down Expand Up @@ -126,12 +128,15 @@ private OpenApiDocument Discover(IRequest request, ApiDiscoveryRegistry registry

if (EnableCaching)
{
_Cached = document;
_Cached = new (document, GetChecksum(document));
return _Cached;
}

return document;
return new (document, GetChecksum(document));
}

private static ulong GetChecksum(OpenApiDocument document) => (ulong)document.ToJson().GetHashCode();

#endregion

}
10 changes: 5 additions & 5 deletions Modules/OpenApi/Handler/OpenApiContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal class OpenApiContent : IResponseContent

#region Initialization

internal OpenApiContent(OpenApiDocument document, OpenApiFormat format)
internal OpenApiContent(ReturnDocument document, OpenApiFormat format)
{
Document = document;
Format = format;
Expand All @@ -21,25 +21,25 @@ internal OpenApiContent(OpenApiDocument document, OpenApiFormat format)

public ulong? Length => null;

private OpenApiDocument Document { get; }
private ReturnDocument Document { get; }

private OpenApiFormat Format { get; }

#endregion

#region Functionality

public ValueTask<ulong?> CalculateChecksumAsync() => new((ulong)Document.ToJson().GetHashCode());
public ValueTask<ulong?> CalculateChecksumAsync() => new(Document.Checksum);

public async ValueTask WriteAsync(Stream target, uint bufferSize)
{
if (Format == OpenApiFormat.Json)
{
await target.WriteAsync(Encoding.UTF8.GetBytes(Document.ToJson()));
await target.WriteAsync(Encoding.UTF8.GetBytes(Document.Document.ToJson()));
}
else
{
await target.WriteAsync(Encoding.UTF8.GetBytes(Document.ToYaml()));
await target.WriteAsync(Encoding.UTF8.GetBytes(Document.Document.ToYaml()));
}
}

Expand Down
7 changes: 4 additions & 3 deletions Testing/Acceptance/Modules/OpenApi/InfrastructureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public async Task TestContentIsPassed()
{
var api = Inline.Create()
.Get("/some-path", () => "Hello World")
.Add(ApiDescription.Create());
.AddOpenApi();

using var host = TestHost.Run(api);

Expand Down Expand Up @@ -43,9 +43,10 @@ private async Task<int> RunCachingTest(bool cacheEnabled)

using var host = TestHost.Run(api);

await host.GetResponseAsync("/openapi.json");
await host.GetResponseAsync("/openapi.json");
AssertX.Contains("\"openapi\"", await (await host.GetResponseAsync("/openapi.json")).GetContentAsync());
AssertX.Contains("openapi:", await (await host.GetResponseAsync("/openapi.yaml")).GetContentAsync());

return counter;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace GenHTTP.Testing.Acceptance;

public static class TestExtensions
public static class TestOpenApiExtensions
{

public static IHandlerBuilder<HandlerBuilder> Wrap(this IHandler handler) => new HandlerBuilder(handler);
Expand Down

0 comments on commit 95dd163

Please sign in to comment.