Skip to content

Commit

Permalink
Merge branch 'main' into support-nested-modules
Browse files Browse the repository at this point in the history
  • Loading branch information
OnkelMato authored May 25, 2024
2 parents 4bac839 + 8456dd0 commit 4c750f9
Show file tree
Hide file tree
Showing 16 changed files with 477 additions and 123 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/dotnetcore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ jobs:

name: Github Actions Build
runs-on: ubuntu-latest

permissions: write-all

steps:
- uses: actions/checkout@v1
- name: Setup .NET Core
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ For a better understanding, take a good look at the [samples](https://github.com

Other extensions include:

* `Validate<T>` - [FluentValidation](https://github.com/JeremySkinner/FluentValidation) extensions to validate incoming HTTP requests which is not available with ASP.NET Core Minimal APIs.
* `Validate<T> / ValidateAsync<T>` - [FluentValidation](https://github.com/JeremySkinner/FluentValidation) extensions to validate incoming HTTP requests which is not available with ASP.NET Core Minimal APIs.
* `BindFile/BindFiles/BindFileAndSave/BindFilesAndSave` - Allows you to easily get access to a file/files that has been uploaded. Alternatively you can call `BindFilesAndSave` and this will save it to a path you specify.
* Routes to use in common ASP.NET Core middleware e.g., `app.UseExceptionHandler("/errorhandler");`.
* `IResponseNegotiator`s allow you to define how the response should look on a certain Accept header(content negotiation). Handling JSON is built in the default response but implementing an interface allows the user to choose how they want to represent resources.
Expand All @@ -21,7 +21,7 @@ Other extensions include:

### Join our Slack Channel

<a href="https://join.slack.com/t/cartercommunity/shared_invite/enQtMzY2Nzc0NjU2MTgyLWY3M2Y2Yjk3NzViN2Y3YTQ4ZDA5NWFlMTYxMTIwNDFkMTc5YWEwMDFiOWUyM2Q4ZmY5YmRkODYyYTllZDViMmE"><img src="./slack.svg" width="140px"/></a>
[![Join our slack channel](https://raw.githubusercontent.com/CarterCommunity/Carter/main/slack.png)](https://join.slack.com/t/cartercommunity/shared_invite/enQtMzY2Nzc0NjU2MTgyLWY3M2Y2Yjk3NzViN2Y3YTQ4ZDA5NWFlMTYxMTIwNDFkMTc5YWEwMDFiOWUyM2Q4ZmY5YmRkODYyYTllZDViMmE)

#### Routing

Expand Down
4 changes: 2 additions & 2 deletions samples/CarterSample/Features/CastMembers/CastMemberModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ public class CastMemberModule : ICarterModule
{
public void AddRoutes(IEndpointRouteBuilder app)
{
app.MapPost("/castmembers", (HttpRequest req, CastMember castMember) =>
app.MapPost("/castmembers", async (HttpRequest req, CastMember castMember) =>
{
var result = req.Validate<CastMember>(castMember);
var result = await req.ValidateAsync<CastMember>(castMember);
if (!result.IsValid)
{
Expand Down
7 changes: 6 additions & 1 deletion samples/ValidatorOnlyProject/CastMemberValidator.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
namespace ValidatorOnlyProject
{
using System.Threading.Tasks;
using FluentValidation;

public class CastMemberValidator : AbstractValidator<CastMember>
{
public CastMemberValidator()
{
this.RuleFor(x => x.Name).NotEmpty();
this.RuleFor(x => x.Name).NotEmpty().MustAsync(async (name, cancellation) =>
{
await Task.Delay(50);
return true;
});
}
}
}
Binary file added slack.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
107 changes: 0 additions & 107 deletions slack.svg

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
namespace Carter.Analyzers;

using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal sealed class CarterModuleShouldNotHaveDependenciesAnalyzer : DiagnosticAnalyzer
{
public override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.RegisterCompilationStartAction(OnCompilationStart);
}

private static void OnCompilationStart(CompilationStartAnalysisContext context)
{
var carterModuleType = context.Compilation.GetTypeByMetadataName("Carter.ICarterModule");
if (carterModuleType is null)
{
return;
}

context.RegisterSymbolAction(ctx => OnTypeAnalysis(ctx, carterModuleType), SymbolKind.NamedType);
}

private static void OnTypeAnalysis(SymbolAnalysisContext context, INamedTypeSymbol carterModuleType)
{
var typeSymbol = (INamedTypeSymbol)context.Symbol;
if (!typeSymbol.Interfaces.Contains(carterModuleType, SymbolEqualityComparer.Default))
{
return;
}

foreach (var constructor in typeSymbol.Constructors)
{
if (constructor.DeclaredAccessibility == Accessibility.Private || constructor.Parameters.Length == 0)
{
continue;
}

foreach (var syntaxReference in constructor.DeclaringSyntaxReferences)
{
var node = syntaxReference.GetSyntax();
SyntaxToken identifier;
if (node is ConstructorDeclarationSyntax constructorDeclaration)
{
identifier = constructorDeclaration.Identifier;
} else if (node is RecordDeclarationSyntax recordDeclaration)
{
identifier = recordDeclaration.Identifier;
}
else
{
continue;
}

var diagnostic = Diagnostic.Create(
DiagnosticDescriptors.CarterModuleShouldNotHaveDependencies,
identifier.GetLocation(),
identifier.Text
);
context.ReportDiagnostic(diagnostic);
}
}
}

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = [DiagnosticDescriptors.CarterModuleShouldNotHaveDependencies];
}
16 changes: 16 additions & 0 deletions src/Carter/Analyzers/DiagnosticDescriptors.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.CodeAnalysis;

namespace Carter.Analyzers;

internal static class DiagnosticDescriptors
{
public static readonly DiagnosticDescriptor CarterModuleShouldNotHaveDependencies = new(
"CARTER1",
"Carter module should not have dependencies",
"'{0}' should not have dependencies",
"Usage",
DiagnosticSeverity.Warning,
true,
"When a class implements ICarterModule, it should not have any dependencies. This is because Carter uses minimal APIs and dependencies should be declared in the request delegate."
);
}
10 changes: 9 additions & 1 deletion src/Carter/Carter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,28 @@
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<MinVerSkip Condition="'$(Configuration)' == 'Debug'">true</MinVerSkip>
<PackageReadmeFile>README.md</PackageReadmeFile>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<NoWarn>RS2008</NoWarn>
</PropertyGroup>
<ItemGroup>
<None Include="..\..\media\carterlogo.png" Pack="true" PackagePath="\" />
<None Include="..\..\README.md" Pack="true" PackagePath="\"/>
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FluentValidation" Version="11.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.7.0" PrivateAssets="compile" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="8.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="all" />
<PackageReference Include="MinVer" Version="2.5.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>

</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="$(AssemblyName).Tests" />
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions src/Carter/DependencyContextAssemblyCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ private static Assembly SafeLoadAssembly(AssemblyName assemblyName)
{
try
{
#pragma warning disable RS1035
return Assembly.Load(assemblyName);
#pragma warning restore RS1035
}
catch (Exception)
{
Expand Down
Loading

0 comments on commit 4c750f9

Please sign in to comment.