Skip to content

Commit

Permalink
feat: Integrate 'Accept-Language' parameter in all endpoints from Swa…
Browse files Browse the repository at this point in the history
…gger UI
  • Loading branch information
MrDave1999 committed Jul 1, 2024
1 parent 6d7b35e commit a967325
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/HostApplication/Extensions/SwaggerGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ public static class SwaggerGen
public static IServiceCollection AddSwagger(this IServiceCollection services)
{
services.AddSwaggerGen(options =>
{
{
options.OperationFilter<AcceptLanguageHeaderParameter>();
options.EnableAnnotations();
options.SwaggerDoc("v1", new OpenApiInfo { Title = "DentallApi", Version = "v1" });
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
Expand All @@ -27,7 +28,7 @@ public static IServiceCollection AddSwagger(this IServiceCollection services)
Id = "Bearer"
}
},
new string[] { }
Array.Empty<string>()
}
});
var coreAssemblyName = typeof(GetDependentsByCurrentUserIdUseCase).Assembly.GetName().Name;
Expand All @@ -44,4 +45,31 @@ public static IServiceCollection AddSwagger(this IServiceCollection services)
});
return services;
}

private class AcceptLanguageHeaderParameter(IConfiguration configuration) : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
operation.Parameters ??= [];
var languageOptions = new List<IOpenApiAny>();
var languages = configuration.GetLanguages();
var defaultLanguage = configuration.GetDefaultLanguage();
foreach (var language in languages)
languageOptions.Add(new OpenApiString(language));

operation.Parameters.Add(new OpenApiParameter
{
Name = "Accept-Language",
Description = "Language preference for the response.",
In = ParameterLocation.Header,
Required = false,
Schema = new OpenApiSchema
{
Type = "string",
Default = new OpenApiString(defaultLanguage),
Enum = languageOptions
}
});
}
}
}

0 comments on commit a967325

Please sign in to comment.