Skip to content

Commit

Permalink
Add default response handling and update OpenAPI config (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcominerva authored Dec 4, 2024
2 parents ceacde5 + 73dd781 commit 52662a0
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 16 deletions.
6 changes: 3 additions & 3 deletions samples/TinyHelpers.AspNetCore.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
builder.Services.AddOpenApi(options =>
{
options.AddAcceptLanguageHeader();
options.AddDefaultResponse();
});

// Add default problem details and exception handler.
Expand Down Expand Up @@ -40,13 +41,12 @@
{
var language = Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName;
return TypedResults.NoContent();
})
.WithOpenApi();
});

app.MapPost("/api/exception", () =>
{
throw new Exception("This is an exception", innerException: new HttpRequestException("This is an inner exception"));
})
.WithOpenApi();
.ProducesProblem(StatusCodes.Status400BadRequest);

app.Run();
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net.Mime;
using Microsoft.AspNetCore.Mvc;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

Expand All @@ -7,5 +8,5 @@ namespace TinyHelpers.AspNetCore.Swagger;
internal class DefaultResponseOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
=> operation.Responses.TryAdd("default", SwaggerExtensions.GetResponse("Error", MediaTypeNames.Application.ProblemJson));
=> operation.Responses.TryAdd("default", SwaggerExtensions.GetResponse("Error", nameof(ProblemDetails), MediaTypeNames.Application.ProblemJson));
}
7 changes: 3 additions & 4 deletions src/TinyHelpers.AspNetCore.Swashbuckle/SwaggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Net.Mime;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
Expand Down Expand Up @@ -46,19 +45,19 @@ public static IServiceCollection AddSwaggerOperationParameters(this IServiceColl
public static void AddOperationParameters(this SwaggerGenOptions options)
=> options.OperationFilter<OpenApiParametersOperationFilter>();

internal static OpenApiResponse GetResponse(string description, string contentType = MediaTypeNames.Application.Json)
internal static OpenApiResponse GetResponse(string description, string id, string contentType = MediaTypeNames.Application.Json)
=> new()
{
Description = description,
Content = new Dictionary<string, OpenApiMediaType>
Content =
{
[contentType] = new()
{
Schema = new()
{
Reference = new()
{
Id = nameof(ProblemDetails),
Id = id,
Type = ReferenceType.Schema
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static class ServiceCollectionExtensions
return settings;
}

public static IServiceCollection AddRequestLocalization(this IServiceCollection services, params IEnumerable<string> cultures)
public static IServiceCollection AddRequestLocalization(this IServiceCollection services, params string[] cultures)
=> services.AddRequestLocalization(cultures, null);

public static IServiceCollection AddRequestLocalization(this IServiceCollection services, IEnumerable<string> cultures, Action<IList<IRequestCultureProvider>>? providersConfiguration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransform
Name = HeaderNames.AcceptLanguage,
In = ParameterLocation.Header,
Required = false,
Schema = new OpenApiSchema
Schema = new()
{
Type = "string",
Enum = supportedLanguages,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#if NET9_0_OR_GREATER

using System.Net.Mime;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OpenApi;
using Microsoft.OpenApi.Models;

namespace TinyHelpers.AspNetCore.OpenApi;

internal class DefaultResponseOperationTransformer : IOpenApiOperationTransformer
{
public Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransformerContext context, CancellationToken cancellationToken)
{
var defaultResponse = new OpenApiResponse()
{
Description = "Error",
Content = new Dictionary<string, OpenApiMediaType>
{
[MediaTypeNames.Application.ProblemJson] = new()
{
Schema = new()
{
Reference = new()
{
Id = nameof(ProblemDetails),
Type = ReferenceType.Schema
}
}
}
}
};

operation.Responses.TryAdd("default", defaultResponse);
return Task.CompletedTask;
}
}

#endif
2 changes: 2 additions & 0 deletions src/TinyHelpers.AspNetCore/OpenApi/OpenApiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public static class OpenApiExtensions
public static void AddAcceptLanguageHeader(this OpenApiOptions options)
=> options.AddOperationTransformer<AcceptLanguageHeaderOperationTransformer>();

public static void AddDefaultResponse(this OpenApiOptions options)
=> options.AddOperationTransformer<DefaultResponseOperationTransformer>();
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ namespace TinyHelpers.EntityFrameworkCore.Extensions;

public static class PropertyBuilderExtensions
{
public static PropertyBuilder<DateOnly> HasDateOnlyConversion<DateOnly>(this PropertyBuilder<DateOnly> propertyBuilder)
=> propertyBuilder.HasConversion<DateOnlyConverter, DateOnlyComparer>();

public static PropertyBuilder<TimeOnly> HasTimeOnlyConversion<TimeOnly>(this PropertyBuilder<TimeOnly> propertyBuilder)
=> propertyBuilder.HasConversion<TimeOnlyConverter, TimeOnlyComparer>();

public static PropertyBuilder<T?> HasJsonConversion<T>(this PropertyBuilder<T?> propertyBuilder, JsonSerializerOptions? jsonSerializerOptions = null, bool useUtcDate = false, bool serializeEnumAsString = false)
{
jsonSerializerOptions ??= new(JsonOptions.Default);
Expand Down

0 comments on commit 52662a0

Please sign in to comment.