Skip to content

Commit

Permalink
Merge branch 'timdeschryver-http-auditing'
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Nov 9, 2023
2 parents 8c6d153 + 5c78c9b commit e5ba883
Show file tree
Hide file tree
Showing 39 changed files with 1,183 additions and 117 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using JasperFx.CodeGeneration;
using JasperFx.Core;
using JasperFx.Core.Reflection;
using Microsoft.AspNetCore.Http;
using Shouldly;
using WolverineWebApi;

namespace Wolverine.Http.Tests;

public class endpoint_adds_requesttype_audit_tags_to_activity : IntegrationContext
{
public endpoint_adds_requesttype_audit_tags_to_activity(AppFixture fixture) : base(fixture)
{
}

[Fact]
public void finds_audit_members_from_attributes()
{
var chain = HttpChains.ChainFor("POST", "/auditable/empty");

chain.AuditedMembers.Single()
.MemberName.ShouldBe(nameof(AuditablePostBody.Id));
}

}
19 changes: 13 additions & 6 deletions src/Http/Wolverine.Http/HttpChain.Codegen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
using Microsoft.AspNetCore.Routing;
using Wolverine.Http.CodeGen;
using Wolverine.Http.Resources;
using Wolverine.Persistence;
using Wolverine.Logging;
using Wolverine.Runtime.Handlers;

namespace Wolverine.Http;

public partial class HttpChain
{
internal string? SourceCode => _generatedType?.SourceCode;

void ICodeFile.AssembleTypes(GeneratedAssembly assembly)
{
assembly.UsingNamespaces!.Fill(typeof(RoutingHttpContextExtensions).Namespace);
Expand Down Expand Up @@ -55,7 +57,7 @@ bool ICodeFile.AttachTypesSynchronously(GenerationRules rules, Assembly assembly
{
return false;
}

Debug.WriteLine(_generatedType?.SourceCode);

return true;
Expand All @@ -80,7 +82,12 @@ internal IEnumerable<Frame> DetermineFrames(GenerationRules rules)
{
Postprocessors.Add(new WriteEmptyBodyStatusCode());
}


if (AuditedMembers.Any())
{
Middleware.Insert(0, new AuditToActivityFrame(this));
}

var index = 0;
foreach (var frame in Middleware)
{
Expand All @@ -89,12 +96,12 @@ internal IEnumerable<Frame> DetermineFrames(GenerationRules rules)
{
RouteParameterStrategy.TryApplyRouteVariables(this, call);
}

foreach (var result in frame.Creates.Where(x => x.VariableType.CanBeCastTo<IResult>()))
{
result.OverrideName("result" + ++index);
}

yield return frame;
}

Expand All @@ -110,7 +117,7 @@ internal IEnumerable<Frame> DetermineFrames(GenerationRules rules)
private string determineFileName()
{
var parts = RoutePattern.RawText.Replace("{", "").Replace("*", "").Replace("}", "").Split('/').Select(x => x.Split(':').First());

return _httpMethods.Select(x => x.ToUpper()).Concat(parts).Join("_").Replace("-", "_").Replace("__", "_");
}
}
30 changes: 21 additions & 9 deletions src/Http/Wolverine.Http/HttpChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public static bool IsValidResponseType(Type type)
private GeneratedType? _generatedType;
private Type? _handlerType;
private string _description;

private Type? _requestType;

public HttpChain(MethodCall method, HttpGraph parent)
{
_description = method.ToString();
Expand Down Expand Up @@ -93,7 +94,7 @@ public HttpChain(MethodCall method, HttpGraph parent)

applyMetadata();
}

private bool tryFindResourceType(MethodCall method, out Type resourceType)
{
resourceType = typeof(void);
Expand Down Expand Up @@ -151,17 +152,28 @@ internal void MapToRoute(string method, string url, int? order = null, string? d

public RoutePattern? RoutePattern { get; private set; }

public Type? RequestType { get; internal set; }
public Type? RequestType
{
get => _requestType;
internal set
{
_requestType = value;
if (_requestType != null)
{
applyAuditAttributes(_requestType);
}
}
}

public override string Description => _description;

internal RouteEndpoint? Endpoint { get; private set; }

/// <summary>
/// Required TenancyMode for this http chain
/// </summary>
public TenancyMode? TenancyMode { get; set; }


public static HttpChain ChainFor<T>(Expression<Action<T>> expression, HttpGraph? parent = null)
{
Expand Down Expand Up @@ -230,7 +242,7 @@ private void applyMetadata()
Metadata.WithMetadata(new FromRouteMetadata(parameter.Name));
}
}

Metadata
.WithMetadata(this)
.WithMetadata(new WolverineMarker())
Expand All @@ -252,7 +264,7 @@ private void applyMetadata()
{
Metadata.Produces(200);
}

foreach (var attribute in Method.HandlerType.GetCustomAttributes()) Metadata.WithMetadata(attribute);
foreach (var attribute in Method.Method.GetCustomAttributes()) Metadata.WithMetadata(attribute);
}
Expand All @@ -267,7 +279,7 @@ public bool FindRouteVariable(ParameterInfo parameter, out Variable variable)
variable = existing;
return true;
}

var matches = RoutePattern!.Parameters.Any(x => x.Name == parameter.Name);
if (matches)
{
Expand Down Expand Up @@ -299,7 +311,7 @@ public bool FindRouteVariable(Type variableType, string routeOrParameterName, ou
variable = matched;
return true;
}

var matches = RoutePattern!.Parameters.Any(x => x.Name == routeOrParameterName);
if (matches)
{
Expand Down
25 changes: 25 additions & 0 deletions src/Http/WolverineWebApi/AuditableEndpoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Wolverine.Attributes;
using Wolverine.Http;

namespace WolverineWebApi;

public class AuditableEndpoint
{

[WolverinePost("/auditable/post"), EmptyResponse]
public string Post(AuditablePostBody body)
{
return "Hello";
}

[WolverinePost("/auditable/empty"), EmptyResponse]
public void EmptyPost(AuditablePostBody command)
{
}
}

public class AuditablePostBody
{
[Audit]
public Guid Id { get; set; }
}
Loading

0 comments on commit e5ba883

Please sign in to comment.