Skip to content

Commit

Permalink
Endpoint Parameter Registration (#30)
Browse files Browse the repository at this point in the history
* Change default registration for post/patch/put
* Allow overrides of registration
  • Loading branch information
viceroypenguin authored Apr 2, 2024
1 parent 439b187 commit da88917
Show file tree
Hide file tree
Showing 118 changed files with 1,764 additions and 34 deletions.
19 changes: 19 additions & 0 deletions src/Immediate.Apis.Generators/ITypeSymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,23 @@ typeSymbol is INamedTypeSymbol
},
},
};

public static bool IsEndpointRegistrationOverrideAttribute(this ITypeSymbol? typeSymbol) =>
typeSymbol is INamedTypeSymbol
{
Name: "EndpointRegistrationOverrideAttribute",
ContainingNamespace:
{
Name: "Shared",
ContainingNamespace:
{
Name: "Apis",
ContainingNamespace:
{
Name: "Immediate",
ContainingNamespace.IsGlobalNamespace: true,
},
},
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public sealed partial class ImmediateApisGenerator
private sealed record Method
{
public required string HttpMethod { get; init; }
public required string ParameterAttribute { get; init; }
public required string Route { get; init; }

public required string ClassName { get; init; }
Expand Down
24 changes: 24 additions & 0 deletions src/Immediate.Apis.Generators/ImmediateApisGenerator.Transform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ CancellationToken token

token.ThrowIfCancellationRequested();

var parameterAttribute = GetParameterAttribute(handleMethod.Parameters[0].Type, httpMethod);

token.ThrowIfCancellationRequested();

var useCustomization = HasCustomizationMethod(symbol);

token.ThrowIfCancellationRequested();
Expand All @@ -79,6 +83,7 @@ CancellationToken token
return new()
{
HttpMethod = httpMethod,
ParameterAttribute = parameterAttribute,
Route = route,

ClassName = className,
Expand Down Expand Up @@ -159,4 +164,23 @@ m is
}
&& SymbolEqualityComparer.IncludeNullability.Equals(returnInnerType, paramType)
);

private static string GetParameterAttribute(ITypeSymbol parameterType, string httpMethod)
{
foreach (var a in parameterType.GetAttributes())
{
if (a.AttributeClass.IsEndpointRegistrationOverrideAttribute())
{
if (a.ConstructorArguments.Length != 0)
return (string)a.ConstructorArguments[0].Value!;

if (a.NamedArguments.Length != 0)
return (string)a.NamedArguments[0].Value.Value!;
}
}

return httpMethod is "MapGet" or "MapDelete"
? "AsParameters"
: "FromBody";
}
}
3 changes: 2 additions & 1 deletion src/Immediate.Apis.Generators/Templates/Route.sbntxt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

#pragma warning disable CS1591
Expand All @@ -12,7 +13,7 @@ public static partial class {{ assembly }}RoutesBuilder
.{{ method.http_method }}(
"{{ method.route }}",
async (
[AsParameters] {{ method.parameter_type }} parameters,
[{{ method.parameter_attribute }}] {{ method.parameter_type }} parameters,
[FromServices] {{ method.class_name }}.Handler handler,
CancellationToken token
) =>
Expand Down
54 changes: 54 additions & 0 deletions src/Immediate.Apis.Shared/EndpointRegistration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
namespace Immediate.Apis.Shared;

/// <summary>
/// Common Registrations for use with <see cref="EndpointRegistrationOverrideAttribute"/>.
/// </summary>
public static class EndpointRegistration
{
/// <summary>
/// Use <c>[AsParameters]</c> as the registration
/// </summary>
public const string AsParameters = nameof(AsParameters);

/// <summary>
/// Use <c>[FromBody]</c> as the registration
/// </summary>
public const string FromBody = nameof(FromBody);

/// <summary>
/// Use <c>[FromForm]</c> as the registration
/// </summary>
public const string FromForm = nameof(FromForm);

/// <summary>
/// Use <c>[FromHeader]</c> as the registration
/// </summary>
public const string FromHeader = nameof(FromHeader);

/// <summary>
/// Use <c>[FromQuery]</c> as the registration
/// </summary>
public const string FromQuery = nameof(FromQuery);

/// <summary>
/// Use <c>[FromRoute]</c> as the registration
/// </summary>
public const string FromRoute = nameof(FromRoute);
}

/// <summary>
/// Specify how the handler parameter should be registered with Minimal Apis
/// </summary>
/// <param name="registration">
/// The desired registration
/// </param>
[AttributeUsage(AttributeTargets.Class)]
public sealed class EndpointRegistrationOverrideAttribute(
string registration
) : Attribute
{
/// <summary>
/// The desired registration
/// </summary>
public string Registration { get; } = registration;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
namespace Immediate.Apis.FunctionalTests.Features.WeatherForecast;

[Handler]
[MapGet("/forecast")]
[MapGet("/forecast/{id:int}")]
[AllowAnonymous]
public static partial class Get
{
internal static void CustomizeEndpoint(IEndpointConventionBuilder endpoint)
=> endpoint
.WithDescription("Gets the current weather forecast");

public sealed record Query;
public sealed record Query
{
public required int Id { get; init; }
public required string Filter { get; init; }
}

public sealed record Result
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
namespace Immediate.Apis.FunctionalTests.Features.WeatherForecast;

[Handler]
[MapPut("/forecast")]
[MapPut("/forecast/{date:datetime}")]
[Authorize("Test")]
public static partial class Put
{
[EndpointRegistrationOverride(EndpointRegistration.AsParameters)]
public sealed record Command
{
public required DateOnly Date { get; init; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//HintName: RouteBuilder.Dummy_GetUsersQuery.g.cs
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

#pragma warning disable CS1591
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//HintName: RouteBuilder.Dummy_GetUsersQuery.g.cs
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

#pragma warning disable CS1591
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//HintName: RouteBuilder.Dummy_GetUsersQuery.g.cs
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

#pragma warning disable CS1591
Expand All @@ -13,7 +14,7 @@ private static void MapDummy_GetUsersQueryEndpoint(IEndpointRouteBuilder app)
.MapPatch(
"/test",
async (
[AsParameters] global::Dummy.GetUsersQuery.Query parameters,
[FromBody] global::Dummy.GetUsersQuery.Query parameters,
[FromServices] global::Dummy.GetUsersQuery.Handler handler,
CancellationToken token
) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//HintName: RouteBuilder.Dummy_GetUsersQuery.g.cs
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

#pragma warning disable CS1591
Expand All @@ -13,7 +14,7 @@ private static void MapDummy_GetUsersQueryEndpoint(IEndpointRouteBuilder app)
.MapPost(
"/test",
async (
[AsParameters] global::Dummy.GetUsersQuery.Query parameters,
[FromBody] global::Dummy.GetUsersQuery.Query parameters,
[FromServices] global::Dummy.GetUsersQuery.Handler handler,
CancellationToken token
) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//HintName: RouteBuilder.Dummy_GetUsersQuery.g.cs
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

#pragma warning disable CS1591
Expand All @@ -13,7 +14,7 @@ private static void MapDummy_GetUsersQueryEndpoint(IEndpointRouteBuilder app)
.MapPut(
"/test",
async (
[AsParameters] global::Dummy.GetUsersQuery.Query parameters,
[FromBody] global::Dummy.GetUsersQuery.Query parameters,
[FromServices] global::Dummy.GetUsersQuery.Handler handler,
CancellationToken token
) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//HintName: RouteBuilder.Dummy_GetUsersQuery.g.cs
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

#pragma warning disable CS1591
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//HintName: RouteBuilder.Dummy_GetUsersQuery.g.cs
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

#pragma warning disable CS1591
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//HintName: RouteBuilder.Dummy_GetUsersQuery.g.cs
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

#pragma warning disable CS1591
Expand All @@ -13,7 +14,7 @@ private static void MapDummy_GetUsersQueryEndpoint(IEndpointRouteBuilder app)
.MapPatch(
"/test",
async (
[AsParameters] global::Dummy.GetUsersQuery.Query parameters,
[FromBody] global::Dummy.GetUsersQuery.Query parameters,
[FromServices] global::Dummy.GetUsersQuery.Handler handler,
CancellationToken token
) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//HintName: RouteBuilder.Dummy_GetUsersQuery.g.cs
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

#pragma warning disable CS1591
Expand All @@ -13,7 +14,7 @@ private static void MapDummy_GetUsersQueryEndpoint(IEndpointRouteBuilder app)
.MapPost(
"/test",
async (
[AsParameters] global::Dummy.GetUsersQuery.Query parameters,
[FromBody] global::Dummy.GetUsersQuery.Query parameters,
[FromServices] global::Dummy.GetUsersQuery.Handler handler,
CancellationToken token
) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//HintName: RouteBuilder.Dummy_GetUsersQuery.g.cs
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

#pragma warning disable CS1591
Expand All @@ -13,7 +14,7 @@ private static void MapDummy_GetUsersQueryEndpoint(IEndpointRouteBuilder app)
.MapPut(
"/test",
async (
[AsParameters] global::Dummy.GetUsersQuery.Query parameters,
[FromBody] global::Dummy.GetUsersQuery.Query parameters,
[FromServices] global::Dummy.GetUsersQuery.Handler handler,
CancellationToken token
) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//HintName: RouteBuilder.Dummy_GetUsersQuery.g.cs
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

#pragma warning disable CS1591
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//HintName: RouteBuilder.Dummy_GetUsersQuery.g.cs
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

#pragma warning disable CS1591
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//HintName: RouteBuilder.Dummy_GetUsersQuery.g.cs
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

#pragma warning disable CS1591
Expand All @@ -13,7 +14,7 @@ private static void MapDummy_GetUsersQueryEndpoint(IEndpointRouteBuilder app)
.MapPatch(
"/test",
async (
[AsParameters] global::Dummy.GetUsersQuery.Query parameters,
[FromBody] global::Dummy.GetUsersQuery.Query parameters,
[FromServices] global::Dummy.GetUsersQuery.Handler handler,
CancellationToken token
) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//HintName: RouteBuilder.Dummy_GetUsersQuery.g.cs
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

#pragma warning disable CS1591
Expand All @@ -13,7 +14,7 @@ private static void MapDummy_GetUsersQueryEndpoint(IEndpointRouteBuilder app)
.MapPost(
"/test",
async (
[AsParameters] global::Dummy.GetUsersQuery.Query parameters,
[FromBody] global::Dummy.GetUsersQuery.Query parameters,
[FromServices] global::Dummy.GetUsersQuery.Handler handler,
CancellationToken token
) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//HintName: RouteBuilder.Dummy_GetUsersQuery.g.cs
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

#pragma warning disable CS1591
Expand All @@ -13,7 +14,7 @@ private static void MapDummy_GetUsersQueryEndpoint(IEndpointRouteBuilder app)
.MapPut(
"/test",
async (
[AsParameters] global::Dummy.GetUsersQuery.Query parameters,
[FromBody] global::Dummy.GetUsersQuery.Query parameters,
[FromServices] global::Dummy.GetUsersQuery.Handler handler,
CancellationToken token
) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//HintName: RouteBuilder.Dummy_GetUsersQuery.g.cs
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

#pragma warning disable CS1591
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//HintName: RouteBuilder.Dummy_GetUsersQuery.g.cs
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

#pragma warning disable CS1591
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//HintName: RouteBuilder.Dummy_GetUsersQuery.g.cs
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

#pragma warning disable CS1591
Expand All @@ -13,7 +14,7 @@ private static void MapDummy_GetUsersQueryEndpoint(IEndpointRouteBuilder app)
.MapPatch(
"/test",
async (
[AsParameters] global::Dummy.GetUsersQuery.Query parameters,
[FromBody] global::Dummy.GetUsersQuery.Query parameters,
[FromServices] global::Dummy.GetUsersQuery.Handler handler,
CancellationToken token
) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//HintName: RouteBuilder.Dummy_GetUsersQuery.g.cs
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

#pragma warning disable CS1591
Expand All @@ -13,7 +14,7 @@ private static void MapDummy_GetUsersQueryEndpoint(IEndpointRouteBuilder app)
.MapPost(
"/test",
async (
[AsParameters] global::Dummy.GetUsersQuery.Query parameters,
[FromBody] global::Dummy.GetUsersQuery.Query parameters,
[FromServices] global::Dummy.GetUsersQuery.Handler handler,
CancellationToken token
) =>
Expand Down
Loading

0 comments on commit da88917

Please sign in to comment.