diff --git a/Svrnty.CQRS.MinimalApi/EndpointRouteBuilderExtensions.cs b/Svrnty.CQRS.MinimalApi/EndpointRouteBuilderExtensions.cs index fe5e3f0..1a6498d 100644 --- a/Svrnty.CQRS.MinimalApi/EndpointRouteBuilderExtensions.cs +++ b/Svrnty.CQRS.MinimalApi/EndpointRouteBuilderExtensions.cs @@ -83,7 +83,8 @@ public static class EndpointRouteBuilderExtensions .Produces(200, queryMeta.QueryResultType) .Produces(400) .Produces(401) - .Produces(403); + .Produces(403) + .WithAllowAnonymousIfAttributePresent(queryMeta.QueryType); } private static void MapQueryGet( @@ -146,7 +147,8 @@ public static class EndpointRouteBuilderExtensions .Produces(200, queryMeta.QueryResultType) .Produces(400) .Produces(401) - .Produces(403); + .Produces(403) + .WithAllowAnonymousIfAttributePresent(queryMeta.QueryType); } public static IEndpointRouteBuilder MapSvrntyCommands(this IEndpointRouteBuilder endpoints, string routePrefix = "api/command") @@ -213,7 +215,8 @@ public static class EndpointRouteBuilderExtensions .Produces(200) .Produces(400) .Produces(401) - .Produces(403); + .Produces(403) + .WithAllowAnonymousIfAttributePresent(commandMeta.CommandType); } private static void MapCommandWithResult( @@ -260,6 +263,17 @@ public static class EndpointRouteBuilderExtensions .Produces(200, commandMeta.CommandResultType) .Produces(400) .Produces(401) - .Produces(403); + .Produces(403) + .WithAllowAnonymousIfAttributePresent(commandMeta.CommandType); + } + + private static RouteHandlerBuilder WithAllowAnonymousIfAttributePresent(this RouteHandlerBuilder builder, Type type) + { + var allowAnonymousAttribute = type.GetCustomAttribute(); + if (allowAnonymousAttribute != null) + { + builder.AllowAnonymous(); + } + return builder; } }