Skip to content

Instantly share code, notes, and snippets.

View stormwild's full-sized avatar
🏠
Working from home

Alexander R Torrijos stormwild

🏠
Working from home
View GitHub Profile
@dj-nitehawk
dj-nitehawk / Endpoint.cs
Created August 5, 2024 04:28
Unit testing an endpoint that publishes an event
[HttpGet("publish"), AllowAnonymous]
sealed class MyEndpoint : EndpointWithoutRequest
{
public override async Task HandleAsync(CancellationToken c)
{
var evnt = new MyEvent { Message = "hello!" };
await PublishAsync(evnt);
await SendAsync("all good!");
}
}
@dj-nitehawk
dj-nitehawk / 1-Program.cs
Last active November 16, 2024 23:12
Response sending post-processor with ErrorOr package
var bld = WebApplication.CreateBuilder(args);
bld.Services
.SwaggerDocument()
.AddFastEndpoints();
var app = bld.Build();
app.UseFastEndpoints(
c =>
{
c.Errors.UseProblemDetails();
@dj-nitehawk
dj-nitehawk / AppFixture.cs
Last active October 1, 2024 09:02
Integration testing with TestContainers & AppFixture
using Testcontainers.MongoDb;
public class Sut : AppFixture<Program>
{
const string Database = "TestingDB";
const string RootUsername = "root";
const string RootPassword = "password";
MongoDbContainer _container = null!;
@dj-nitehawk
dj-nitehawk / Program.cs
Last active October 1, 2024 09:01
Customizing Swagger Middleware & UI Paths
app.UseSwaggerGen(
s =>
{
//where to serve swagger.json files from
s.Path = "/PREFIX/swagger/{documentName}/swagger.json";
//api endpoint server base path customization
s.PostProcess = (document, request) =>
{
document.Servers.Clear();
@dj-nitehawk
dj-nitehawk / Program.cs
Created January 21, 2024 05:40
Results pattern with a Post-Processor doing the response sending.
var bld = WebApplication.CreateBuilder(args);
bld.Services
.AddFastEndpoints()
.SwaggerDocument();
var app = bld.Build();
app.UseFastEndpoints()
.UseSwaggerGen();
app.Run();
@dj-nitehawk
dj-nitehawk / 1-BaseQuestion.cs
Last active October 1, 2024 09:04
Validator inheritance for polymorphic DTOs.
[
JsonPolymorphic(TypeDiscriminatorPropertyName = "_t"),
JsonDerivedType(typeof(MultiChoiceQuestionRequest), "mcq"),
JsonDerivedType(typeof(RatingQuestionRequest), "rq")
]
public class BaseQuestionRequest
{
public int Id { get; set; }
}
@dj-nitehawk
dj-nitehawk / Program.cs
Created October 6, 2023 10:43
Unit testing an endpoint that executes a command
sealed class MyRequest
{
public int Id { get; set; }
}
public sealed class MyCommand : ICommand<int>
{
public string Identity { get; set; }
}
@dj-nitehawk
dj-nitehawk / AddCustomHeader.cs
Created September 18, 2023 02:07
Customizing Swagger Spec With An IOperationProcessor
internal sealed class AddCustomHeader : IOperationProcessor
{
public bool Process(OperationProcessorContext context)
{
var hdrParameter = new OpenApiParameter()
{
Name = "x-custom",
Kind = OpenApiParameterKind.Header,
IsRequired = true,
Type = JsonObjectType.String,
@dj-nitehawk
dj-nitehawk / Program.cs
Last active April 12, 2025 20:40
Showing deprecated endpoint versions in Swagger
var bld = WebApplication.CreateBuilder();
bld.Services
.AddFastEndpoints()
.SwaggerDocument(o =>
{
o.DocumentSettings = s =>
{
s.DocumentName = "Initial Release";
s.Version = "v0";
};
@dj-nitehawk
dj-nitehawk / Program.cs
Last active October 1, 2024 09:03
Request DTO inheritance with Validator composition
public class BaseRequest
{
public string? Id { get; init; }
}
public class BaseRequestValidator : Validator<BaseRequest>
{
public BaseRequestValidator()
{
RuleFor(x => x.Id)