Skip to content

Commit

Permalink
Update Swagger config, package versions, and clean up code (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcominerva authored Sep 23, 2024
2 parents 62910a5 + 5daa55c commit 96aad75
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 40 deletions.
2 changes: 1 addition & 1 deletion samples/TinyHelpers.AspNetCore.Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.OpenApi.Models;
using TinyHelpers.AspNetCore.ExceptionHandlers;
using TinyHelpers.AspNetCore.Extensions;
using TinyHelpers.AspNetCore.Swagger;

Expand Down Expand Up @@ -47,6 +46,7 @@
builder.Services.AddSwaggerGen(options =>
{
options.AddAcceptLanguageHeader();
options.AddDefaultResponse();

// Enable Swagger integration for custom parameters.
options.AddOperationParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.8.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ namespace TinyHelpers.AspNetCore.DataAnnotations;
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public class AllowedExtensionsAttribute(params string[] extensions) : ValidationAttribute("Only files with the following extensions are supported: {0}")
{
private readonly IEnumerable<string> extensions = extensions.Select(e => e.ToLowerInvariant().Replace("*.", string.Empty));
private readonly IEnumerable<string> extensions = extensions.Select(e => e.Replace("*.", string.Empty));

protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
{
if (value is IFormFile file)
{
var extension = Path.GetExtension(file.FileName).ToLower()[1..];
if (!extensions.Contains(extension))
var extension = Path.GetExtension(file.FileName)[1..];
if (!extensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
{
return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class ContentTypeAttribute : ValidationAttribute
public ContentTypeAttribute(params string[] validContentTypes)
: base(DefaultErrorMessage)
{
this.validContentTypes = validContentTypes.Select(s => s.ToLowerInvariant());
this.validContentTypes = validContentTypes;
}

public ContentTypeAttribute(FileType fileType)
Expand All @@ -43,7 +43,7 @@ public ContentTypeAttribute(FileType fileType)

protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
{
if (value is IFormFile formFile && !validContentTypes.Contains(formFile.ContentType))
if (value is IFormFile formFile && !validContentTypes.Contains(formFile.ContentType, StringComparer.OrdinalIgnoreCase))
{
return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public static IServiceCollection AddDefaultProblemDetails(this IServiceCollectio
options.CustomizeProblemDetails = context =>
{
var statusCode = context.ProblemDetails.Status.GetValueOrDefault(StatusCodes.Status500InternalServerError);

context.ProblemDetails.Type ??= $"https://httpstatuses.io/{statusCode}";
context.ProblemDetails.Title ??= ReasonPhrases.GetReasonPhrase(statusCode);
context.ProblemDetails.Instance ??= context.HttpContext.Request.Path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ namespace TinyHelpers.AspNetCore.Swagger;
internal class DefaultResponseOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
=> operation.Responses.TryAdd("default", SwaggerExtensions.GetResponse("Error"));
=> operation.Responses.TryAdd("default", SwaggerExtensions.GetResponse("Error", "application/problem+json"));
}
30 changes: 2 additions & 28 deletions src/TinyHelpers.AspNetCore/Swagger/SwaggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,6 @@ public static void AddDefaultResponse(this SwaggerGenOptions options)
public static void AddAcceptLanguageHeader(this SwaggerGenOptions options)
=> options.OperationFilter<AcceptLanguageHeaderOperationFilter>();

//public static void AddDateOnlyTypeMapping(this SwaggerGenOptions options, bool useCurrentDateAsExample = false)
// => options.AddDateOnlyTypeMapping(useCurrentDateAsExample ? DateOnly.FromDateTime(DateTime.Now).ToString() : null);

//public static void AddDateOnlyTypeMapping(this SwaggerGenOptions options, string? example)
//{
// options.MapType<DateOnly>(() => new()
// {
// Type = "string",
// Format = "date",
// Example = example is not null ? new OpenApiString(example) : null
// });
//}

//public static void AddTimeOnlyTypeMapping(this SwaggerGenOptions options, bool useCurrentTimeAsExample = false)
// => options.AddTimeOnlyTypeMapping(useCurrentTimeAsExample ? TimeOnly.FromDateTime(DateTime.Now).ToString("hh:mm:ss") : null);

//public static void AddTimeOnlyTypeMapping(this SwaggerGenOptions options, string? example)
//{
// options.MapType<TimeOnly>(() => new()
// {
// Type = "string",
// Format = "time",
// Example = example is not null ? new OpenApiString(example) : null
// });
//}

public static void AddTimeSpanTypeMapping(this SwaggerGenOptions options, bool useCurrentTimeAsExample = false)
=> options.AddTimeSpanTypeMapping(useCurrentTimeAsExample ? TimeOnly.FromDateTime(DateTime.Now).ToString("hh:mm:ss") : null);

Expand Down Expand Up @@ -72,13 +46,13 @@ public static IServiceCollection AddSwaggerOperationParameters(this IServiceColl
public static void AddOperationParameters(this SwaggerGenOptions options)
=> options.OperationFilter<OpenApiParametersOperationFilter>();

internal static OpenApiResponse GetResponse(string description)
internal static OpenApiResponse GetResponse(string description, string contentType = MediaTypeNames.Application.Json)
=> new()
{
Description = description,
Content = new Dictionary<string, OpenApiMediaType>
{
[MediaTypeNames.Application.Json] = new()
[contentType] = new()
{
Schema = new()
{
Expand Down
2 changes: 1 addition & 1 deletion src/TinyHelpers.AspNetCore/TinyHelpers.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.7.3" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.8.0" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions tests/TinyHelpers.Tests/TinyHelpers.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down

0 comments on commit 96aad75

Please sign in to comment.