Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for nested modules #350

Merged
merged 2 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions samples/CarterSample/Features/Home/NestedHomeModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace CarterSample.Features.Home;

public static class NestedHomeModule
{
public class HomeModule : ICarterModule
{
public void AddRoutes(IEndpointRouteBuilder app)
{
app.MapGet("/nestedHome", (HttpResponse res) =>
{
res.StatusCode = 409;
return Results.Text("There's no place like 127.0.0.1");
});
}
}
}
2 changes: 1 addition & 1 deletion src/Carter/CarterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ private static IEnumerable<Type> GetNewModules(CarterConfigurator carterConfigur
!t.IsAbstract &&
typeof(ICarterModule).IsAssignableFrom(t) &&
t != typeof(ICarterModule) &&
t.IsPublic
(t.IsPublic || t.IsNestedPublic)
));

carterConfigurator.ModuleTypes.AddRange(modules);
Expand Down
60 changes: 53 additions & 7 deletions test/Carter.Tests/RouteExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ public RouteExtensionsTests(ITestOutputHelper outputHelper)

x.AddRouting();
x.AddCarter(configurator: c =>
{
c.WithModule<TestModule>();
c.WithValidator<TestModelValidator>();
}
);
{
c.WithModule<TestModule>();
c.WithValidator<TestModelValidator>();
});
})
.Configure(x =>
{
Expand Down Expand Up @@ -82,14 +81,61 @@ public async Task Should_hit_route_if_validation_successful(string httpMethod)
{
var res = await this.httpClient.SendAsync(new HttpRequestMessage(new HttpMethod(httpMethod), "/endpointfilter")
{
Content = new StringContent(JsonConvert.SerializeObject(new TestModel{MyStringProperty = "hi", MyIntProperty = 123}), Encoding.UTF8, "application/json")
Content = new StringContent(JsonConvert.SerializeObject(new TestModel { MyStringProperty = "hi", MyIntProperty = 123 }), Encoding.UTF8, "application/json")
});

var body = await res.Content.ReadAsStringAsync();

Assert.Equal(httpMethod, body);
}
}
public class NestedRouteExtensionsTests
{
private readonly TestServer server;

private readonly HttpClient httpClient;

public NestedRouteExtensionsTests(ITestOutputHelper outputHelper)
{
this.server = new TestServer(
new WebHostBuilder()
.ConfigureServices(x =>
{
x.AddLogging(b =>
{
XUnitLoggerExtensions.AddXUnit((ILoggingBuilder)b, outputHelper, x => x.IncludeScopes = true);
b.SetMinimumLevel(LogLevel.Debug);
});

x.AddSingleton<IDependency, Dependency>();

x.AddRouting();
x.AddCarter(configurator: c => {
c.WithValidator<TestModelValidator>();
}
);
})
.Configure(x =>
{
x.UseRouting();
x.UseEndpoints(builder => builder.MapCarter());
})
);
this.httpClient = this.server.CreateClient();
}

[Theory]
[InlineData("GET")]
public async Task Should_have_nested_class_registered(string httpMethod)
{
var res = await this.httpClient.SendAsync(new HttpRequestMessage(new HttpMethod(httpMethod), "/nested")
{
Content = new StringContent(JsonConvert.SerializeObject(new TestModel()), Encoding.UTF8, "application/json")
});
Assert.Equal(HttpStatusCode.OK, res.StatusCode);
}

}

internal interface IDependency
{
Expand Down
18 changes: 18 additions & 0 deletions test/Carter.Tests/TestModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,22 @@ public void AddRoutes(IEndpointRouteBuilder app)
app.MapPut<TestModel>("/endpointfilter", (IDependency dependency, TestModel testModel) => "PUT");
}
}
public static class NestedTestModule
{
public class TestModule : ICarterModule
{
private Guid instanceId;

public TestModule()
{
this.instanceId = Guid.NewGuid();
}

public void AddRoutes(IEndpointRouteBuilder app)
{
app.MapGet("/nested", async (HttpContext ctx) => { await ctx.Response.WriteAsync("Hello Nested"); });
}
}
}

}
2 changes: 1 addition & 1 deletion test/Carter.Tests/TypeExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class TypeExtensionTests
[Fact]
public void MustDeriveFrom_TypesDerivingFrom_WontThrow()
{
var types = new[] { typeof(TestModule), typeof(StreamModule) }.ToArray();
var types = new[] { typeof(TestModule), typeof(StreamModule), typeof(NestedTestModule.TestModule) }.ToArray();
types.MustDeriveFrom<ICarterModule>();
}

Expand Down
Loading