feat: configurar swagger con soporte para multiples versiones v1 y v2

This commit is contained in:
2026-03-07 20:41:26 -03:00
parent 7856147cfa
commit 62a10af833
4 changed files with 81 additions and 26 deletions

View File

@@ -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);