C
C#2y ago
Kitty

❔ Swagger does not generate schemas

C#
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> Get(double latitude, double longitude)
{
// Create a HttpClient instance
var client = _clientFactory.CreateClient();

// Make a GET request to the external API
var response = await client.GetAsync($"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}&current_weather=true");

// If the request is successful
if (response.IsSuccessStatusCode)
{
// Read the response content as a JSON string
var json = await response.Content.ReadAsStringAsync();

// Deserialize the JSON string into a WeatherForecastClass object
var weatherForecast = JsonSerializer.Deserialize<WeatherForecastClass>(json);

// Return the weather forecast with a 200 OK status
return Ok(weatherForecast);
}

// If the request is not successful, log an error and return the status code from the external API
_logger.LogError("Failed to retrieve weather forecast. Status Code: {ResponseStatusCode}", response.StatusCode);
return StatusCode((int)response.StatusCode);
}
}
C#
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> Get(double latitude, double longitude)
{
// Create a HttpClient instance
var client = _clientFactory.CreateClient();

// Make a GET request to the external API
var response = await client.GetAsync($"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}&current_weather=true");

// If the request is successful
if (response.IsSuccessStatusCode)
{
// Read the response content as a JSON string
var json = await response.Content.ReadAsStringAsync();

// Deserialize the JSON string into a WeatherForecastClass object
var weatherForecast = JsonSerializer.Deserialize<WeatherForecastClass>(json);

// Return the weather forecast with a 200 OK status
return Ok(weatherForecast);
}

// If the request is not successful, log an error and return the status code from the external API
_logger.LogError("Failed to retrieve weather forecast. Status Code: {ResponseStatusCode}", response.StatusCode);
return StatusCode((int)response.StatusCode);
}
}
Swagger does not generate schema for WeatherForecastClass
3 Replies
undisputed world champions
[ProducesResponseType(typeof(WeatherForecastClass), StatusCodes.Status200OK)] should help 😉
pox
pox2y ago
Could also change the return type to Task<ActionResult<WeatherForecastClass>>
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?