Skip to content

Commit

Permalink
Version 0.0.4 (#8)
Browse files Browse the repository at this point in the history
Update to .NET 9.0
  • Loading branch information
KrzysztofPajak authored Jan 10, 2025
1 parent 6c10528 commit 944f1a2
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 114 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
dotnet-version: 9.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nuget-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v2
with:
dotnet-version: '8.0.x'
dotnet-version: '9.0.x'

- name: Restore dependencies
run: dotnet restore
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,4 @@ public class YourResponse
[RoleBasedAccess("Admin")]
public string AdminOnlyField { get; set; }
}
```
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using MediatR;
using System.ComponentModel.DataAnnotations;

using System.Text;
using System.Text.Json;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentValidation" Version="11.10.0" />
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.10.0" />
<PackageReference Include="FluentValidation" Version="11.11.0" />
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.11.0" />
<PackageReference Include="MediatR" Version="12.4.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.6" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.6" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
<PackageReference Include="Scalar.AspNetCore" Version="1.2.74" />
</ItemGroup>

<ItemGroup>
Expand Down
85 changes: 19 additions & 66 deletions samples/MediatRSampleWebApplication/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,23 @@
using MediatRSampleWebApplication.EndpointFilters;
using MediatRSampleWebApplication.Models;
using MediatRSampleWebApplication.Queries.Companies;
using MediatRSampleWebApplication.Transformers;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Http.Json;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Scalar.AspNetCore;
using System.Text;
using System.Text.Json.Serialization;
using static System.Net.Mime.MediaTypeNames;

var builder = Microsoft.AspNetCore.Builder.WebApplication.CreateBuilder(args);
builder.Services.AddProblemDetails();

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
builder.Services.AddOpenApi("v1", options =>
{
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Name = "Authorization",
Type = SecuritySchemeType.Http,
Scheme = "Bearer",
BearerFormat = "JWT",
In = ParameterLocation.Header,
Description = "Enter 'Bearer' [space] and then your valid token in the text input below.\r\n\r\nExample: \"Bearer 12345abcdef\"",
});

options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
Array.Empty<string>()
}
});
options.OpenApiVersion = Microsoft.OpenApi.OpenApiSpecVersion.OpenApi3_0;
options.AddDocumentTransformer<BearerSecuritySchemeTransformer>();
});

builder.Services.AddValidatorsFromAssembly(typeof(Program).Assembly);
Expand Down Expand Up @@ -86,12 +64,16 @@
var app = builder.Build();

app.UseStatusCodePages();
app.MapOpenApi();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.MapScalarApiReference(options =>
{
options.WithTitle("OpenApi Playground");
options.WithDefaultHttpClient(ScalarTarget.CSharp, ScalarClient.HttpClient);
});
}

// Register MediatR endpoints (based on attributes Endpoint)
Expand All @@ -100,60 +82,31 @@
// Register MediatR endpoints (based on manual registration)
app.MapPostWithMediatR<CreateRole.CreateRoleCommand, Role>("/role/create")
.WithDisplayName("CreateRole")
.WithOpenApi(x =>
{
x.Tags = [new OpenApiTag() { Name = "Role" }];
return x;
});
.WithTags("Role");

app.MapGetWithMediatR<GetRoles.GetRolesCommand, IList<Role>>("/role/get")
.WithDisplayName("GetRoles")
.WithOpenApi(x =>
{
x.Tags = [new OpenApiTag() { Name = "Role" }];
return x;
});
.WithTags("Role");

app.MapGetWithMediatR<GetRoleById.GetRoleByIdCommand, Role>("/role/get/{id}")
.WithDisplayName("GetRoleById")
.WithOpenApi(x =>
{
x.Tags = [new OpenApiTag() { Name = "Role" }];
return x;
});
.WithTags("Role");

app.MapPutWithMediatR<UpdateRole.UpdateRoleCommand, Role>("/role/update/{Id}")
.WithDisplayName("UpdateRole")
.WithOpenApi(x =>
{
x.Tags = [new OpenApiTag() { Name = "Role" }];
return x;
});
.WithTags("Role");

app.MapDeleteWithMediatR<DeleteRole.DeleteRoleCommand, bool>("/role/delete/{Id}")
.WithDisplayName("DeleteRole")
.WithOpenApi(x =>
{
x.Tags = [new OpenApiTag() { Name = "Role" }];
return x;
});

.WithTags("Role");

app.MapPostWithMediatR<CreateCompany.CreateCompanyCommand, Company>("/company/create")
.WithDisplayName("CreateCompany")
.WithOpenApi(x =>
{
x.Tags = [new OpenApiTag() { Name = "Company" }];
return x;
});
.WithTags("Company");

app.MapGetWithMediatR<GetCompany.Companies, IList<Company>>("/company/get")
.WithDisplayName("GetCompanies")
.WithOpenApi(x =>
{
x.Tags = [new OpenApiTag() { Name = "Company" }];
return x;
});
.WithTags("Company");


app.Run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"launchUrl": "scalar/v1",
"applicationUrl": "http://localhost:5117",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
Expand All @@ -22,7 +22,7 @@
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"launchUrl": "scalar/v1",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.OpenApi;
using Microsoft.OpenApi.Models;

namespace MediatRSampleWebApplication.Transformers;

internal sealed class BearerSecuritySchemeTransformer(IAuthenticationSchemeProvider authenticationSchemeProvider) : IOpenApiDocumentTransformer
{
public async Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerContext context, CancellationToken cancellationToken)
{
var authenticationSchemes = await authenticationSchemeProvider.GetAllSchemesAsync();
if (authenticationSchemes.Any(authScheme => authScheme.Name == "Bearer"))
{
var requirements = new Dictionary<string, OpenApiSecurityScheme>
{
["Bearer"] = new OpenApiSecurityScheme
{
Type = SecuritySchemeType.Http,
Scheme = "bearer",
In = ParameterLocation.Header,
BearerFormat = "Json Web Token"
}
};
document.Components ??= new OpenApiComponents();
document.Components.SecuritySchemes = requirements;
}
}
}
9 changes: 1 addition & 8 deletions src/MediatR.MinimalApi/Behaviors/ValidationBehavior.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using FluentValidation;
using FluentValidation.Results;
using FluentValidation;
using MediatR.MinimalApi.Exceptions;

namespace MediatR.MinimalApi.Behaviors;
Expand Down Expand Up @@ -27,10 +26,4 @@ public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TRe

return await next();
}

private static string BuildErrorMessage(IEnumerable<ValidationFailure> errors)
{
IEnumerable<string> values = errors.Select((ValidationFailure x) => $"{Environment.NewLine} -- {x.PropertyName}: {x.ErrorMessage} Severity: {x.Severity.ToString()}");
return "Validation failed: " + string.Join(string.Empty, values);
}
}
2 changes: 1 addition & 1 deletion src/MediatR.MinimalApi/Exceptions/HttpResponseException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ public HttpResponseException(int statusCode, string message, Dictionary<string,
StatusCode = statusCode;
Extensions = extensions;
}
}
}
2 changes: 1 addition & 1 deletion src/MediatR.MinimalApi/Extensions/OpenApiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void WithOpenApiDescription(this RouteHandlerBuilder builder, Type

if (!string.IsNullOrEmpty(attribute!.TagName))
{
operation.Tags = [new() { Name = attribute.TagName }];
builder.WithTags(attribute!.TagName);
}

switch (attribute.Method)
Expand Down
16 changes: 7 additions & 9 deletions src/MediatR.MinimalApi/MediatR.MinimalApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,25 @@
<Authors>Krzysztof Pajak</Authors>
<Copyright>Copyright Krzysztof Pajak</Copyright>
<Description>Extends Mediatr functionality for Minimal APIs in ASP.NET Core</Description>
<Version>0.0.3</Version>
<Version>0.0.4</Version>
<RepositoryUrl>https://github.com/KrzysztofPajak/MediatR.MinimalApi</RepositoryUrl>
<PackageLicenseUrl>https://github.com/KrzysztofPajak/MediatR.MinimalApi/blob/develop/LICENSE</PackageLicenseUrl>
<RepositoryType>Git</RepositoryType>
<PackageTags>mediator;mediatr;minimalapi</PackageTags>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\MediatR.MinimalApi.snk</AssemblyOriginatorKeyFile>
<TargetFramework>net8.0</TargetFramework>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="FluentValidation" Version="11.11.0" />
<PackageReference Include="MediatR" Version="12.4.1" />
<PackageReference Include="Microsoft.OpenApi" Version="1.6.23" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentValidation" Version="11.10.0" />
<PackageReference Include="MediatR" Version="12.4.1" />
<PackageReference Include="Microsoft.OpenApi" Version="1.6.14" />
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.6" />
<PackageReference Include="xunit" Version="2.6.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3">
<PrivateAssets>all</PrivateAssets>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
Expand All @@ -12,18 +12,18 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="FakeItEasy" Version="8.2.0" />
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="Lamar" Version="12.1.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="8.0.6" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="xunit" Version="2.6.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="FakeItEasy" Version="8.3.0" />
<PackageReference Include="FluentAssertions" Version="7.0.0" />
<PackageReference Include="Lamar" Version="14.0.1" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="9.0.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.7.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3">
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down

0 comments on commit 944f1a2

Please sign in to comment.