feat: configurar swagger con soporte para multiples versiones v1 y v2
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
<PackageReference Include="Asp.Versioning.Http" Version="8.1.1" />
|
||||
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.3" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.1.4" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
42
ApiVersioningDemo.Api/ConfigureSwaggerOptions.cs
Normal file
42
ApiVersioningDemo.Api/ConfigureSwaggerOptions.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using Asp.Versioning.ApiExplorer;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.OpenApi;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
|
||||
namespace ApiVersioningDemo.Api;
|
||||
|
||||
public class ConfigureSwaggerOptions : IConfigureOptions<SwaggerGenOptions>
|
||||
{
|
||||
private readonly IApiVersionDescriptionProvider _provider;
|
||||
|
||||
public ConfigureSwaggerOptions(IApiVersionDescriptionProvider provider)
|
||||
{
|
||||
_provider = provider;
|
||||
}
|
||||
|
||||
public void Configure(SwaggerGenOptions options)
|
||||
{
|
||||
// Por cada versión de API detectada, crea un documento Swagger
|
||||
foreach (var description in _provider.ApiVersionDescriptions)
|
||||
{
|
||||
options.SwaggerDoc(description.GroupName, CreateInfoForApiVersion(description));
|
||||
}
|
||||
}
|
||||
|
||||
private static OpenApiInfo CreateInfoForApiVersion(ApiVersionDescription description)
|
||||
{
|
||||
var info = new OpenApiInfo()
|
||||
{
|
||||
Title = "Mi API Versionada",
|
||||
Version = description.ApiVersion.ToString(),
|
||||
Description = "Ejemplo de API REST con versionado en .NET"
|
||||
};
|
||||
|
||||
if (description.IsDeprecated)
|
||||
{
|
||||
info.Description += " (Esta versión está obsoleta)";
|
||||
}
|
||||
|
||||
return info;
|
||||
}
|
||||
}
|
||||
@@ -11,8 +11,11 @@ public class WeatherForecastV2Controller : ControllerBase
|
||||
{
|
||||
private static readonly string[] Summaries = [
|
||||
"Helado", "Frío", "Fresco", "Templado", "Cálido", "Caluroso", "Sofocante", "SuperScorching"
|
||||
]; [HttpGet(Name = "GetWeatherForecastV2")]
|
||||
public IActionResult Get() // Cambiamos a IActionResult para devolver un objeto anónimo
|
||||
];
|
||||
|
||||
[HttpGet(Name = "GetWeatherForecastV2")]
|
||||
// CAMBIO 1: Cambiamos IActionResult por ActionResult<TipoConcreto>
|
||||
public ActionResult<WeatherForecastResponseV2> Get()
|
||||
{
|
||||
var forecast = Enumerable.Range(1, 5).Select(index => new WeatherForecast
|
||||
{
|
||||
@@ -21,11 +24,12 @@ public class WeatherForecastV2Controller : ControllerBase
|
||||
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
|
||||
}).ToArray();
|
||||
|
||||
// 3. EL BREAKING CHANGE: Ahora devolvemos un objeto con la "Ciudad" y la lista adentro
|
||||
return Ok(new
|
||||
{
|
||||
Ciudad = "Buenos Aires",
|
||||
Pronosticos = forecast
|
||||
});
|
||||
// CAMBIO 2: Devolvemos el record tipado en lugar del anónimo
|
||||
var response = new WeatherForecastResponseV2("Buenos Aires", forecast);
|
||||
|
||||
return Ok(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Definición fuerte de la respuesta para que Swagger la entienda
|
||||
public record WeatherForecastResponseV2(string Ciudad, IEnumerable<WeatherForecast> Pronosticos);
|
||||
@@ -1,44 +1,52 @@
|
||||
// ApiVersioningDemo.api/Program.cs
|
||||
using ApiVersioningDemo.Api;
|
||||
using Asp.Versioning;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// CONFIGURACIÓN DE VERSIONADO
|
||||
// 1. Configuración de Versionado (YA LO TENÍAS)
|
||||
builder.Services.AddApiVersioning(options =>
|
||||
{
|
||||
// Si el cliente no especifica versión, usaremos la 1.0 por defecto
|
||||
options.DefaultApiVersion = new ApiVersion(1, 0);
|
||||
options.AssumeDefaultVersionWhenUnspecified = true;
|
||||
|
||||
// Esto añade una cabecera HTTP en las respuestas diciendo qué versiones existen (ej: api-supported-versions: 1.0, 2.0)
|
||||
options.ReportApiVersions = true;
|
||||
})
|
||||
.AddMvc() // Integra el versionado con los Controladores
|
||||
.AddMvc()
|
||||
.AddApiExplorer(options =>
|
||||
{
|
||||
// Configura el formato para Swagger (ej: "v1", "v2")
|
||||
options.GroupNameFormat = "'v'VVV";
|
||||
// IMPORTANTE: El formato "'v'VVV" hace que la versión se llame "v1", "v2", etc.
|
||||
options.GroupNameFormat = "'v'VVV";
|
||||
options.SubstituteApiVersionInUrl = true;
|
||||
});
|
||||
|
||||
// 2. Configuración de Swagger
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
// Esta lógica crea un documento Swagger por cada versión descubierta automáticamente
|
||||
builder.Services.ConfigureOptions<ConfigureSwaggerOptions>();
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||
builder.Services.AddOpenApi();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
// 3. Activar la Interfaz Gráfica
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.MapOpenApi();
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(options =>
|
||||
{
|
||||
// Genera un endpoint JSON por cada versión que exista en la API
|
||||
var descriptions = app.DescribeApiVersions();
|
||||
foreach (var description in descriptions)
|
||||
{
|
||||
var url = $"/swagger/{description.GroupName}/swagger.json";
|
||||
var name = description.GroupName.ToUpperInvariant();
|
||||
options.SwaggerEndpoint(url, name);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
app.Run();
|
||||
Reference in New Issue
Block a user